CSS :required Pseudo-Class

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The :required pseudo-class matches any input, select, or textarea that has the HTML required attribute — fields that must be filled before the form can submit.

It marks the field as mandatory, not as valid. An empty required field still matches :required. Pair with :optional and :invalid for fuller form UX.

Baseline — widely available.

Supported across modern browsers since mid-2015. Always set the HTML required attribute for assistive tech — CSS alone is not enough for accessibility.

CSS Syntax

syntax.css
:required { /* styles */ }

Example: input:required { border-color: maroon; }.

What It Matches

:required has no values — it is a state based on the HTML attribute:

TargetNotesTry it
input:requiredText, email, checkbox, and other supported typesTry it
select:requiredDropdowns that must have a choiceTry it
With :optionalContrast mandatory vs optional fieldsTry it
With :invalidEmpty or bad required valuesTry it
Not matchedControls without required; buttonsN/A

How to Use :required

Mark the field in HTML, then style the mandatory state in CSS.

  1. Add required to the input, select, or textarea.
  2. Keep a clear label (and often a “(required)” hint that is not color-only).
  3. Style with input:required (or a scoped form selector).
  4. Optionally add :required:invalid for empty/invalid feedback.
PieceRequired?Role
required attributeYesMakes the control mandatory for submit + AT
:required ruleYesVisual treatment for mandatory fields
Label / text hintRecommendedDo not rely on border color alone
:optional / :invalidOptionalContrast and validation feedback

Live Preview

Toggle the required attribute. The gold highlight applies only while the field matches :required.

Rule shown: input:required

Field

Matches :required — gold background

CSS Examples

Short patterns for mandatory fields. Use Try It Yourself for a live editor.

Example 1 — Required Input

Highlight a mandatory name field.

CSS
<style>
  input:required {
    background: #fef08a;
    border: 1px solid #ca8a04;
  }
</style>

<label>
  Name (required)
  <input type="text" name="name" required>
</label>
Try It Yourself

How It Works

  1. HTML required makes the field mandatory for submit.
  2. input:required matches that attribute state.
  3. Gold background flags the field visually.
  4. Label text also says “required” — not color alone.

Example 2 — Pair with :optional

Contrast mandatory and optional fields in one form.

CSS
<style>
  input:required {
    border-left: 4px solid #b91c1c;
  }
  input:optional {
    border-left: 4px solid #94a3b8;
  }
</style>

<input type="email" required placeholder="Email *">
<input type="text" placeholder="Nickname (optional)">
Try It Yourself

How It Works

  1. Email has required → red left border via :required.
  2. Nickname has no required → matches :optional.
  3. Users see priority at a glance.
  4. Still keep text labels for screen readers and clarity.

Example 3 — Required and Invalid

Stronger border when a required field is empty or invalid.

CSS
<style>
  input:required {
    border: 2px solid #78716c;
  }
  input:required:invalid {
    border-color: #c00000;
  }
</style>

<label>
  Email
  <input type="email" required>
</label>
Try It Yourself

How It Works

  1. :required styles every mandatory field.
  2. :required:invalid tightens the look when empty or bad.
  3. Typing a valid email drops the invalid match (border softens).
  4. Separates “must fill” from “value is wrong.”

Edge Cases & Pitfalls

Most confusion is mixing “required” with “valid.”

not valid

Empty required still matches

:required does not check the value. Use :valid / :invalid for filled vs empty.

a11y

Attribute + text, not color alone

Keep the HTML required attribute and a visible text or icon hint. Color-only borders fail WCAG intent for “required.”

select

Placeholder options

A required select often needs an empty first option (value="") so the browser can treat “no choice” as invalid until the user picks one.

checkbox

Required checkboxes

A required checkbox must be checked to submit. Style carefully so the box stays usable and labeled.

Browser Support

:required is a browser CSS pseudo-class. Node.js, Deno, and Bun do not apply it.

✓ Baseline · Widely available

:required

Widely available across modern browsers since CSS / HTML5 form validation landed (MDN: mid-2015). Works with input, select, and textarea. Internet Explorer 10+ supports the selector with HTML5 forms.

98% Widely available
Google Chrome Supported
Full support
Mozilla Firefox Supported
Full support
Apple Safari Supported
Full support
Microsoft Edge Supported
Full support
Opera Supported
Full support
Samsung Internet Supported
Full support
Internet Explorer 10+
With HTML5 forms
:required Baseline · Widely available

Bottom line: Safe for everyday form styling. Always set the required attribute in HTML. This is not a JavaScript runtime API.

Key Takeaways

  • Role: style controls that have the HTML required attribute.
  • Not validity: empty required fields still match :required.
  • Start simple: input:required { border-color: …; }.
  • A11y: attribute + text hint; pair with :optional / :invalid.

One line: :required paints “must fill” — mark it in HTML, style it in CSS, don’t confuse it with valid.

Frequently Asked Questions

It matches input, select, or textarea elements that have the HTML required attribute — fields the user must complete before submit.
No. It only means the field is mandatory. An empty required field still matches :required. Use :valid / :invalid for filled vs empty validation styling.
:required matches controls with required. :optional matches controls without it. They are natural pairs for form styling.
Form controls that support the required attribute: many input types, select, and textarea. Buttons and non-form elements do not match.
No. It is a browser CSS pseudo-class. Node.js, Deno, and Bun do not apply it.

Next: :root

Learn how to target the document root for CSS custom properties.

:root tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful