css · level 2 · unit 6 · lesson 3
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
shows keyboard users where they are.
Many people move round a page with the Tab key instead of a mouse. The element that key presses go to has , and :focus picks it, whatever put it there.
:focus-visible is smarter. It picks the focused element only when a ring would help, such as after a Tab press, and not after every mouse click.
Style it with an : a line drawn outside the border that takes up no space. Never remove a focus ring without putting a better one in its place.
<button>Save</button>
<button>Share</button> button {
padding: 10px 16px;
}
button:focus-visible {
outline: 3px solid darkorange;
outline-offset: 2px;
} Press show my page to see it here.
outline-offset adds a gap between the ring and the button. Click the preview, then press Tab.
guess first
Click the button with a mouse. Does the ring show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
<button>Play</button> button:focus-visible {
outline: 3px solid blue;
}
your turn
Your turn. Draw a focus ring.
Give links a ring that shows for keyboard users. Use outline with a width from 2px to 4px, solid, and any colour.
the page you’re styling
<a href="https://example.com/news">News</a>
<a href="https://example.com/sport">Sport</a>
<a href="https://example.com/weather">Weather</a>your css
Press show my page to see it here.
- links get a 2px to 4px ringnot checked yet
fix it
Keyboard users cannot see where they are. Fix it!
the page you’re styling
<button>Buy tickets</button>
<button>See dates</button>your css
Press show my page to see it here.
- nothing removes the focus ringnot checked yet
- a ring shows for keyboard usersnot checked yet
- One rule hides something keyboard users need. Which one?
outline: noneremoves the focus ring for everybody.- Change the rule to
button:focus-visiblewithoutline: 3px solid navy;, then press run.
nailed it
keyboard users can see where they are. :D
- Focus is where key presses go.
:focus-visibleshows a ring when it helps.- Never remove a focus ring without a better one.
new words in your collection
- focus
- focus-visible
- outline
This lesson isn’t saved. A free account remembers every lesson you finish.