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.
Fundamentals
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.
Foundation
📝 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
Scoping to .signup-form keeps required styling inside the form. Email and password match :required; company name matches :optional.
Tips
💬 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.
Watch Out
⚠️ 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.
A11y
♿ 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.
Compatibility
🖥 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 Chrome10+ · Desktop & Mobile
Full support
Mozilla Firefox4+ · Desktop & Mobile
Full support
Apple Safari5+ · macOS & iOS
Full support
Microsoft Edge12+
Full support
Opera10.1+
Full support
:required pseudo-class99% supported
Bottom line: Safe for all modern form UI. Pair with :optional for complete field state styling.
Wrap Up
🎉 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.
Use these points when styling mandatory form fields.
5
Core concepts
*01
Mandatory
Must fill in.
Purpose
#02
required
HTML attr.
HTML
:opt03
: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.