Academic Block

CSS Pseudo Classes Get Certified

What are CSS Pseudo Classes?

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>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
.demo-button {
    padding: 12px 20px;
    background: #007BFF;
    color: white;
    border: none;
    cursor: pointer;
}
.demo-button:hover {
    background: #0056b3;
}
</style>
</head>
<body>
<button class="demo-button">Hover Over Me</button>
</body>
</html>

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>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
.demo-button {
    padding: 12px 20px;
    background: #28a745;
    color: white;
    border: none;
}
.demo-button:active {
    background: #145c2d;
}
</style>
</head>
<body>
<button class="demo-button">Click Me</button>
</body>
</html>

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>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
.demo-input {
    padding: 10px;
    border: 2px solid gray;
}
.demo-input:focus {
    border-color: #007BFF;
    outline: none;
    box-shadow: 0 0 0 3px rgba(0,123,255,.3);
}
</style>
</head>
<body>
<input class="demo-input" type="text" placeholder="Enter your name">
</body>
</html>

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>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
.demo-list li:first-child {
    color: red;
    font-weight: bold;
}
</style>
</head>
<body>
<ul class="demo-list">
<li>HTML</li>
<li>CSS</li>
<li>JavaScript</li>
</ul>
</body>
</html>

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>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
.demo-table {
    border-collapse: collapse;
    width: 100%;
}
.demo-table td {
    border: 1px solid #ccc;
    padding: 10px;
}
.demo-table tr:nth-child(even) {
    background: #f2f2f2;
}
</style>
</head>
<body>
<table class="demo-table">
<tr><td>HTML</td></tr>
<tr><td>CSS</td></tr>
<tr><td>JavaScript</td></tr>
<tr><td>Python</td></tr>
</table>
</body>
</html>

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>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
.demo-button {
    background: #007BFF;
    color: white;
    border: none;
    padding: 12px 20px;
    cursor: pointer;
}
.demo-button:hover {
    background: #0056b3;
}
.demo-button:active {
    background: #003f7f;
}
.demo-input {
    padding: 10px;
    border: 2px solid gray;
    margin-top: 15px;
}
.demo-input:focus {
    border-color: #007BFF;
    outline: none;
    box-shadow: 0 0 0 3px rgba(0,123,255,.3);
}
</style>
</head>
<body>
<button class="demo-button">Submit</button>
<br>
<input class="demo-input" type="text" placeholder="Type something">
</body>
</html>

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.

Web Resources on CSS Pseudo Classes

1. MDN Web Docs : Pseudo-classes
2. Web.dev : Pseudo-classes
3. CSS Tricks.com : Meet the Pseudo Class Selectors
4. Dev.to : Learn CSS Pseudo-classes by Category

๐Ÿงช Test Your CSS Code

Edit the CSS code on the left and click “Run Code” to see the result on the right.

๐Ÿ“ CSS Code
๐Ÿ‘๏ธ Preview (HTML rendered with your CSS)

Click “Run Code” to see the result here.