:required
Example
input:required::after {
content: ' *';
color: red;
}label:has(+ input:required)::after {
content: ' *';
color: #c00;
}Inputs can't have ::after content, so the asterisk goes on the label next to the required field.
Common mistakes
- Adding an asterisk with
::afteron the input, where it never shows. - Marking fields required only visually, so the browser and screen readers don't know.
Key points
- Required fields have the
requiredattribute, not only a CSS asterisk or class. - The required state is also visible in the label text, not just from color or
::aftercontent. - Code does not expect
:requiredto match custom controls without therequiredattribute.
Interview questions
Which elements can match :required?
input, select and textarea with the required attribute.
Do I still need aria-required if I use required?
No. The native required attribute is already announced, so aria-required is only for custom controls.