In this lesson, you gave the work-day sign-up form a red border on any field that’s wrong, in the site’s own style, and made it wait until the visitor has had a go before it shows.
Key Points
- The form already refuses bad input, but the browser only says so when someone clicks Sign up, and then its bubble points at one field at a time. A style on the field itself can say more, right in the page.
:invalidis a pseudo-class, like the:hoverand:focus-visiblethe nav links already use. It matches a field whose value fails its checks. The lesson added an--errortoken, a dark red, to thetokenslayer, and aninput:invalidrule at the end of thecomponentslayer.- Red already: on a fresh page, Your name and Email have red borders before Mei has touched a thing. An empty
requiredfield fails its check from the moment the page loads, and:invaliddescribes the value right now. - CSS matches states, not events. There’s no code that runs when someone gets a field wrong. A selector matches whatever state the field is in at this moment.
:user-invalidmatches the same invalid values, but only once the visitor has changed the field and left it, or tried to submit. The browser keeps track of that as part of the field’s state, so CSS is still matching a state. Changing:invalidto:user-invalidmakes the page load calm.- Tabbing through a field doesn’t count. Typing
mei.lininto Email changes nothing until Mei presses Tab to leave it, and then the border turns red. Once a field counts as touched, the rule follows its value as she types, and it stops matching as soon as the address is valid. While the field has focus, Chrome’s focus ring is drawn over the border. - Trying to submit counts too: clicking Sign up on an empty form turns every empty required field red at once, and the browser’s bubble still points at the first one.
- A red border is only a start. Some visitors can’t tell the color, and a border doesn’t say what’s wrong. MDN’s
:user-invalidpage has more on when it matches.
Final Code
In style.css, the tokens layer gains one custom property after --free: