CSS Portal

:required CSS Pseudo Class

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
:required
CSS Pseudo ClassBrowser support for this pseudo class varies across browsers.
Updated 31st December 2025

Description

The :required pseudo-class in CSS is used to select form elements that are required for user input. It specifically targets elements with the required attribute in HTML forms, allowing developers to style them differently from optional fields. This can be particularly useful for providing visual cues to users about which inputs must be completed before submitting a form. Commonly used elements with :required include input, select, and textarea elements.

When combined with other pseudo-classes like :invalid, :required allows for precise control over form validation styling. For example, you might highlight required fields that the user has not yet filled in red, while optional fields remain neutral.

This pseudo-class does not affect the behavior of the form itself - it is purely for styling purposes. Its effect is applied dynamically, meaning that if an element has its required attribute added or removed via JavaScript, the styling updates automatically without needing to refresh the page.

Example usage:

<form>
  <label for="username">Username (required):</label>
  <input type="text" id="username" name="username" required>

  <label for="nickname">Nickname (optional):</label>
  <input type="text" id="nickname" name="nickname">

  <button type="submit">Submit</button>
</form>
input:required {
  border: 2px solid red;
  background-color: #ffe6e6;
}

input:optional {
  border: 2px solid #ccc;
  background-color: #f9f9f9;
}

In this example, the username field is visually distinguished as required using a red border and light red background, whereas the optional nickname field uses neutral styling.

The :required pseudo-class is particularly helpful in improving form usability and accessibility, as it clearly communicates mandatory fields to users without requiring additional scripting. It works in all modern browsers and can be combined with other selectors and pseudo-classes for complex styling scenarios.

Syntax

:required {
  /* ... */
}

Example

<form>
<div class="form-group">
<label for="name">Name (Required):</label>
<input type="text" id="name" name="name" required>
</div>

<div class="form-group">
<label for="nickname">Nickname (Optional):</label>
<input type="text" id="nickname" name="nickname">
</div>

<button type="submit">Submit</button>
</form>
/* Style only the inputs that have the 'required' attribute */
input:required {
border: 2px solid #ff7f50; /* Coral border */
background-color: #fff5f2;
}

/* Optional: Style the input when it's required AND has valid text */
input:required:valid {
border: 2px solid #2ecc71; /* Green border */
background-color: #f0fff4;
}

/* Basic styling for layout */
.form-group {
margin-bottom: 15px;
font-family: sans-serif;
}

label {
display: block;
margin-bottom: 5px;
}

input {
padding: 8px;
border-radius: 4px;
border: 1px solid #ccc;
outline: none;
}
Open in editor

Browser Support

The following information will show you the current browser support for the CSS :required pseudo class. Hover over a browser icon to see the version that first introduced support for this CSS pseudo class.

Browser support for this pseudo class varies across browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee