CSS :required Selector

❮ Previous Selectors Next ❯

Example

input:required { 
  background-color: red;
}

Meaning

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.




Status







❮ Previous Selectors Next ❯