Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Style Hover and Keyboard Focus

In this lesson, you gave the menu links on the events page you built in HTML a style for the mouse and the same style for the keyboard. Then you forced a state in DevTools to inspect its rule, and met the states a link goes through when it’s visited and pressed.

Key Points

  • A pseudo-class is a colon and a name added to a selector, as in .site-nav a:hover. The ones in this lesson are state pseudo-classes: each is a condition on what’s happening to the element. The style applies only while the condition holds, a bit like an if in your code. Unlike an if, you never run that check. The browser checks again each time the mouse moves.
  • :hover matches while a pointer, such as the mouse, is over the element. With the rule below, Plots turns pale green when you point at it, and the green goes when you move away. Without it, the pointer turns into a hand, but the link itself doesn’t react.
  • Tab moves the keyboard’s focus from link to link. The focused link gets a dark ring, the browser’s own focus style.
  • Watch out: Tab never triggers :hover, so keyboard users never see your hover style. Tab to Plots with only the hover rule, and it has the ring but no green background.
  • :focus-visible matches the focused element when the browser decides focus should show, as it does after Tab. Add it to the hover selector after a comma, and Plots is pale green with its ring when you Tab to it.
  • Keep the focus ring. It’s the link’s outline, and a rule like outline: none takes it away. Never write one without a focus style in its place. Here, Plots keeps both the ring and the green.
  • Force a state in DevTools: with the link selected in the Elements panel, the Styles pane doesn’t list the hover rule, because the mouse is in DevTools and the link isn’t focused. Click :hov at the top of the Styles pane, and under Force element state check :hover. The rule appears in the Styles pane, and the link turns pale green on the page while the box stays checked.
  • :link matches a link you haven’t visited yet, :visited one you have, and :active a link while you’re pressing it. After you visit The work day’s jobs and come back, it’s in the darker green, while How the swap works still matches :link and keeps the garden’s green. If you’ve opened these pages before, their links, the menu’s included, are darker from the start.
  • Press and hold How the swap works, and it turns the darkest green. Slide the pointer off the link before you let go, and the link doesn’t open.
  • Keep :active below :visited for now. Find Out Which Rule Wins explains why.
  • :visited has limits, to protect your privacy. Otherwise a page could link to other sites and check which ones look visited. So :visited can only change colors. A font size in it is ignored, because scripts can measure a bigger link. And a script that asks for a link’s color gets the unvisited one.
  • The rule: a state pseudo-class matches while its state holds. Give every hover style a :focus-visible style too, so the mouse and the keyboard both get an answer.

Final Code

At the end of events/events.css, after the h2 + p rule from Follow the Tree With Combinators, add the rule for the menu’s links:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 So far, your selectors pick elements by what they are and where they sit. Now you’ll style links by what’s happening to them. Here’s the events page, with its nav of three links.

00:11 Point at Plots with the mouse. The pointer turns into a hand, but the link itself doesn’t react, so nothing on the page shows which one you’re on.

00:22 At the end of events.css, add a rule. Its selector picks the nav’s links, then adds a colon and the word hover after them. That colon starts a state pseudo-class, a condition on what’s happening to the element.

00:37 Give the rule a pale green background. Save, and point at Plots again. Its background turns pale green. Move away, and it’s gone.

00:48 It works like an if in your code. The style applies only while the condition holds. But you never run this check. The browser does, each time the mouse moves.

01:00 Now leave the mouse alone and press Tab, as keyboard users do, to move focus from link to link. Home gets a dark ring, the browser’s own focus style. Press Tab again.

01:11 Plots has the ring, but no green background. Watch out: Tab never triggers :hover, so keyboard users never see your hover style.

01:22 The state they need is :focus-visible. It matches the focused element when the browser decides focus should show, as it does after Tab. Add it to the :hover selector, after a comma.

01:34 Tab to Plots again, and it’s pale green with its ring. Keep that ring. It’s the link’s outline. A rule like outline: none takes it away. Never write one without a focus style in its place.

01:48 Here, Plots keeps both the ring and the green.

01:53 To inspect the rule, select the Plots link in the Elements panel. The Styles pane doesn’t list it, because the mouse is in DevTools and Plots isn’t focused.

02:03 Click the :hov button at the top of the Styles pane. Under Force element state, check the :hover box. Your rule appears in the Styles pane, and Plots turns pale green while the box stays checked.

02:18 Links have three more states. The :link state matches a link you haven’t visited yet, and :visited matches one you have. And :active matches while you’re pressing it.

02:29 Add two rules: visited links in the darker green, and active ones in the darkest. Keep :active below :visited: Find Out Which Rule Wins explains why. Click The work day’s jobs, and come back.

02:42 That link is darker now. How the swap works still matches the :link state. Opened these pages before? Their links start darker, the menu’s too. Now press and hold How the swap works, and it turns darkest.

02:56 Slide off the link before you let go, and it doesn’t open.

03:01 The :visited state has limits, to protect your privacy. Otherwise a page could link to other sites and check which ones look visited. So it can only change colors.

03:11 A font size in it is ignored, because scripts can measure a bigger link. And a script that asks for a link’s color gets the unvisited one.

03:21 So a state pseudo-class picks an element by what’s happening to it right now. Give every :hover style a :focus-visible style too, so the mouse and the keyboard both get an answer.

03:33 Next is Select by Position and Add Content, where you’ll pick elements by their place among their siblings, and add small decorations.

Become a Member to join the conversation.