Loading video player…

Keep Focus Visible and in View

In this video, you made focus visible on the choir’s site, and kept it out from under the sticky header.

Key Points

  • The starting stylesheet’s base layer removed the outline from every link and button with nothing in its place, so pressing Tab moved focus without anything on the page showing it. That fails 2.4.7 Focus Visible (Level AA).
  • Watch out for hover styles. Pressing Tab never triggers :hover, so a hover style isn’t a focus style.
  • Deleting the rule would bring back the browser’s thin default ring. A bare :focus-visible rule replaces it and covers every control, in the site’s own focus colors.
  • :focus-visible matches the focused element when the browser decides you need to see it. In Chrome, clicking Ticket prices with the mouse moved focus to the dialog’s Getting to the venues link, which matched :focus but not :focus-visible, so no ring appeared. Pressing Tab moved focus to Close, and the ring appeared.
  • :focus matches whenever an element has focus, so a rule on it would ring the link after a click. Clicks can count for :focus-visible too. A text field matches it after a click.
  • :focus-within matches a container, such as the dialog, while anything inside it has focus. It’s for styling the container, so the ring skips it.
  • The components layer’s .site-header rule sets --color-focus to --color-focus-on-brand, so the one ring rule draws yellow in the dark header, at 9:1, and red on the page, at 6.25:1. Both clear the 3:1 that 1.4.11 Non-text Contrast asks of a focus ring.
  • An outline stays in forced colors, where a box shadow is dropped.
  • The browser doesn’t scroll to a focused control that’s already in the window. In a short window, walking back up the Concerts page with Shift+Tab put focus on Ticket prices while it sat under the sticky header, so the header covered it, ring and all. That fails 2.4.11 Focus Not Obscured (Minimum) (Level AA).
  • scroll-padding-top tells the browser that the top of the window is taken. When it scrolls, it treats that strip as outside the window.

The Final Code

In the base layer, the rule that removed the outline is now a focus ring:

Language: CSS
  :focus-visible {
    outline: 3px solid var(--color-focus);
    outline-offset: 2px;
  }

Every control now shows a ring when focus reaches it from the keyboard. It’s yellow in the header and red on the page.

The header is 67 pixels tall, a little over 4rem, so the top of the base layer pads the top of the window by 5rem, leaving room for the ring:

Language: CSS
  html {
    scroll-padding-top: 5rem;
  }

Walking back up the Concerts page with Shift+Tab now scrolls Ticket prices out from under the header. At 200% zoom, the walk ends the same way. Zoom scales rem and pixels alike, so the header still measures 67 pixels, Ticket prices still comes out with its ring, and the 5rem needs no change.

Next comes Add a Skip Link and Hide Closed Menus, where you’ll give keyboard users a way past the header and keep a closed menu’s links out of their way.

00:00 When you walked the choir’s site, you only saw focus by switching a rule off. In this video, you’ll make focus visible, and keep it in view. On the home page, press Tab three times.

00:13 Focus is now on Rehearsals, but nothing on the page shows it.

00:18 A rule in the base layer takes the outline away from every link and button, with nothing in its place. Watch out for hover styles too. Tab never triggers hover, so a hover style isn’t a focus style.

00:32 Someone using the keyboard can’t see where they are. That fails 2.4.7 Focus Visible, a Level AA criterion.

00:42 Deleting the rule would bring back the browser’s thin default ring. Replace it instead with a bare :focus-visible rule, which covers every control.

00:51 It matches the focused element when the browser decides you need to see it. Give it a 3 pixel solid outline in the site’s focus color, and an offset of 2 pixels.

01:02 Press Tab three times again. A yellow ring now surrounds Rehearsals. The header sets --color-focus to its own yellow, at 9 to 1 on the dark blue.

01:14 On the page, it’s red, at 6.25 to 1. Both clear the 3 to 1 that 1.4.11 Non-text Contrast asks of a focus ring. And an outline stays in forced colors, where a box shadow is dropped.

01:29 Why :focus-visible and not :focus? Open the Concerts page, and click Ticket prices with the mouse. The dialog opens, and focus moves to its link, Getting to the venues.

01:41 In Chrome, the link matches :focus, but not :focus-visible, so no ring appears.

01:48 Now press Tab. Focus moves to Close, and a red ring appears, because you’re using the keyboard. :focus matches whenever an element has focus, so a rule on it would ring the link after a click.

02:00 Clicks can count too. After a click, a text field matches :focus-visible. While anything inside the dialog has focus, the dialog itself matches :focus-within, a third pseudo-class.

02:14 It’s for styling a container, so the ring skips it.

02:19 The header is sticky, so the page scrolls under it. Make the window short, about 450 pixels, as with DevTools open below. Scroll to the bottom, and click the footer’s text so focus starts there.

02:32 Then press Shift+Tab. Walking back up is where a sticky header gets in the way. Focus lands on the sign-up link. It’s already in the window, so the browser doesn’t scroll.

02:44 Get tickets isn’t a tab stop yet, so the next stop is Ticket prices, above it, still inside the window. Your turn: pause the video and predict what you’ll see after one more Shift+Tab.

03:02 Nothing at all. Focus is on Ticket prices, but it’s inside the window, so the page didn’t scroll. The header covers it, ring and all. That fails 2.4.11 Focus Not Obscured (Minimum), also Level AA.

03:20 The fix tells the browser that the top of the window is taken. DevTools measures the header at 67 pixels, a little over 4 rem. In the base layer, give the html element a scroll-padding-top of 5 rem, the header’s height plus room for the ring.

03:36 Now the browser treats those top 5 rem as outside the window when it scrolls. Walk back up with Shift+Tab again. This time the page scrolls, and Ticket prices comes out from under the header with its ring.

03:50 Repeat the walk at 200% zoom. Ticket prices still comes out with its ring, so 5 rem needs no change.

03:59 Here’s the rule: every control shows a ring on :focus-visible, and a sticky header never hides a control as focus reaches it. Next comes Add a Skip Link and Hide Closed Menus.

04:12 There, you’ll give keyboard users a way past the header, and keep a closed menu’s links out of their way.

Become a Member to join the conversation.