The :Required Pseudo-Class

The :required selector selects form elements which are required. Form elements with a required attribute are defined as required. Note: The :required selector only applies to the form elements: input, select and textarea. This demonstration shows the selector selecting and styling only if the <input> element has a "required" attribute:

HTML file: Displayed by browser:
<style>
input:required {
    background-color: yellow;
}
</style>
<body>
<h3>A demonstration of the :required selector.</h3>
<p>An optional input element:<br /><input></p>
<p>A required input element:<br /><input required></p>
<p>The :required selector selects form elements with a "required" attribute.</p>
</body>

A demonstration of the :required selector.

An optional input element:

A required input element:

The :required selector selects form elements with a "required" attribute.

Use the :optional selector to select form elements which are optional. Note: The :required selector is not supported in Internet Explorer 9 or earlier versions.

Back button Table of Contents Next button