Apr 24, 2025
CSS pseudo-classes are used to define a special state of an element. They allow you to style elements based on their state, such as when a user hovers over them or when they have been visited. Pseudo-classes can be particularly useful for enhancing the interactivity and accessibility of web pages.
selector:pseudo-class {
property: value;
}
a:link { color: #FF0000; } /* Unvisited link */
a:visited { color: #00FF00; } /* Visited link */
a:hover { color: #FF00FF; } /* Mouse over link */
a:active { color: #0000FF; } /* Selected link */
a:hover must follow a:link and a:visited; a:active must follow a:hover for correct styling.highlight when hovered.
a.highlight:hover { color: #ff0000; }
<div> Elements:hover can be applied to other elements such as <div>.<div> on hover.
div:hover { background-color: blue; }
## Tooltip Hover Example
- Use `:hover` to display elements like tooltips.
- **Example**: Display a `<p>` element when hovering over a `<div>`.
```css
p { display: none; background-color: yellow; padding: 20px; }
div:hover p { display: block; }
:first-child Pseudo-class<p> element in a parent.
p:first-child { color: blue; }
- More complex selectors can be used to target specific child elements within parents.
## The `:lang` Pseudo-class
- Allows for language-specific styling.
- **Example**: Style quotes in paragraphs with Norwegian language.
```css
q:lang(no) { quotes: "~" "~"; }