Skip to content
Tutorials Center
My learning

Topics

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

Show which fields are locked

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 disabled input is also :read-only. Use :disabled or :not(:disabled) to separate them. Unlike disabled controls, readonly ones are focusable and submitted with the form.
  • Checkboxes and radios. The readonly attribute does not apply to them, and they are never editable text, so in some browsers they match :read-only and in others they don't; avoid depending on it.
  • readonly isn't validated. Read-only inputs are barred from constraint validation, so they match neither :valid nor :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.