CHAPTER 29
CSS Pseudo-classes
Learn how pseudo-classes style elements based on their state or position.
01. What is CSS Pseudo-classes?
Pseudo-classes select elements based on a special state.
Common examples include :hover, :focus, :active and :first-child.
They are extremely useful for interactive website designs.
02. Syntax
selector:pseudo-class {
property: value;
}
03. Example
CSS / HTML Example
AIFA Coding Lab
<!DOCTYPE html>
<html>
<head>
<style>
button {
padding: 12px 20px;
border: none;
background: #2563eb;
color: white;
}
button:hover {
background: #06b6d4;
}
button:active {
transform: scale(.96);
}
input:focus {
outline: 3px solid #38bdf8;
}
</style>
</head>
<body>
<button>
Hover Me
</button>
<br><br>
<input
type="text"
placeholder="Click and type"
>
</body>
</html>
04. Important Points
✓
:hover applies when the pointer is over an element.
✓
:focus applies when an element receives focus.
✓
:active applies while an element is being activated.
✓
:first-child selects the first child element.
✓
Pseudo-classes are important for interactive interfaces.
05. Common Mistakes
⚠
Using hover as the only way to communicate important information.
⚠
Removing focus styles from keyboard users.
⚠
Creating effects that are difficult to use on touch devices.
06. Real World Use
Pseudo-classes are used for buttons, navigation links, forms, menus, cards, tabs and interactive components.
Ready to Practice? 🚀
Open the AIFA CSS Editor and practice this chapter live.
Try It Yourself →
Practice Exercise
Create an Interactive Button
Create a button with normal, hover and active states. Also create an input with a custom focus effect.