Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Pseudo-classes

The :required pseudo-class

Reference for the CSS :required pseudo-class: mark input, select and textarea elements with the required attribute, add asterisks and avoid common mistakes.

The :required pseudo-class matches <input>, <select> and <textarea> elements that have the required attribute. The user must fill them in before the form can be submitted.

Use it to flag mandatory fields visually, for example with a coloured edge or an asterisk, without adding a class to every field.

CSS
input:required {
  border-left: 4px solid #dc2626;
}

Try it

Mark required fields

Syntax

CSS
:required { /* declarations */ }
  • Applies to: <input>, <select> and <textarea> that have the required attribute (and a type that supports it).
  • Specificity: (0,1,0).
  • Inherited / animatable: not applicable.

Values

Syntax and arguments Description
:required No arguments. Matches a control where the required attribute applies and is present.

Usage notes

  • Type matters. Some input types ignore required: hidden, range, color, buttons and others always have a value. They never match :required.
  • Labels are separate. The label is a different element, so to add an asterisk to it use :has(:required) on the wrapper, as in the example, or put the label after the input and use +.
  • Don't rely on colour alone. Add a text or symbol cue ("required", *) and the required attribute itself, which assistive technology announces.
  • Required is not invalid. An empty required field is :invalid from page load, which is why red-on-load styles feel hostile. See :user-invalid.
  • Radio groups. Putting required on any radio of a named group makes the group required.
  • Complement. :optional matches the rest.

Browser support

Supported in every browser.