Pseudo classes let you style elements based on their state (hover, focus, active) or position (:first-child, :nth-child). They make interactive and dynamic styling possible without JavaScript.
Step 1: Using :hover
What this code does: Makes a blue button turn a darker blue while the mouse pointer is over it. The first rule sets how the button normally looks, and the :hover rule changes only the background colour for the moment of hovering. Move the pointer over the button in the preview and away again: the colour switches back by itself, with no JavaScript.
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the :hover demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo has a normal rule for the button and a second rule for its hover state. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
This is the normal state of the button, the way it looks when nothing is happening. padding: 12px 20px makes it roomy, and background: #007BFF gives it a blue color.
White text on blue is easy to read. border: none removes the default gray button border, and cursor: pointer shows the hand cursor, which tells the user the button can be clicked.
.demo-button:hover has two parts: the class selects the button, and :hover limits the rule to the moment the mouse pointer is over it. Only the background changes to a darker blue, and every other property is kept from the normal rule. When the pointer leaves, the browser goes back to the normal style automatically, with no JavaScript. Touch screens have no real hover, so never depend on it alone.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: one button.
A normal button element with the demo-button class. Move the mouse over it in the preview to see the color change.
</body> closes the body.
</html> is the final closing tag and ends the document.
Step 2: Using :active
What this code does: Makes a green button turn dark green only while it is being pressed. :active applies from the moment the mouse button goes down until it is released, so the button looks pushed in. Press and hold the button in the preview to see it, and release to bring the normal colour back.
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the :active demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo styles the moment when the button is pressed. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
The normal state: a roomy button with a green background.
White text for contrast, and border: none to remove the default border.
:active applies only while the element is being pressed, from the moment the mouse button goes down until it is released. The background becomes a much darker green, so it looks as if the button is pushed in. Releasing it brings the normal color back. A button can also be activated with the Space or Enter key, which triggers the same state.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: one button.
Press and hold the mouse button on it in the preview. The color changes only while it is held down.
</body> closes the body.
</html> is the final closing tag and ends the document.
Step 3: Using :focus
What this code does: Highlights a text field when the user clicks into it or reaches it with the Tab key. The field normally has a gray border. With :focus the border turns blue and a soft blue ring appears, so the user can see where typing will go. Click the field and then click elsewhere to see the highlight appear and disappear.
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the :focus demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo styles a text field when the user is typing in it. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
The default look of the field: padding: 10px gives the text room, and a 2px gray border outlines it.
An element has focus when it is ready for input: after a click, or after the user reaches it with the Tab key. border-color changes only the color of the existing border to blue, so the field looks highlighted.
Browsers draw their own outline on a focused field. outline: none removes it, so the design is clean, but keyboard users need to see where they are. That is why a replacement is added: box-shadow: 0 0 0 3px draws a soft 3px blue ring (no offset, no blur) in a transparent blue. Never remove the outline without a visible replacement.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: one text field.
type="text" makes a one-line text field, and placeholder shows a hint that disappears when typing starts. Click the field, or press Tab, to focus it.
</body> closes the body.
</html> is the final closing tag and ends the document.
Step 4: Using :first-child
What this code does: Makes only the first item of a list red and bold, without adding a class to that item. The selector li:first-child chooses the item by its position, so the other items keep the normal style. In the preview, HTML is red and bold, while CSS and JavaScript stay normal.
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the :first-child demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo styles one item of a list, without giving it a class. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
Read it from left to right: inside .demo-list, select each li that is the first child of its parent. :first-child is based on position, not state. No extra class is needed on the item, and if the first item changes, the style follows automatically. Similar selectors are :last-child and :nth-child(2).
The matching item becomes red and bold. The other list items keep the normal style.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: a list with three items.
The ul has the demo-list class, which the selector uses to find the list.
This li is the first child of the ul, so it is red and bold.
These are the 2nd and 3rd children, so :first-child does not match them, and they keep the normal style.
</ul> closes the list.
</body> closes the body.
</html> is the final closing tag and ends the document.
Step 5: Using :nth-child()
What this code does: Gives alternate table rows a light gray background (zebra striping) with :nth-child(even). The 2nd and 4th rows (CSS and Python) turn gray, while the 1st and 3rd stay white. Stripes make long tables easier to read, and the pattern keeps working when you add more rows.
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the :nth-child() demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo stripes the rows of a table. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
border-collapse: collapse merges the cell borders into single lines, and width: 100% makes the table fill its container.
A light gray border and 10px of padding for every data cell.
:nth-child(even) selects the 2nd, 4th, 6th and so on, counted among the rows in the same group. Those rows get a light gray background. You can use odd, a number such as :nth-child(3), or a formula such as :nth-child(3n) for every third row.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: a table with four rows.
The table with the demo-table class. The browser puts the rows into an invisible tbody, and the count starts there.
The 1st row is odd, so it keeps the white background.
The 2nd row is even, so it gets the gray stripe.
The 3rd row is odd, so it is white.
The 4th row is even, so it is gray. The pattern continues if you add more rows.
</table> closes the table.
</body> closes the body.
</html> is the final closing tag and ends the document.
Complete CSS Pseudo Classes Example
What this code does: Combines three states in one demo. The button turns a darker blue on :hover and an even darker blue on :active (while pressed), and the text field gets a blue border and ring on :focus. Hover over the button, press it, and click into the field to see each state in turn.
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the pseudo-classes demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo uses three different pseudo-classes: hover and active for the button, focus for the input. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
The normal button: blue background with white text.
border: none removes the default border, padding makes the button roomy, and cursor: pointer shows the hand cursor.
A darker blue while the mouse pointer is over the button.
An even darker blue while the button is pressed. This rule is written after :hover on purpose. While you press, the pointer is also over the button, so both rules match. They have the same specificity, so the later rule wins, and the pressed color shows. If you swap them, the press color would never appear.
The input has 10px of padding and a gray border. margin-top: 15px adds space between it and the button above.
When the field has focus, the border turns blue. The default outline is removed, and a soft blue ring made with box-shadow replaces it, so keyboard users can still see where they are.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: a button and a text field.
The button reacts to hover and press. Try both.
<br> moves the input to the next line. The space between them comes from margin-top in the CSS.
Click it, or press Tab to reach it, and the blue focus ring appears.
</body> closes the body.
</html> is the final closing tag and ends the document.
Advantages of CSS Pseudo Classes
Advantage
Benefit
Description
Interactive Design
Better User Experience
Responds to user actions without JavaScript.
Cleaner CSS
Less Code
Styles elements based on state or position.
Form Styling
Easy Input Design
Highlights active input fields.
Flexible Selection
Precise Styling
Targets specific elements without extra classes.
Best Practices
Use :hover to provide visual feedback for interactive elements.
Style form controls with :focus to improve accessibility.
Use :nth-child() for striped tables and lists.
Avoid relying only on hover effects for mobile devices.
Keep pseudo-class styles simple and consistent.
Use meaningful color changes to improve usability.
Test interactive states in different browsers.
Combine pseudo-classes with classes and IDs for more specific styling.