Sub topic 5
Style hover and keyboard focus states
This chapter assumes you know basic HTML elements and attributes, CSS selectors and properties, and the box model. It also assumes you already have the navigation bar from the earlier chapters: a nav containing a ul.nav-list of links, laid out as a flex row, with each link set to display: block and padded.
Why links need more than one state
A link can be in several states at once. A mouse user sees a hover state. A keyboard user sees a focus state. Both need visible feedback, or the reader cannot tell which link they are about to activate.
Many people never use a mouse. They press Tab to move between links and Enter to follow one. If your CSS removes the focus indicator, those readers cannot see where they are. Styling focus is a requirement for a usable bar, not decoration.
Style the hover state
The :hover pseudo-class matches an element while a pointer is over it. Add a slightly lighter background to the link:
.nav-list a:hover {
background: #374151;
}Because each link is display: block with padding, the whole padded rectangle changes color, not just the text. This is why the previous chapter's box setup matters.
Style keyboard focus with :focus-visible
CSS has two focus selectors:
:focusmatches any element that has focus, including after a mouse click.:focus-visiblematches focus only when the browser decides the indicator should show. For links, that is usually keyboard navigation.
Use :focus-visible so mouse users do not see an outline after every click, while keyboard users always do.
.nav-list a:hover,
.nav-list a:focus-visible {
background: #374151;
}
.nav-list a:focus-visible {
outline: 2px solid #38bdf8;
outline-offset: -2px;
}The first rule gives hover and focus the same background, so both input methods get the same cue. The second rule adds an outline for focus only. The negative outline-offset draws the outline inside the link's box. This matters because the bar has gap and may sit against the edge of the page, where an outside outline could be clipped.
Style the current page
The link for the page the reader is on should look different from the others. The markup already carries this information in aria-current="page", so style from that attribute:
.nav-list a[aria-current="page"] {
background: #111827;
border-bottom: 3px solid #38bdf8;
}The attribute selector uses the same attribute that screen readers announce. Sighted users and assistive technology therefore get the same information from one source. A separate .active class would have to be kept in sync by hand.
How the rules combine
All of these selectors have the same specificity, which is one class plus one pseudo-class or attribute, plus one element. When two rules match, the later one in the stylesheet wins. Consider a link that is the current page and is also hovered. The hover rule sets #374151, and the current-page rule sets #111827. Because the current-page rule comes last, the current link keeps its darker background on hover.
Whether that is what you want is a design choice. If you want hover feedback on the current link too, move the current-page rule above the hover rule.
Common errors
- Removing the outline. Writing
outline: noneon links hides the focus indicator. If you remove it, you must provide an equally visible replacement. - Styling only
:hover. Keyboard users get no feedback. Pair:hoverwith:focus-visible. - Putting the padding on the
li. The hover color then fills the list item, but the clickable area stays small. Keep padding on thea. - Rule order surprises. If a state does not appear, check whether a later rule with equal specificity overrides it.
- Using
:focusfor the outline. This works, but mouse clicks then show an outline that many designers find distracting.
Test it
Open the page and press Tab. Focus should move through the links in order, and each one should show the outline. Then hover with the mouse and confirm the background changes. Finally, tab to the current-page link and check that it is still distinguishable from the others.
Exercise
A teammate adds .nav-list a { outline: none; } because the focus ring "looks ugly." Why is this a problem, and what is a better fix?
Show answer
With the outline removed, keyboard users cannot see which link has focus, so they cannot tell what Enter will activate. A better fix is to restyle it, not remove it: use a:focus-visible with a visible outline such as outline: 2px solid #38bdf8; outline-offset: -2px;, and adjust the color or thickness to suit the design.
Exercise
A link is both the current page and hovered, but its background does not change on hover. The current-page rule appears after the hover rule in the stylesheet. Name the cause and give one way to make hover visible on the current link.
Show answer
Both rules have equal specificity, so the later one (current page) wins. Move the [aria-current="page"] rule above the hover rule, or add a separate rule such as .nav-list a[aria-current="page"]:hover with the hover color.