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 anifin your code. Unlike anif, you never run that check. The browser checks again each time the mouse moves. :hovermatches 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-visiblematches 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: nonetakes 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.
:linkmatches a link you haven’t visited yet,:visitedone you have, and:activea 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:linkand 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
:activebelow:visitedfor now. Find Out Which Rule Wins explains why. :visitedhas limits, to protect your privacy. Otherwise a page could link to other sites and check which ones look visited. So:visitedcan 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-visiblestyle 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: