- Home
- /
- CSS Pseudo-classes
CSS Tutorial
Introduction
CSS Properties
CSS Designing
CSS Advance Topics
CSS FAQs
- Home
- /
- CSS Pseudo-classes
CSS Pseudo-classes
A pseudo-class lets you style an element based on its state or position, without needing to add a specific class in your HTML. These are super useful for interactions like hovering over a button, clicking a link, or styling the first item in a list.
Basic Syntax:
selector:pseudo-class {
property: value;
}
:hover
— On Mouse Hover
This applies styles when the user hovers the mouse over an element.
When you move your mouse over the button, its background color changes.
:link
— Unvisited Link
Styles an anchor (<a>
) before it’s been clicked.
a:link {
color: blue;
text-decoration: none;
}
:visited
— After Clicked
Targets a link after it has been visited.
a:visited {
color: purple;
}
Combine it with :link
for full control.
:active
— While Being Clicked
Used while the element is being clicked.
a:active {
color: red;
}
Example:
:nth-child(n)
— Select by Position
Targets elements based on their order.
li:nth-child(2) {
color: green;
}
:first-child
and :last-child
li:first-child {
font-weight: bold;
}
li:last-child {
font-style: italic;
}
:focus
— When Element is Focused
The style is applied only when the user clicks or tabs into the input field.
Summary Table
Pseudo-class | Description |
---|---|
:hover | When the mouse hovers over an element |
:link | Unvisited link |
:visited | Link after it has been clicked |
:active | While the link or element is being clicked |
:focus | When an input is selected/focused |
:nth-child(n) | Targets elements by position in a list |
:first-child | Targets the first child element |
:last-child | Targets the last child element |