Interaction and link states
Definition and purpose
Interaction pseudo-classes match changing browser states: :hover during pointing, :active during activation, :focus on the focused element, and :focus-visible when the browser determines a visible focus indicator is appropriate. They let you communicate interaction without scripting.
Hover and keyboard focus
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> button { color: navy; } button:hover { color: green; } button:focus-visible { outline: 3px solid navy; } </style> </head> <body> <button id="a">Save</button> <button id="b">Cancel</button> </body></html>Move a pointer onto Save and then use Tab in a fresh page to see distinct states.
Syntax and mental model
button:hover can change appearance when a pointer hovers. button:focus-visible can provide a clear keyboard focus outline; :focus-within also matches an ancestor containing focus. a:link matches unvisited links, a:visited visited links (restricted by privacy protections), :any-link either. :target matches the element identified by the document URL fragment. A state can overlap another state; competing declarations are explained in the cascade chapter. An outline such as 3px solid navy draws a visible line without changing normal box size.
Focus inside a form
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> .field { background-color: white; } .field:focus-within { background-color: lightblue; } </style> </head> <body> <div id="a" class="field"> <label for="name">Name</label> <input id="name"> </div> </body></html>Focusing the labeled input changes the container background; blur restores white.
Common mistakes
Hover is not a keyboard or touch substitute. Do not remove default focus outlines without an accessible replacement. Visited styles are privacy-restricted, so script-reported styles cannot reveal browsing history.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Structural pseudo-classes. Next lesson: Pseudo-elements and generated boxes.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Interaction and link states — knowledge check
Five questions on interaction and link states.
Which matches when the pointer is over a button?
Which is especially useful for keyboard focus indication?
Which matches a container when its input has focus?
Why should hover not be the only interactive cue?
What is true of :visited styling?