Skip to main content

How to Style Disabled Inputs with :disabled in CSS

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

:disabled matches form fields that can't be used. input:disabled styles every disabled text box, so people can see at a glance which fields they can't change.

Syntax​

Syntax
<element>:disabled {
<declarations>
}
PartWhat it does
<element>The kind of field to style, such as input. Leave it out to match every disabled element.
:disabledKeeps only the disabled ones. It goes straight after <element>, with no space.
<declarations>The styles the disabled fields get.

MDN's :disabled page says it represents any disabled element: one that can't be selected, clicked on, typed into or focused.

The Example​

The example is a profile form with two text boxes: Name, which people can change, and Employee ID, which is disabled. One rule gives the disabled box a light grey background and a dashed border.

Step 1: Write the Code​

Create a file named index.html in a text editor, such as Visual Studio Code:

index.html, an HTML page with a style element holding a rule for disabled inputs with the background f5f5f5 and a 1-pixel dashed bdbdbd border, and two labelled text boxes, Name with the value Ana and Employee ID with the value E-1024 and the disabled attribute.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>:disabled</title>
<style>
input:disabled {
background: #f5f5f5;
border: 1px dashed #bdbdbd;
}
</style>
</head>
<body>
<p><label>Name <input value="Ana"></label></p>
<p><label>Employee ID <input value="E-1024" disabled></label></p>
</body>
</html>

The disabled attribute disables the Employee ID box, and input:disabled matches it.

Step 2: Open It in a Browser​

Double-click index.html. It opens in your browser:

The page in Chromium: a white Name text box holding Ana, and below it a light grey Employee ID text box with a dashed border, holding E-1024 in grey.

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.

What Happened​

  • Employee ID has a light grey background and a dashed border. Chromium gave it the background rgb(245, 245, 245), which is #f5f5f5, and a dashed border, because it's disabled.
  • Name kept the browser's own white box, because it isn't disabled.

Common Mistakes​

MistakeWhat happensDo this instead
Selecting the attribute: input[disabled]It misses fields disabled another way: a text box inside <fieldset disabled> matched :disabled, but not [disabled]Use :disabled
Giving a field a class named disabledA class doesn't disable anything, and a text box with class="disabled" didn't match :disabledAdd the disabled attribute

Troubleshooting​

SymptomLikely causeFix
A field inside a disabled fieldset isn't styledThe rule uses [disabled]Use :disabled
No field is styledThe fields aren't disabledAdd the disabled attribute to the field, or to the fieldset around the fields

FAQ​

Does it work for fields inside a disabled fieldset?​

Yes. With <fieldset disabled>, the text box inside matched :disabled and got the dashed border, although it had no disabled attribute of its own.

Does it work for buttons and other form fields?​

Yes. MDN's page says :disabled represents any disabled element, so button:disabled matches a disabled button the same way.

Conclusion​

Add disabled to the fields people can't change, and style them with :disabled. It also catches fields disabled by a fieldset, which the [disabled] attribute selector misses.

Additional Resources​