CSS :read-only
Pseudo-Class
The :read-only
pseudo-class selects form fields or elements that are not editable by the user. It helps apply styles specifically to fields with the readonly
attribute, distinguishing them from editable fields.
1. Syntax
✅ This applies a gray border and light gray background to read-only input fields.
2. Example – Styling Read-Only Fields
✅ Editable fields get a green border and light green background.
✅ Read-only fields get a gray border and gray background.
3. :read-only
vs :read-write
Pseudo-Class | Targets |
---|---|
:read-only | Input fields with the readonly attribute |
:read-write | Editable fields (input , textarea , contenteditable ) |
✅ Helps differentiate editable vs. non-editable fields.
4. :read-only
with textarea
The readonly
attribute can also apply to textarea elements.
✅ Prevents user input but allows copying and selecting the text.
5. Browser Support
✅ Fully supported in Chrome, Firefox, Edge, and Safari.
❌ Not supported in Internet Explorer.
6. Best Practices
✔ Use :read-only
to style non-editable fields for better UX.
✔ Combine with :read-write
for clearer visual distinction.
✔ Works great for display-only form elements.