States, Focus, and Honest Feedback

What this is. Interactive elements have states, and CSS can target each one.

:hover is the pointer being over it.

:focus-visible is keyboard focus, and it is the important one.

:disabled is unavailable.

:invalid is failing validation.

::placeholder styles the hint text inside a field.

✦ Never remove the focus outline

outline: none is the most damaging one-line change in front-end development. Someone navigating by keyboard loses all sense of where they are on the page, which makes the site unusable rather than merely awkward. If the default ring does not suit your design, replace it with something of your own that has real contrast. Use :focus-visible so the ring appears for keyboard users without appearing on every mouse click.

LIVE PREVIEW
loading editor…

Your turn → Tab through the two buttons and feel the difference. Then type letters with no at sign into the email field and watch the border change, but only after you have typed, because of the :not(:placeholder-shown) guard.

Validate kindly. Marking a field red before someone has finished typing is hostile. The :not(:placeholder-shown) guard above waits until there is actually content to judge. And color alone is never enough, because a red border is invisible to many color blind users. Pair it with text that says what is wrong.

◈ Graded Build

Build the contact form for your portfolio. It needs three fields, each with a label properly connected to its input by for and id: a text field for name, an email field, and a textarea for the message. The email field must be required, and every focusable control must keep a visible focus style.

Checked against the page you actually render, not against how you wrote it. 4 visible and 3 hidden checks.

loading editor…

Mark this lesson complete

Signed in, your progress follows you to every device.

Next: Practice Test →
← Back