Style elements with a "readonly" attribute with CSS

The CSS :read-only pseudo-class selector is used to style form elements that have the readonly attribute. This selector targets input fields and textareas that users cannot modify.

Syntax

input:read-only {
    /* CSS properties */
}

textarea:read-only {
    /* CSS properties */
}

/* Target all read-only elements */
:read-only {
    /* CSS properties */
}

Example: Styling Read-Only Input Fields

The following example demonstrates how to apply different styles to read-only and editable input fields −

<!DOCTYPE html>
<html>
<head>
<style>
    input {
        padding: 10px;
        margin: 5px;
        border: 2px solid #ccc;
        border-radius: 5px;
        font-size: 14px;
    }
    
    input:read-only {
        background-color: #f0f0f0;
        border-color: #999;
        color: #666;
        cursor: not-allowed;
    }
    
    input:not(:read-only) {
        background-color: white;
        border-color: #4CAF50;
    }
</style>
</head>
<body>
    <form>
        <label>Editable Field:</label><br>
        <input type="text" name="subject" value="Mathematics" placeholder="You can edit this"><br><br>
        
        <label>Read-Only Field:</label><br>
        <input type="text" name="student" readonly value="John Doe"><br><br>
        
        <label>Read-Only Email:</label><br>
        <input type="email" readonly value="john@example.com">
    </form>
</body>
</html>
Three input fields appear: the first is editable with white background and green border, while the second and third are read-only with gray background, darker border, and a "not-allowed" cursor when hovered.

Example: Styling Read-Only Textarea

You can also apply the :read-only selector to textarea elements −

<!DOCTYPE html>
<html>
<head>
<style>
    textarea {
        width: 300px;
        height: 100px;
        padding: 10px;
        border: 2px solid #ccc;
        border-radius: 5px;
        font-family: Arial, sans-serif;
        resize: vertical;
    }
    
    textarea:read-only {
        background-color: #e8f4f8;
        border-color: #2196F3;
        color: #1976D2;
        font-style: italic;
    }
</style>
</head>
<body>
    <form>
        <label>Editable Comments:</label><br>
        <textarea placeholder="You can type here..."></textarea><br><br>
        
        <label>Read-Only Terms:</label><br>
        <textarea readonly>These terms and conditions cannot be modified. Please read carefully before proceeding.</textarea>
    </form>
</body>
</html>
Two textarea elements appear: the first is editable with default styling, while the second is read-only with light blue background, blue border, and italic text styling.

Conclusion

The :read-only pseudo-class is essential for creating visually distinct read-only form elements. It helps users immediately identify which fields they can and cannot modify, improving overall user experience.

Updated on: 2026-03-15T12:23:48+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements