CSS3 Selectors: :required & :optional

This entry is part 23 of 40 in the series CSS3 Selectors

We might want to highlight a form field where the users will have to fill up.


Applies to form controls that have the <required> attribute set.


Applies to all form controls that do not have the <required> attribute.

Quick contact form

<!DOCTYPE html>
<html lang='en'>
    <meta charset='utf-8'>
    <title>Quick contact form</title>
        input:required {
            border: 1px solid red;
        input:optional {
            border: 1px solid blue;
        <legend>Contact information</legend>
                <label for='email'>Email:</label>
                <input required type='email' id='email' name='email'/>
                <label for='tel'>Telephone number:</label>
                <input type='tel' id='tel' name='tel'/>
                <label for='url'>Website:</label>
                <input type='url' id='url' name='url'/>
        <input type='submit' value='Submit this' />

Series Navigation<< CSS3 Selectors: Pseudo class :checked
CSS3 Selectors: Structural pseudo-class :first-child >>

Leave a comment

Leave a Reply