-
Hajipur, Bihar, 844101
CSS Pseudo-classes are special keywords added to selectors that define a specific state of an element. They allow you to style elements not just based on their type, class, or ID, but based on how users interact with them or where they appear in the document structure. Pseudo-classes make websites more interactive, user-friendly, and visually responsive without using JavaScript.
In real-world websites, users hover over links, click buttons, fill out forms, and navigate through menus. CSS pseudo-classes help you respond to these actions directly in CSS. In this chapter, you will learn what CSS pseudo-classes are, why they are important, and how to use the most common ones with practical examples.
A pseudo-class represents a special state of an element. It starts with a colon (:) and is attached to a selector.
Pseudo-classes answer questions like:
What happens when a user hovers over an element?
How should a link look after it is visited?
Is this input field focused or disabled?
Is this element the first or last child?
Instead of adding extra classes in HTML, pseudo-classes let CSS handle these conditions automatically.
Pseudo-classes play a key role in modern web design and usability.
Some important benefits include:
Improve user interaction without JavaScript
Provide visual feedback to users
Make forms easier to use
Enhance navigation and accessibility
Reduce extra HTML markup
Keep CSS clean and meaningful
Without pseudo-classes, interactive styling would require scripts or repeated class changes.
The basic syntax is:
selector:pseudo-class {
property: value;
}
Example:
a:hover {
color: red;
}
This applies styles to anchor elements only when the user hovers over them.
CSS provides many pseudo-classes. Some are related to user interaction, some to links, forms, and document structure.
Let’s explore the most important ones in detail.
These pseudo-classes are used mainly with anchor (<a>) elements.
The :link pseudo-class selects links that have not been visited yet.
a:link {
color: blue;
}
This style applies only to fresh, unvisited links.
The :visited pseudo-class selects links that the user has already visited.
a:visited {
color: purple;
}
This helps users recognize which pages they have already opened.
The :hover pseudo-class applies styles when the user places the mouse pointer over an element.
a:hover {
text-decoration: underline;
}
It is commonly used for links, buttons, menus, and cards.
The :active pseudo-class applies when an element is being clicked.
a:active {
color: orange;
}
This gives instant feedback during a click action.
When styling links, the correct order is important:
:link
:visited
:hover
:active
Following this order prevents styles from overriding each other incorrectly.
Forms are a major part of user interaction. Pseudo-classes make forms more user-friendly.
The :focus pseudo-class applies when an input element is selected or active.
input:focus {
border-color: blue;
outline: none;
}
It helps users see which field they are currently typing in.
The :disabled pseudo-class selects elements that are disabled.
input:disabled {
background-color: #eee;
cursor: not-allowed;
}
This visually indicates that the field cannot be used.
The :enabled pseudo-class selects elements that are active and usable.
input:enabled {
background-color: white;
}
The :checked pseudo-class applies to checked checkboxes or radio buttons.
input:checked {
outline: 2px solid green;
}
It is commonly used for custom form controls.
The :required pseudo-class targets required input fields.
input:required {
border-left: 4px solid red;
}
This helps users identify mandatory fields.
Structural pseudo-classes are based on the position of elements in the HTML structure.
Selects the first child of a parent element.
li:first-child {
font-weight: bold;
}
Only the first list item will be affected.
Selects the last child of a parent element.
li:last-child {
color: red;
}
Selects elements based on a specific position or pattern.
li:nth-child(2) {
color: blue;
}
You can also use patterns:
li:nth-child(even) {
background-color: #f5f5f5;
}
This is very useful for table rows and lists.
Counts elements from the end instead of the beginning.
li:nth-last-child(1) {
font-style: italic;
}
Selects elements that are the only child of their parent.
p:only-child {
color: green;
}
These pseudo-classes respond to user actions and system states.
:hover works with mouse interaction
:focus works with keyboard and mouse
For accessibility, both should be styled properly.
button:hover,
button:focus {
background-color: #333;
color: white;
}
Selects elements that have no content.
div:empty {
display: none;
}
Useful for hiding empty containers.
You can combine pseudo-classes with other selectors.
input[type="text"]:focus {
background-color: #ffffcc;
}
This applies only to focused text inputs.
Some mistakes beginners make include:
Confusing pseudo-classes with pseudo-elements
Forgetting the colon syntax
Not following link order rules
Overusing complex selectors
Ignoring keyboard focus styles
Understanding these details avoids bugs and accessibility issues.
Follow these practices for clean CSS:
Always style :focus for accessibility
Use :hover with care on touch devices
Keep selectors readable
Combine pseudo-classes logically
Test interactions in the browser
These habits help create professional and user-friendly designs.
Pseudo-classes are used everywhere in real websites:
Navigation menus
Buttons and hover effects
Form validation styling
Table row highlighting
Interactive cards and lists
They reduce dependency on JavaScript and improve performance.
CSS pseudo-classes allow you to style elements based on their state, position, or user interaction. From link states like :hover and :visited to form states like :focus and :checked, and structural selectors like :first-child and :nth-child, pseudo-classes make CSS powerful and interactive. Mastering pseudo-classes helps you build responsive, accessible, and user-friendly websites with clean and efficient code.
Q1. Make hovered links change color to red.
Q2. Bold the first paragraph inside a container.
Q3. Apply background to even list items.
Q4. Highlight form input when focused.
Q5. Change color of checked checkboxes.
Q6. Style the last child of an <ul>.
Q7. Make required input fields show red border.
Q8. Hide buttons that are disabled.
Q9. Apply italic style to all paragraphs except ones with class .no-style.
Q10. Use :nth-child(3) to underline the 3rd list item.