CSS pseudo-classPseudo-classes

:disabled

Matches disabled form elements, meaning ones that can’t be clicked, checked, or edited.

On this page

Example

#
CSS
.btn {
  background: #7c3aed;
  transition: opacity 0.2s;
}

.btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}
Preview

Check the box. The script removes the disabled attribute from the button, and the CSS immediately stops graying it out.

Definition and usage

#

The :disabled pseudo-class selects controls with the disabled attribute: buttons, input fields, select lists, textarea fields, as well as option, optgroup, and fieldset. A disabled element doesn’t respond to clicks, doesn’t receive focus, and isn’t submitted with the form.

Disabling also passes through <fieldset disabled>: every control inside then matches :disabled, except those in the first legend. The opposite of :disabled is :enabled, which matches active controls.

Browsers gray out disabled controls by default, but with a custom look you have to handle that yourself. The typical combo is reduced opacity and a not-allowed cursor. Keep in mind that :disabled doesn’t work for links or for elements marked only with aria-disabled="true"; for those, use an attribute selector.

Syntax

#
Syntax
selector:disabled { property: value; }

Common uses

#
  • :disabled

    Disabled controls are dimmed, and the cursor over them shows a ban sign. Hover to check.
  • button:enabled:hover

    A hover effect only for active buttons, thanks to the opposite pseudo-class :enabled. The disabled one doesn’t react.
  • input:disabled

    A dashed border and dimmed text make it clear right away that a disabled field can’t be edited.

Common uses in practice

#

:disabled

Disabled controls are dimmed, and the cursor over them shows a ban sign. Hover to check.
:disabled { opacity: 0.45; cursor: not-allowed; }

button:enabled:hover

A hover effect only for active buttons, thanks to the opposite pseudo-class :enabled. The disabled one doesn’t react.
button:enabled:hover { background: #5b21b6; }

input:disabled

A dashed border and dimmed text make it clear right away that a disabled field can’t be edited.
input:disabled { border-style: dashed; color: #71717a; }

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • A disabled button doesn’t say why it’s disabled. Add a short hint next to it, or leave the button enabled and show a message after the click.
  • Don’t overdo the transparency: a control should look inactive, but its text still has to be readable.
  • You can disable a whole form section with a single attribute on <fieldset disabled>. Every field inside immediately matches :disabled.

Related entries

#

Spotted a mistake or missing an example? Write to us.