css · level 3 · unit 7 · lesson 3
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
Never hide focus: give it a clear ring from your tokens.
Many people use a keyboard, not a mouse: Tab moves between links and buttons. The shows where they are. The worst line in CSS is outline: none with nothing to replace it, because it leaves them lost.
Style the ring on :focus-visible, so it shows for keyboards but not for mouse clicks. Use outline, which takes up no space and never shifts the layout, plus for a small gap from the edge.
Make the ring colour a token, like --focus, so every component shares one ring and each theme can tune it.
<button class="btn">Save</button>
<a href="https://example.com/help">Help</a> :root {
--focus: #1d4ed8;
}
:is(a, button):focus-visible {
outline: 3px solid var(--focus);
outline-offset: 2px;
} Press show my page to see it here.
Click in the preview, then press Tab to watch the ring move from the button to the link.
guess first
The outline is removed. What does a keyboard user see?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
<a class="link" href="https://example.com/more">Read more</a> .link {
color: #1d4ed8;
}
.link:focus {
outline: none;
}
your turn
Your turn. Give the button a focus ring.
Fill in the blanks: a 2px gap between the button and its ring, and a 3px solid ring in the --focus colour.
the page you’re styling
<button class="btn">Add to basket</button>your css
Press show my page to see it here.
- the ring sits 2px from the edgenot checked yet
- the ring is 3px solid in
--focusnot checked yet
fix it
Keyboard users get lost in this menu. Can you fix it?
the page you’re styling
<nav class="menu">
<a href="https://example.com/">Home</a>
<a href="https://example.com/shop">Shop</a>
</nav>your css
Press show my page to see it here.
- the ring uses the
--focustokennot checked yet - nothing removes the outlinenot checked yet
- Click in the preview and press Tab. Can you tell which link you are on?
outline: nonehides the ring, and nothing replaces it.- Change it to
outline: 3px solid var(--focus);, then press run.
nailed it
you made every focus clear to see. :D
- Never remove the outline without a replacement.
- Draw the ring on
:focus-visiblewithoutlineandoutline-offset. - Keep the ring colour in a token.
new words in your collection
- focus ring
- outline-offset
This lesson isn’t saved. A free account remembers every lesson you finish.