CSS :required Selector

Beginner
⏱️ 6 min read
📚 Updated: Jul 2026
🎯 4 Examples
Form States

What You’ll Learn

The :required pseudo-class targets form fields that must be filled in — those with the HTML required attribute. It helps users spot mandatory fields at a glance.

01

Mandatory

Must fill in.

02

required

HTML attr.

03

:optional

Opposite pair.

04

input

Email, text.

05

select

Dropdowns.

06

Form UX

Visual cues.

Introduction

The CSS :required selector targets form elements marked with the required attribute. These are fields users must complete before the browser allows form submission.

It is especially useful for highlighting mandatory inputs, making forms easier to understand and reducing submission errors.

Definition and Usage

Use :required on its own or scoped to a control type: input:required, select:required, or textarea:required. Pair it with :optional to style non-mandatory fields differently.

💡
Beginner Tip

:required indicates the field is mandatory, not that it already has a valid value. An empty required email field still matches :required. Use :invalid for empty-or-wrong styling after interaction.

📝 Syntax

The syntax for the :required pseudo-class is:

syntax.css
:required {
  /* CSS properties */
}

:required applies to form elements with the required attribute:

  • <input> — text, email, number, tel, url, and other types
  • <select> — dropdown menus
  • <textarea> — multi-line text areas

Basic Example

required.css
:required {
  border: 2px solid #dc2626;
  background-color: #fef2f2;
}

:optional {
  border: 2px solid #cbd5e1;
  background-color: #fff;
}
input:required select:required textarea:required :optional

:required vs :optional

SelectorMatchesHTML
:requiredField has required attributerequired present
:optionalField lacks requiredNo required (default)
:invalidValue fails validationEmpty required, bad email, etc.

Syntax Rules

  • Add required in HTML for the selector to match.
  • Pair with :optional for complete required/optional form styling.
  • Scope with element selectors: input:required avoids affecting unrelated elements.
  • Do not confuse with :invalid — required fields can be valid or invalid.
  • Always include visible labels; don’t rely on color alone for mandatory cues.

Related Selectors

  • :optional — matches fields without the required attribute
  • :invalid — matches fields that fail validation
  • :valid — matches fields that pass validation
  • [required] — attribute selector alternative
  • :disabled — matches inactive form controls

⚡ Quick Reference

QuestionAnswer
Selector typePseudo-class (UI state)
When it appliesField has the required attribute
Default stateFields are optional unless required is set
Best paired with:optional
Common propertiesborder, background-color, border-left
Browser supportAll modern browsers

When to Use :required

:required is ideal for highlighting mandatory form fields:

  • Signup forms — Mark email and password as required with distinct styling.
  • Checkout flows — Highlight shipping address and payment fields.
  • Contact forms — Show which fields must be completed before submit.
  • Surveys — Distinguish mandatory questions from optional ones.
  • Admin panels — Visually separate required configuration fields.

👀 Live Preview

The email field has required (red styling). The phone field is optional (neutral border):

Examples Gallery

Practice :required with basic pairing, left-border accents, select dropdowns, and registration forms.

📜 Core Patterns

Highlight mandatory fields and contrast them with optional ones.

Example 1 — Basic :required and :optional

Style required email red and optional phone with a neutral border.

required-form.html
<style>
  :required {
    border: 2px solid #dc2626;
    background: #fef2f2;
  }
  :optional {
    border: 2px solid #cbd5e1;
    background: #fff;
  }
  input {
    padding: 0.5rem;
    border-radius: 0.4rem;
    width: 100%;
    margin-bottom: 0.75rem;
  }
</style>

<form>
  <label for="email">Email (required):</label>
  <input type="email" id="email" required>

  <label for="phone">Phone (optional):</label>
  <input type="tel" id="phone">
</form>
Try It Yourself

How It Works

The email input with required matches :required and gets red styling. The phone field without required matches :optional instead.

Example 2 — Left border accent on required fields

Use a thick left border instead of full red backgrounds for a subtler mandatory cue.

required-border.css
input:required,
select:required,
textarea:required {
  border-left: 4px solid #dc2626;
  padding-left: 0.65rem;
  background: #fff;
  border-top: 1px solid #e2e8f0;
  border-right: 1px solid #e2e8f0;
  border-bottom: 1px solid #e2e8f0;
}
Try It Yourself

How It Works

A red left border signals mandatory status without overwhelming the whole field. This pattern works well alongside asterisks in label text.

📄 Selects & Signup

Style required dropdowns and complete registration forms.

Example 3 — Required select dropdown

Apply select:required to highlight mandatory dropdown menus.

required-select.css
select:required {
  border: 2px solid #dc2626;
  background: #fef2f2;
  color: #1e293b;
}

select:optional {
  border: 2px solid #cbd5e1;
  background: #fff;
}
Try It Yourself

How It Works

select elements support the required attribute. A required dropdown with no selection will also match :invalid on submit attempt.

Example 4 — Signup form with required fields

Style all required inputs, selects, and textareas in a registration form.

required-signup.css
.signup-form input:required,
.signup-form select:required,
.signup-form textarea:required {
  border-color: #dc2626;
  background: #fffafa;
}

.signup-form input:optional,
.signup-form textarea:optional {
  border-color: #e2e8f0;
  background: #fff;
}
Try It Yourself

How It Works

Scoping to .signup-form keeps required styling inside the form. Email and password match :required; company name matches :optional.

💬 Usage Tips

  • Pair with :optional — Style both states so users see required vs skippable fields.
  • Use labels + asterisks — Combine CSS with “Email *” in label text for accessibility.
  • Subtle accents work — Left borders or light backgrounds can be gentler than full red fields.
  • Scope selectors — Use input:required within form wrappers.
  • Combine with :invalid — Use :required:invalid for empty mandatory fields after submit.
  • Test input types — Verify styling on email, text, select, and textarea consistently.

⚠️ Common Pitfalls

  • Confusing with :invalid — Required fields are not automatically invalid; empty ones become invalid on submit.
  • Missing required attribute — Without it in HTML, :required won’t match.
  • Color-only cues — Don’t rely on red borders alone; use labels and asterisks too.
  • Wrong elements — Buttons and divs don’t support :required meaningfully.
  • Over-aggressive red — Full red backgrounds can feel alarming; consider subtler accents.
  • CSS-only validation — Always validate on the server as well.

♿ Accessibility

  • Visible labels required — Every field needs a <label>; CSS cannot replace that.
  • Don’t use color alone — Pair red styling with “(required)” or asterisks in labels.
  • aria-required — The required attribute sets this automatically on supported elements.
  • Contrast — Ensure required field borders and text meet WCAG contrast ratios.
  • Error messages — Provide text feedback when required fields are empty on submit.

🧠 How :required Works

1

required attribute set

HTML marks the field with required so the browser enforces it on submit.

HTML
2

:required matches

CSS rules targeting :required apply mandatory field styling.

CSS
3

User sees visual cue

Red borders or accents signal which fields must be completed.

UX
=

Clearer form completion

Users know which fields they cannot skip before submitting.

🖥 Browser Compatibility

The :required pseudo-class is supported in all modern browsers.

Baseline · Modern browsers

Required field styling everywhere

:required works in Chrome, Firefox, Safari, Edge, and Opera.

99% Global support
Google Chrome 10+ · Desktop & Mobile
Full support
Mozilla Firefox 4+ · Desktop & Mobile
Full support
Apple Safari 5+ · macOS & iOS
Full support
Microsoft Edge 12+
Full support
Opera 10.1+
Full support
:required pseudo-class 99% supported

Bottom line: Safe for all modern form UI. Pair with :optional for complete field state styling.

🎉 Conclusion

The CSS :required selector is an essential tool for improving form user experience by visually indicating which fields must be filled out. By applying distinct styles to mandatory fields, you make forms easier to understand and interact with.

Pair it with :optional, use clear labels with asterisks, and combine with :invalid for validation feedback. Always validate on the server too.

💡 Best Practices

✅ Do

  • Pair :required with :optional
  • Use labels with asterisks or “(required)”
  • Consider subtle left-border accents
  • Scope rules within form wrappers
  • Validate on the server as well

❌ Don’t

  • Confuse :required with :invalid
  • Rely on red color alone for mandatory cues
  • Style every field red aggressively
  • Forget the required HTML attribute
  • Skip visible field labels

Key Takeaways

Knowledge Unlocked

Five things to remember about :required

Use these points when styling mandatory form fields.

5
Core concepts
# 02

required

HTML attr.

HTML
:opt 03

:optional

Opposite pair.

Tip
! 04

Not :invalid

State vs value.

Note
🌐 05

99% support

All browsers.

Compat

❓ Frequently Asked Questions

The :required pseudo-class matches form elements that have the required HTML attribute — fields users must fill in before the form can be submitted.
:required applies to form controls that support the required attribute: input, select, and textarea. It does not match buttons or elements without the required attribute.
:required matches fields with the required attribute set. :optional matches the opposite — fields without required. They are natural pairs for form styling.
No. :required only checks whether the field is mandatory. An empty required field still matches :required. Use :valid or :invalid for value-based validation styling.
Yes. All modern browsers support :required alongside HTML5 form validation. It works reliably with :optional, :valid, and :invalid.

Practice in the Live Editor

Open the HTML editor and experiment with :required, :optional, and form validation styling.

HTML Editor →

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