Pseudo-classes
Make CSS react to hovering, focusing, position, and other special element states.
background: #20e778;
color: #071a35;
}
hover me
>_ What Is a Pseudo-class?
A pseudo-class lets CSS select an element when it is in a particular state or condition — such as when someone hovers over it, focuses on it, or when it appears in a certain position.
Pseudo-classes are added to selectors using a colon (:).
This allows your CSS to respond to what the user is doing without changing the HTML element itself.
Hover
Applies styles while the pointer is positioned over an element.
Focus
Applies styles when an interactive element such as an input receives focus.
First Child
Selects an element when it is the first child of its parent.
>_ The :hover Pseudo-class
:hover is commonly used for links, buttons, cards, and navigation items.
The styles activate while the user’s pointer is over the selected element.
background-color: #20e778;
color: #071a35;
}
The button keeps its normal styles until the pointer moves over it.
>_ The :focus Pseudo-class
:focus is especially useful with interactive controls such as text inputs.
Click or keyboard-tab into the input and its focus styles can appear.
outline: 3px solid #20e778;
background-color: #f4fff9;
}
>_ The :first-child Pseudo-class
Pseudo-classes aren’t only about mouse or keyboard interaction. Some describe an element’s position or relationship.
color: #087b49;
font-weight: bold;
}
This targets the first list item when that
<li> is the first child of
its parent.
>_ Reading a Pseudo-class Selector
button
The normal selector identifies the element being targeted.
:
The colon introduces the pseudo-class.
hover
Describes the state or condition that must be true.
{ ... }
These CSS declarations activate while the condition matches.
>_ Pseudo-classes Can Work With Classes
Pseudo-classes can also be attached to class selectors.
background-color: #071a35;
color: white;
}
.email:focus {
border-color: #009cff;
}
:hover depends on pointer
interaction, so don’t rely on hover alone
for important information. Keyboard users
often interact through states such as
:focus.
Make It Interactive
Use pseudo-classes to make different parts of the page react to interaction and position.
- ✓ Create a button with normal CSS styling.
-
✓
Use
button:hoverto change its background and text color. -
✓
Add an
<input>field. -
✓
Use
input:focusto make the input stand out when selected. - ✓ Create a list with at least three items.
-
✓
Use
li:first-childto style only the first item.
Pseudo-classes Challenge
What You’ve Learned
:.
:hover responds when
the pointer is over an element.
:focus styles an
interactive element when it receives focus.
:first-child can target
an element based on its position.
Nice work!
Finished this lesson? Mark it complete to track your progress.


