CSS :required Selector

❮ Previous Selectors Next ❯


input:required { 
  background-color: red;


The :required selector selects <input> , <select>, or <textarea> element that has the required attribute set on it.

Use the :optional selector to select form elements which are optional.

This pseudo-class is useful for highlighting fields that must have valid data before a form can be submitted.

Note: The :optional pseudo-class selects optional form fields.

Mandatory <input> should have the required attribute applied to them.

This will ensure that people navigating with the aid of assistive technology such as a screen reader will be able to understand which inputs need valid content to ensure a successful submission.

Version: CSS3

Standard Syntax

:required {
  css declarations;

Browser Support

The numbers in the table specify the first browser version that fully supports the property.


❮ Previous Selectors Next ❯