CSS Reference ยท Pseudo-classes
The :read-only pseudo-class
Reference for the CSS :read-only pseudo-class: match elements the user cannot edit, including readonly and disabled inputs and ordinary text elements.
The :read-only pseudo-class matches every element that the user cannot edit. That includes inputs and textareas with the readonly or disabled attribute, and, perhaps surprisingly, ordinary elements such as <p>, <div> and <h1>, because they are not editable either.
Because it matches so much, you nearly always attach it to a form element: input:read-only.
CSS
input:read-only { background: #f1f5f9; }
Try it
Syntax
CSS
:read-only { /* declarations */ }
- Applies to: all elements; it matches those that are not user-editable.
- Specificity: (0,1,0).
- Inherited / animatable: not applicable.
Values
| Syntax and arguments | Description |
|---|---|
:read-only |
No arguments. Matches any element that does not match :read-write. |
Usage notes
- Scope it. Without an element selector you will style the whole page. Write
input:read-only. - Disabled counts. A
disabledinput is also:read-only. Use:disabledor:not(:disabled)to separate them. Unlike disabled controls, readonly ones are focusable and submitted with the form. - Checkboxes and radios. The
readonlyattribute does not apply to them, and they are never editable text, so in some browsers they match:read-onlyand in others they don't; avoid depending on it. readonlyisn't validated. Read-only inputs are barred from constraint validation, so they match neither:validnor:invalid.- Look consistent. Make read-only fields look different from editable ones, but keep the text readable and copyable.
Browser support
Supported in all current browsers. Very old Firefox needed the :-moz-read-only prefix.
Related
:read-write:disabled:enabled- Lesson: Styling Forms