CSS :required Selector

The :required pseudo class selector in CSS allows authors to select and style any matched element with the required attribute. Forms can easily indicate which fields must have valid data before the form can be submitted, but allows the user to avoid the wait incurred by having the server be the sole validator of the user's input.

The :required CSS pseudo-class represents any <input>, <select>, or <textarea> element that has the required attribute set on it.

The :required selector selects form elements which are required.Form elements with a required attribute are defined as required.

CSS Syntax

:required {css declarations;}

Let's say we have an input with an attribute of type="name" and make it a required input using the required boolean attribute1.

Example -

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