Coding/Learn/css/pseudo classes
CSS Beginner Course
01.Introduction to CSS
02.Selectors (Element, Class & ID)
03.3 Ways to Add CSS to HTML
04.Colors & Backgrounds (HEX, RGB, HSL)
05.Typography & Fonts (font-size, font-weight, line-height)
06.The Cascade & Specificity Wars
07.The Sacred CSS Box Model
08.Padding vs Margin & Shorthand Notation
09.Borders & Border Radius (Rounded Corners & Circles)
10.The Magic of box-sizing: border-box
11.Sizing & Units (px, rem, em, %, vh, vw)
12.The Display Property (block, inline, inline-block, none)
13.Flexbox Part 1: Container Powers (justify, align, gap)
14.Flexbox Part 2: Direction, Wrapping & Item Powers
15.Positioning (relative, absolute, fixed, sticky)
16.Z-Index & The 3D Stacking Context
17.Pseudo-Classes (:hover, :active, :focus, :first-child)
18.Smooth Transitions & Box Shadows
19.Responsive Design & Media Queries (@media)
20.Capstone: Crafting a Modern UI Profile Card
Course Certificate0%

Complete all lessons to earn

LearnPseudo-Classes (:hover, :active, :focus, :first-child)
17 / 20 0%
Lesson 17 of 20
5 mins read

Pseudo-Classes (:hover, :active, :focus, :first-child)

PreviousNext Lesson
In Plain English

Pseudo-classes let you style elements based on user interaction states or document position. The most famous is :hover (when the mouse hovers over an element), :active (when clicking down), and :focus (when focused via keyboard Tab).

The Mental Model

"Think of a physical elevator button. The default state is a flat steel circle. :hover is your finger approaching it. :active is pressing the button down. :focus is the glowing yellow ring showing which floor is selected."

Deep Dive: How It Works

:hover: Triggers when the cursor hovers over the element. Essential for buttons and cards.

:active: Triggers while the user is actively holding down the mouse click or touchscreen tap (perfect for scale(0.98) tactile click feel!).

:focus and :focus-visible: Crucial for keyboard accessibility when users Tab through inputs and links.

:first-child and :last-child: Targets the first or last item in a list or grid.

:nth-child(even/odd/2n+1): Creates alternating zebra-striped table rows with zero extra classes!

Syntax Blueprint

/* Button Hover & Active States */
.btn {
  background-color: #ffffff;
  color: #000000;
  transition: background-color 0.15s ease;
}
.btn:hover {
  background-color: #e2e8f0;
}
.btn:active {
  transform: scale(0.98);
}

Define hover color changes and active tactile click down-states.

PointCore Rules to Remember

Point star
Interactive Feedback: Always provide clear visual hover and focus feedback on clickable elements.
Point star
The Tactile :active: Adding a subtle transform: scale(0.98) on :active makes web apps feel like native software.
Point star
Never Remove Focus Rings: Never set outline: none without providing a clean visible replacement for keyboard navigation.

Common Beginner Traps & How to Fix Them

Mistake:Forgetting the colon before pseudo-classes

Why it happens: Writing .btnhover instead of .btn:hover.

How to fix: Pseudo-classes always begin with a single colon: :hover, :focus, :active.

Pro Tip: Use :focus-visible instead of :focus to only show keyboard rings for Tab users while keeping mouse clicks clean!

Live Interactive Example

Hit Run Code to see it live
Interactive Button State Machine
CSS3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Live Browser Preview
Click "Preview / Apply" to view the rendered output.
How it works: :hover brightens the background and :active compresses the button for a tactile feel.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Create a hover state for a link

  • PointWrite a CSS rule for a:hover setting color: #38bdf8;
1
2
Browser Preview & Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates