CSS :read-only Selector

Beginner
⏱️ 6 min read
📚 Updated: Jul 2026
🎯 4 Examples
Form States

What You’ll Learn

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.

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.

📝 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.

Basic Example

read-only.css
:read-only {
  background-color: #f1f5f9;
  border: 1px solid #cbd5e1;
  color: #64748b;
  cursor: default;
}

:read-write {
  background-color: #fff;
  border: 2px solid #cbd5e1;
  color: #1e293b;
}
input:read-only textarea:read-only :read-write [readonly]

:read-only vs :disabled

Feature:read-only:disabled
Can focusYesNo
Can select/copy textYesLimited
Submitted with formYesNo
HTML attributereadonlydisabled

Syntax Rules

  • Add the readonly attribute in HTML for the selector to match.
  • Pair with :read-write to style editable fields in the same form.
  • Scope with element selectors: input:read-only avoids affecting other elements.
  • Do not confuse with :disabled — they serve different purposes.
  • Read-only values can be set dynamically with JavaScript: element.readOnly = true.

Related Selectors

  • :read-write — matches editable form controls
  • :disabled — matches inactive, non-interactive controls
  • :enabled — matches controls without the disabled attribute
  • [readonly] — attribute selector alternative
  • :focus — read-only fields can still receive focus

⚡ Quick Reference

QuestionAnswer
Selector typePseudo-class (UI state)
When it appliesField cannot be edited by the user
Required HTMLreadonly attribute
Best paired with:read-write
Common propertiesbackground-color, color, border, cursor
Browser supportAll modern browsers

When to Use :read-only

:read-only is ideal for view-only form fields:

  • Order IDs and reference numbers — Show locked identifiers users should not change.
  • Profile usernames — Display account names that cannot be edited inline.
  • Pre-filled data — Highlight server-provided values in edit forms.
  • Admin review panels — Distinguish locked system fields from user-editable ones.
  • Confirmation screens — Show submitted values in a non-editable summary form.

👀 Live Preview

Compare the gray read-only field with the white editable field below:

Examples Gallery

Practice :read-only with basic pairing, scoped selectors, textarea fields, and mixed forms.

📜 Core Patterns

Style non-editable and editable fields with paired pseudo-classes.

Example 1 — Basic :read-only and :read-write

Give read-only fields a muted gray look and editable fields a bright white background.

read-only-form.html
<style>
  :read-only {
    background: #f1f5f9;
    border: 1px solid #cbd5e1;
    color: #64748b;
  }
  :read-write {
    background: #fff;
    border: 2px solid #cbd5e1;
    color: #1e293b;
  }
  input {
    padding: 0.5rem;
    border-radius: 0.4rem;
    width: 100%;
    margin-bottom: 0.75rem;
  }
</style>

<form>
  <label for="ro">Read-only field:</label>
  <input type="text" id="ro" value="This is read-only" readonly>

  <label for="rw">Editable field:</label>
  <input type="text" id="rw" placeholder="You can edit this">
</form>
Try It Yourself

How It Works

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.

read-only-order.css
input:read-only {
  background: #f8fafc;
  border: 1px dashed #94a3b8;
  color: #475569;
  font-family: ui-monospace, monospace;
  cursor: default;
}
Try It Yourself

How It Works

The dashed border and monospace font communicate that this is a system-generated ID. Users can select and copy it, but cannot change it.

📄 Textareas & Profiles

Apply read-only styling to multi-line fields and profile forms.

Example 3 — Read-only textarea

Style a locked description field in a textarea with reduced opacity.

read-only-textarea.css
textarea:read-only {
  background: #f1f5f9;
  border: 1px solid #e2e8f0;
  color: #64748b;
  resize: none;
  cursor: default;
}

textarea:read-write {
  background: #fff;
  border: 2px solid #cbd5e1;
  resize: vertical;
}
Try It Yourself

How It Works

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.

read-only-profile.css
.profile-form input:read-only {
  background: #f1f5f9;
  border-color: #e2e8f0;
  color: #94a3b8;
}

.profile-form input:read-write {
  background: #fff;
  border: 2px solid #cbd5e1;
  color: #1e293b;
}

.profile-form input:read-write:focus {
  border-color: #3b82f6;
  outline: none;
}
Try It Yourself

How It Works

The username is locked with readonly and styled via :read-only. The display name is editable and gets focus styling through :read-write:focus.

💬 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.

⚠️ 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 misconceptionreadonly prevents casual editing but is not server-side protection.

♿ 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.

🖥 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 Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions
Full support
Opera All versions
Full support
:read-only pseudo-class 99% supported

Bottom line: Safe for all modern form UI. Pair with :read-write for editable field contrast.

🎉 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.

💡 Best Practices

✅ Do

  • Pair :read-only with :read-write
  • Use readonly when values must be submitted
  • Keep read-only text readable and copyable
  • Explain locked fields in label text
  • Scope rules with input:read-only

❌ Don’t

  • Confuse read-only with disabled states
  • Use cursor: not-allowed on read-only fields
  • Rely on readonly for security enforcement
  • Make locked values too faint to read
  • Forget the readonly HTML attribute

Key Takeaways

Knowledge Unlocked

Five things to remember about :read-only

Use these points when styling view-only form fields.

5
Core concepts
# 02

readonly

HTML attr.

HTML
:rw 03

: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.

Practice in the Live Editor

Open the HTML editor and experiment with :read-only, :read-write, and form state styling.

HTML Editor →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful