Sub topic 8
Styling form state: :checked, :disabled, :required, :invalid, :placeholder-shown
This chapter assumes you know CSS rule syntax, attribute selectors, and the adjacent sibling combinator (+). You should also know the difference between a pseudo-class and a pseudo-element from earlier in this tutorial.
State pseudo-classes for form controls
Browsers track the state of every form control. State pseudo-classes let you style a control based on that state, with no JavaScript and no extra classes. These are the five you will use most often:
| Pseudo-class | Matches when |
|---|---|
| `:checked` | A checkbox, radio button, or `<option>` is selected |
| `:disabled` | A control has the `disabled` attribute |
| `:required` | A control has the `required` attribute |
| `:invalid` | The control's value fails its validation rules |
| `:placeholder-shown` | The placeholder text is currently visible |
The browser decides what counts as invalid. It uses attributes such as required, type="email", pattern, min, and maxlength.
Styling checked and disabled controls
:checked is often paired with the adjacent sibling combinator. If the label comes right after the input in the HTML, you can style the label from the input's state. :disabled dims controls that the user cannot use.
input:disabled {
opacity: 0.5;
}
input:checked + label {
font-weight: 600;
}This needs the label to directly follow the input:
<input type="checkbox" id="news">
<label for="news">Send me the newsletter</label>Validation without noisy errors
A field with required is invalid when it is empty. If you style :invalid on its own, every required field shows an error as soon as the page loads, before the user has typed anything.
:placeholder-shown fixes this. It matches only while the placeholder is visible, meaning the field is empty. Adding :not(:placeholder-shown) limits the error style to fields that have content.
input:required:invalid:not(:placeholder-shown) {
border-color: #c00;
}<input type="email" required placeholder="[email protected]">The rule has four conditions, and all of them must hold:
- The input has
required. - Its value is invalid.
- The placeholder is not showing, so the field has content.
- The element is an
input.
How the states combine
The diagram shows how the browser state of one email field leads to its final style.
Common errors
- Styling
:invalidalone. Required fields look broken on page load. Pair it with:not(:placeholder-shown). - Putting the label before the input. The
+combinator only looks forward, soinput:checked + labelfails if the label comes first. - Hiding the native checkbox without a replacement. If you set
display: noneon the input, keyboard users cannot reach it. Hide it visually instead, and keep a visible focus style on the label. - Relying on color alone. A red border does not help users who cannot tell red from other colors. Add text, an icon, or
aria-describedbywith an error message.
Exercise
A form has <input type="text" required> with no placeholder attribute, styled with input:required:invalid:not(:placeholder-shown) { border-color: #c00; }. The page loads and the field is empty. Does the field have a red border, and why?
Show answer
Yes. Without a placeholder attribute, :placeholder-shown never matches, so :not(:placeholder-shown) always matches. The empty required field is invalid, so all three conditions hold and the border turns red immediately. Adding a placeholder attribute, even placeholder=" ", fixes it.
Exercise
Write a rule that makes a label bold and green (#2a7d2e) when the checkbox directly before it is checked.
Show answer
input:checked + label { font-weight: bold; color: #2a7d2e; }