CSS Pseudo Classes
1. Introduction to CSS Pseudo Classes
CSS Pseudo Classes allow you to style elements based on their state or position without adding extra classes or JavaScript.
Why Use Pseudo Classes?
- Create interactive web pages.
- Improve user experience.
- Style elements dynamically.
- Reduce JavaScript for simple effects.
- Create responsive forms and buttons.
a:hover{
color:red;
}
2. What are Pseudo Classes?
A pseudo class begins with a colon (:) and
selects elements based on a particular state rather than
their name or class.
selector:pseudo-class{
property:value;
}
| Pseudo Class | Description |
|---|---|
| :hover | When the mouse pointer is over an element. |
| :active | When an element is being clicked. |
| :focus | When an input element receives focus. |
| :visited | Styles visited links. |
3. :hover Pseudo Class
The :hover pseudo class applies styles when
the mouse pointer moves over an element.
button:hover{
background:green;
color:white;
}
img:hover{
transform:scale(1.1);
}
| Element | Hover Effect |
|---|---|
| Button | Change color |
| Image | Zoom effect |
| Link | Change text color |
:hover is one of the most commonly used pseudo classes in CSS.
4. :active Pseudo Class
The :active pseudo class applies styles while
the user is clicking or pressing an element.
button:active{
background:red;
transform:scale(0.95);
}
| State | Description |
|---|---|
| Normal | Button before clicking. |
| :active | Button while being clicked. |
:active to provide visual feedback when users click buttons or links.
5. :focus Pseudo Class
The :focus pseudo class styles form elements
when they receive keyboard or mouse focus.
input:focus{
border:2px solid blue;
outline:none;
}
textarea:focus{
background:#f5f5f5;
}
| Element | Focus Effect |
|---|---|
| Input | Blue border |
| Textarea | Background color changes |
| Select | Highlight active field |
:focus improves accessibility by clearly indicating the currently active form element.
6. :first-child & :last-child Pseudo Classes
The :first-child pseudo class selects the first
child element of its parent, while
:last-child selects the last child element.
li:first-child{
color:blue;
font-weight:bold;
}
li:last-child{
color:red;
}
| Pseudo Class | Description |
|---|---|
| :first-child | Selects the first child element. |
| :last-child | Selects the last child element. |
7. :nth-child() Pseudo Class
The :nth-child() pseudo class selects elements
based on their position within a parent element.
li:nth-child(2){
color:green;
}
tr:nth-child(even){
background:#f2f2f2;
}
tr:nth-child(odd){
background:#ffffff;
}
| Example | Description |
|---|---|
| :nth-child(1) | First child |
| :nth-child(3) | Third child |
| :nth-child(even) | Even-numbered elements |
| :nth-child(odd) | Odd-numbered elements |
:nth-child() is commonly used for striped tables and alternating list styles.
8. Other Common Pseudo Classes
CSS provides many other pseudo classes to style elements according to their current state.
| Pseudo Class | Purpose |
|---|---|
| :checked | Styles checked radio buttons and checkboxes. |
| :disabled | Styles disabled form controls. |
| :enabled | Styles enabled form controls. |
| :visited | Styles links that have already been visited. |
input:checked{
accent-color:green;
}
input:disabled{
background:#ddd;
}
a:visited{
color:purple;
}
9. Advantages of CSS Pseudo Classes
- Create interactive web pages.
- Improve user experience.
- Reduce the need for JavaScript.
- Improve accessibility.
- Style elements dynamically.
- Keep HTML clean.
- Easy to maintain.
| Feature | Available |
|---|---|
| Interactive Effects | ✔ Yes |
| Dynamic Styling | ✔ Yes |
| Accessibility Support | ✔ Yes |
10. CSS Pseudo Class Comparison Table
| Pseudo Class | Purpose | Example |
|---|---|---|
| :hover | Mouse over an element | button:hover |
| :active | Element while clicking | button:active |
| :focus | Focused input element | input:focus |
| :first-child | First child element | li:first-child |
| :last-child | Last child element | li:last-child |
| :nth-child() | Select by position | tr:nth-child(even) |
| :checked | Checked form control | input:checked |
| :disabled | Disabled element | input:disabled |
| :visited | Visited hyperlink | a:visited |
button:hover{
background:#007bff;
color:white;
}
button:active{
transform:scale(0.95);
}
11. CSS Pseudo Class Best Practices
Using pseudo classes correctly improves usability, accessibility, and the overall user experience of a website.
- Use
:hoverfor interactive effects. - Always provide a visible
:focusstyle for accessibility. - Use
:activeto provide click feedback. - Keep hover animations smooth and subtle.
- Use
:nth-child()for alternating rows. - Avoid unnecessary JavaScript when CSS pseudo classes are sufficient.
- Test interactions on desktop and mobile devices.
- Keep styles consistent throughout the website.
12. Common CSS Pseudo Class Mistakes
| Mistake | Correct Practice |
|---|---|
| Removing the focus outline completely. | Replace it with a custom visible focus style. |
| Using only hover effects. | Include focus styles for keyboard users. |
| Using excessive animations. | Keep transitions simple and smooth. |
| Ignoring mobile devices. | Test pseudo class behavior on touch devices. |
| Adding unnecessary HTML classes. | Use pseudo classes whenever appropriate. |
❌ Poor
input:focus{
outline:none;
}
✔ Better
input:focus{
outline:2px solid blue;
}
13. Browser Support
CSS Pseudo Classes are supported by all major modern web browsers.
| Browser | Support |
|---|---|
| Google Chrome | ✔ Supported |
| Mozilla Firefox | ✔ Supported |
| Microsoft Edge | ✔ Supported |
| Safari | ✔ Supported |
| Opera | ✔ Supported |
14. CSS Pseudo Class Interview Questions
- What is a CSS pseudo class?
- What is the purpose of the
:hoverpseudo class? - What is the difference between
:hoverand:active? - Why is the
:focuspseudo class important? - What does
:nth-child()do? - What is the difference between
:first-childand:last-child? - How does the
:checkedpseudo class work? - When is
:visitedapplied? - Can multiple pseudo classes be combined?
- Are CSS pseudo classes supported by modern browsers?
15. Lesson Summary
In this lesson, you learned:
- ✔ Introduction to CSS Pseudo Classes.
- ✔ What pseudo classes are.
- ✔
:hoverpseudo class. - ✔
:activepseudo class. - ✔
:focuspseudo class. - ✔
:first-childand:last-child. - ✔
:nth-child()pseudo class. - ✔ Other common pseudo classes.
- ✔ Best Practices and Common Mistakes.
- ✔ Browser Support and Interview Questions.
Quick Quiz
Which CSS pseudo class is used when the mouse pointer moves over an element?