CSS Portal

:read-only CSS Pseudo Class

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!

Description

The :read-only pseudo-class in CSS is used to select elements that are not editable by the user. It specifically targets elements that have been rendered in a state where the user cannot modify their value, making it particularly useful for form elements such as input, textarea, or contenteditable elements that have been set to read-only. This pseudo-class allows developers to apply specific styles to these non-editable elements, enhancing visual feedback and improving user experience.

When applied, :read-only can be used to differentiate between elements that can be edited and those that cannot. For example, you might want to style read-only input fields with a different background color or border style to signal that they are not interactive. This pseudo-class complements :read-write, which targets elements that can be edited by the user.

It’s important to note that :read-only does not require the element to explicitly have the readonly attribute, although many browsers use the presence of this attribute to determine read-only status. Elements with disabled attributes, however, are generally not considered read-only; they are excluded from this pseudo-class. Additionally, :read-only respects the natural read-only behavior of certain form elements, such as file inputs and buttons, which are inherently non-editable.

Here’s a practical example of how :read-only can be used:

<form>
  <label for="username">Username (read-only):</label>
  <input type="text" id="username" value="ReneSpronk" readonly>
  
  <label for="email">Email (editable):</label>
  <input type="email" id="email" value="user@example.com">
</form>
input:read-only {
  background-color: #f0f0f0;
  border: 1px solid #ccc;
  color: #666;
  cursor: not-allowed;
}

input:read-write {
  background-color: #fff;
  border: 1px solid #888;
  color: #000;
}

In this example, the read-only input field appears grayed out and visually signals to the user that it cannot be edited, while editable fields remain interactive. Using :read-only can also improve accessibility by providing visual cues that distinguish editable content from non-editable content, making forms easier to navigate.

Syntax

:read-only {
  /* ... */
}

Example

<form>
<div class="field-group">
<label for="name">Editable Name:</label>
<input type="text" id="name" value="John Doe">
</div>

<div class="field-group">
<label for="id">Employee ID (Read-Only):</label>
<input type="text" id="id" value="12345" readonly>
</div>

<div class="field-group">
<label for="bio">Biography:</label>
<textarea id="bio" readonly>This bio is locked and cannot be changed.</textarea>
</div>
</form>
/* Styles applied to any input that is NOT editable */
input:read-only,
textarea:read-only {
background-color: #e9e9e9;
border: 1px dashed #999;
color: #666;
cursor: not-allowed;
}

/* For contrast, styles for editable fields */
input:read-write {
border: 2px solid #4A90E2;
background-color: #fff;
}

/* General spacing for the demo */
.field-group {
margin-bottom: 20px;
font-family: sans-serif;
}

label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}

Browser Support

The following information will show you the current browser support for the CSS :read-only pseudo class. Hover over a browser icon to see the version that first introduced support for this CSS psuedo class.

This psuedo class is supported by all modern browsers.
Desktop
Chrome
Edge
Firefox
Opera
Safari
Tablets & Mobile
Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-

Last updated by CSSPortal on: 31st December 2025

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!