The :read-only pseudo-class styles form fields users can view but not edit. It helps you visually distinguish locked values from editable inputs in profiles, order forms, and admin panels.
01
View only
Not editable.
02
readonly
HTML attr.
03
:read-write
Editable pair.
04
input
Text fields.
05
textarea
Multi-line.
06
vs disabled
Still focused.
Fundamentals
Introduction
The CSS :read-only selector targets form elements in a read-only state — fields where users can see the value but cannot change it. This is usually set with the HTML readonly attribute on <input> or <textarea>.
Unlike disabled fields, read-only inputs can still receive focus, allow text selection, and their values are included when the form is submitted. Styling them clearly helps users understand which fields they can edit.
Definition and Usage
Use :read-only on its own or scoped to a field type: input:read-only or textarea:read-only. Pair it with :read-write to style editable fields in the same form.
💡
Beginner Tip
:read-only is not the same as :disabled. Disabled fields cannot be focused and are not submitted. Read-only fields can be focused, copied, and submitted with the form.
Foundation
📝 Syntax
The syntax for the :read-only pseudo-class is:
syntax.css
:read-only{/* CSS properties */}
The :read-only pseudo-class targets form controls that cannot be edited by the user.
The field with readonly gets muted gray styling via :read-only. The editable field matches :read-write with a white background, making the difference obvious.
Example 2 — Order ID with dashed border
Scope to input:read-only and use a dashed border to signal a locked system value.
Setting resize: none on read-only textareas prevents the resize handle from suggesting the field is editable. Editable textareas keep resize: vertical.
Example 4 — Profile edit form
Mix read-only username with editable display name in a profile form.
The username is locked with readonly and styled via :read-only. The display name is editable and gets focus styling through :read-write:focus.
Tips
💬 Usage Tips
Pair with :read-write — Style editable fields distinctly from read-only ones.
Use readonly, not disabled — When values must still be submitted with the form.
Allow copy — Read-only fields let users select and copy text; avoid user-select: none.
Scope selectors — Use input:read-only within form wrappers for targeted rules.
Set via JavaScript — Toggle with element.readOnly = true for dynamic forms.
Visual cues — Gray backgrounds, dashed borders, or monospace fonts signal locked values.
Watch Out
⚠️ Common Pitfalls
Confusing with :disabled — Disabled fields cannot be focused and are not submitted; read-only fields can.
Missing readonly attribute — Without it, the field stays editable and :read-only won’t match.
Styling disabled as read-only — Use :disabled for inactive controls, not :read-only.
Too faint text — Read-only values should remain readable; don’t make contrast too low.
Buttons and selects — :read-only mainly applies to text inputs and textareas.
Security misconception — readonly prevents casual editing but is not server-side protection.
A11y
♿ Accessibility
Keep text readable — Read-only fields should meet WCAG contrast requirements.
Explain locked fields — Use label text like “Order ID (cannot be changed)” for clarity.
Allow focus — Users can tab to read-only fields and copy values; don’t block keyboard access.
Don’t mimic disabled — If a field is read-only, avoid cursor: not-allowed which implies it’s inactive.
aria-readonly — For custom widgets, consider aria-readonly="true" alongside visual styling.
🧠 How :read-only Works
1
readonly attribute set
HTML marks the field with readonly so the browser blocks editing.
HTML
2
:read-only matches
CSS rules targeting :read-only apply muted styling to the field.
CSS
3
User can focus & copy
Unlike disabled fields, read-only inputs can be tabbed to and text can be selected.
UX
=
🔒
Clear view-only feedback
Users know which values are locked and which they can change.
Compatibility
🖥 Browser Compatibility
The :read-only pseudo-class is supported in all modern browsers.
✓ Baseline · Modern browsers
Read-only styling everywhere
:read-only works in Chrome, Firefox, Safari, Edge, and Opera.
99%Global support
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions
Full support
OperaAll versions
Full support
:read-only pseudo-class99% supported
Bottom line: Safe for all modern form UI. Pair with :read-write for editable field contrast.
Wrap Up
🎉 Conclusion
The :read-only selector is a valuable CSS tool for enhancing the visual presentation of non-editable form fields. By styling read-only elements appropriately, you give users clear feedback about which parts of a form they can change and which are view-only.
Pair it with :read-write for editable fields, keep contrast accessible, and remember that readonly is a UX convenience — always validate data on the server.
Use these points when styling view-only form fields.
5
Core concepts
🔒01
View only
Not editable.
Purpose
#02
readonly
HTML attr.
HTML
:rw03
:read-write
Editable pair.
Tip
📝04
Still submitted
Unlike disabled.
Forms
🌐05
99% support
All browsers.
Compat
❓ Frequently Asked Questions
The :read-only pseudo-class matches form elements that users can view but not edit — typically those with the readonly HTML attribute. It lets you style non-editable fields differently from editable ones.
:read-only applies to input and textarea elements with the readonly attribute, and to elements with contenteditable set to false. It does not apply to buttons or selects in the same way.
:read-only fields can still be focused and their values are submitted with the form. :disabled fields cannot be interacted with, are skipped on submit, and usually look more inactive.
Yes. Pair them for clear contrast — :read-only for view-only fields and :read-write for editable fields in the same form.
:read-only is supported in all modern browsers including Chrome, Firefox, Safari, and Edge.