Last updated on

The :disabled pseudo-class selector provides conditional styling to HTML elements that can receive user input, when the elements have the disabled attribute. It is defined in the CSS Selectors Level 3 spec as a “UI element state pseudo-class”, meaning it is used to style content based on the user’s interaction with an input element.

Elements that can receive the disabled attribute include <button>, <input>,<textarea>, <optgroup>, <option> and <fieldset>. There are two valid syntaxes for setting this attribute: either disabled=”disabled” or (in HTML5) simply thedisabled Boolean keyword. An element is disabled if it can’t be activated (e.g. selected, clicked on or accept text input) or accept focus.

Such an element can be styled using the :disabled pseudo-class selector:

<form action="#">
    <label for="name">Enabled Input:</label>
    <input type="text" autofocus>
      <hr>
    <label for="name">Disabled Input:</label>
    <input type="text" disabled>
</form>
/* The good stuff starts here :)
*/

  input {
    width: 60%;
    margin: 0;
    border: none;
    outline: 1px solid lightgrey;
    outline-offset: 2px;
  }

input:disabled {
  background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAQAAAAECAYAAACp8Z5+AAAAIklEQVQIW2NkQAKrVq36zwjjgzhhYWGMYAEYB8RmROaABADeOQ8CXl/xfgAAAABJRU5ErkJggg==) repeat;
}

Points of Interest

  • :disabled can be “chained” with other pseudo-selectors: like :before or :after to generate icons or text to provide more user feedback.
  • Any property that can be animated with CSS Transitions or Animations will be properly animated when the disabled attribute is added or removed from the HTML element.

Browser Support

Chrome Safari Firefox Opera IE Android iOS
All 3.1 All 9 9 All All

Leave a Reply

Your email address will not be published. Required fields are marked *