- CSS Tutorial
- CSS Introduction
- CSS Syntax
- CSS Comments
- CSS Selectors
- CSS Fonts
- CSS Colors
- CSS Backgrounds
- CSS Box Model
- CSS Borders
- CSS Margins
- CSS Padding
- CSS Text
- CSS Images
- CSS Links
- CSS Lists
- CSS Tables
- CSS Outline
- CSS Icons
- CSS Display
- CSS max-witdh
- CSS Position
- CSS z-index
- CSS Overflow
- CSS Float
- CSS Align
- CSS Opacity
- CSS Navigation Bar
- CSS Dropdowns
- CSS Forms
- CSS Units
- CSS !important
- CSS Specificity
- CSS Combinators
- CSS inline-block
- CSS Hover
- CSS Cursors
- CSS Selectors
- CSS Type Selector
- CSS Class Selector
- CSS ID Selector
- CSS Attribute Selector
- CSS Pseudo-class Selector
- CSS Pseudo-element Selector
- CSS Universal Selector
- CSS Advanced
- CSS Text Formatting
- CSS Gradients
- CSS Shadow
- CSS Rounded Corners
- CSS Text Effects
- CSS 2D Transform
- CSS 3D Transform
- CSS Border Images
- CSS Inherit
- CSS Transitions
- CSS Animations
- CSS Box Sizing
- CSS Tooltip
- CSS Masking
- CSS Pagination
- CSS Styling Images
- CSS object-fit
- CSS object-position
- CSS Buttons
- CSS Multiple Columns
- CSS Variables
- CSS Flexbox
- CSS Grid
- CSS Media Queries
CSS Pseudo Class Selector
In standard CSS, you target elements based on their names, IDs, or classes. However, sometimes you need to style an element based on information that isn't strictly in the HTML code—such as whether a user is hovering over a button, if a link has been clicked, or if an input field is currently active. This is where Pseudo-class selectors come in.
A pseudo-class is a keyword added to a selector that specifies a special state of the selected element. They allow you to create dynamic, interactive user interfaces without needing to write complex JavaScript for simple visual changes.
The syntax for pseudo-classes uses a single colon (:) between the selector and the pseudo-class keyword:
selector:pseudo-class {
property: value;
}
Here are some common pseudo-classes and their uses in modern web development:
:hover: Applies when the user moves their mouse over an element. This is the most common way to provide visual feedback for buttons and links.:focus: Applies when an element (like an input field or a button) is selected via the keyboard or a mouse click. This is critical for accessibility.:active: Applies during the exact moment an element is being clicked or activated. It provides an immediate "pressed" sensation.:visitedand:link: Used specifically for navigation.:linktargets unvisited URLs, while:visitedstyles links the user has already seen.:first-child: A structural pseudo-class that targets an element only if it is the very first sibling inside its parent container.:lang(): Selects elements based on thelangattribute, which is useful for applying specific fonts or styles to different languages (e.g., Japanese vs. English text).
:visited pseudo-class. Usually, you can only change colors to prevent websites from "sniffing" a user's browsing history.
Practical Example: Interactive Links
A classic real-world use case is making a navigation menu feel responsive. By using :hover, you give the user a clear signal that an item is clickable.
CSS
/* The base state of the link */
a {
color: black;
transition: color 0.3s ease;
}
/* The state when the mouse is over the link */
a:hover {
color: blue;
text-decoration: underline;
}
:link — :visited — :hover — :active. If you put :hover before :visited, the visited style will override the hover style!
Combining Pseudo-Classes with Other Selectors
Pseudo-classes are incredibly powerful because they can be combined with class selectors, IDs, or even other pseudo-classes to increase specificity. For instance, you might want to style only a specific "Call to Action" button differently when it is hovered:
CSS
/* Only affects tags with the class "highlight" */
a.highlight:hover {
background-color: yellow;
color: red;
font-weight: bold;
}
:hover doesn't work the same way on touchscreens. If you rely solely on hover effects to show important information (like a tooltip), mobile users may never see it.
Real-World Example: Form Validation
You can also use pseudo-classes like :focus to improve the user experience of forms. By highlighting the active input field, you help the user stay oriented.
CSS
input:focus {
border: 2px solid #4A90E2;
outline: none;
background-color: #f0f7ff;
}
This simple addition makes your forms feel much more professional and accessible to users navigating with a keyboard.