What are pseudo-classes and pseudo-elements?
Pseudo-classes are keywords that describe a state or position of an element. They don't exist in the HTML โ they're detected by CSS. For example, :hover activates when you move your mouse over an element, :focus activates when an element is selected, and :nth-child(2) targets the second child of its parent.
Pseudo-elements represent parts of an element that can be styled independently. For example, ::before and ::after create generated content before and after an element's content, ::first-letter styles just the first letter, and ::selection styles the highlight color when text is selected.
Interactive pseudo-classes
These pseudo-classes respond to user interactions:
:hoverโ Activates when the mouse hovers over an element:focusโ Activates when an element is focused (e.g., clicked on a button or input field):activeโ Activates while an element is being clicked (the instant between mousedown and mouseup):visitedโ Applies to links the user has already clicked (limited by browsers for security)
The transition: all 0.2s; makes state changes smooth instead of instant. This is especially important for interactive elements โ it makes your UI feel polished and responsive.
Structural pseudo-classes
These pseudo-classes target elements based on their position in the DOM:
:first-childโ Targets the first child of its parent:last-childโ Targets the last child of its parent:nth-child(n)โ Targets the nth child (e.g.,:nth-child(3)is the 3rd child):nth-child(odd)โ Targets every odd-numbered child (1st, 3rd, 5th...):nth-child(even)โ Targets every even-numbered child (2nd, 4th, 6th...):nth-child(2n+1)โ Same as odd. The formula2n+1works for any pattern.:not(selector)โ Negation pseudo-class. Targets elements that do NOT match the selector.
These selectors are powerful for styling lists, tables, and grids without needing extra HTML classes.
Try it yourself: Interactive button states
Click and hover over the buttons, and interact with the list to see pseudo-classes in action. Edit the CSS to change colors, add effects, or modify the list styling.
Pseudo-elements: ::before and ::after
Pseudo-elements create new elements in CSS without adding HTML. The most common are ::before and ::after, which insert content before and after an element's content.
Every pseudo-element requires the content property. Even if you want an empty element, use content: "";
Important: ::before and ::after require the content property. Without it, nothing displays. You can use text, emojis, or even leave it empty (content: "";) if you just want to style the space.
Single colon (:) vs double colon (::)
Modern CSS distinguishes between pseudo-classes and pseudo-elements with syntax:
- Pseudo-classes use single colon (:) โ
:hover,:focus,:nth-child(),:not() - Pseudo-elements use double colon (::) โ
::before,::after,::first-letter,::selection
However, for backwards compatibility with older browsers, many developers still write :before and :after with a single colon, and browsers accept both. The double colon is the modern standard, and that's what you should use going forward.
Try it yourself: Decorative pseudo-elements
See ::before and ::after in action. Try changing the content, adding emojis, or modifying the styling. Select text to see the custom ::selection color.
Task: Create a pricing table with striped rows, hover effects, and decorative elements. Include:
- A list of features with alternating row colors using
:nth-child(odd)and:nth-child(even) - A
:hovereffect that highlights rows on mouseover ::beforecheckmarks on each feature::first-letterdrop cap on a description paragraph- Custom
::selectionhighlight color
Use one of the editors above to build this table, or extend it in your own project.
Hint: Start with an unordered list <ul>. Style odd rows with li:nth-child(odd), add hover effects with li:hover, and use li::before { content: 'โ '; } to add checkmarks. Add a paragraph with some description text and use p::first-letter to make the first letter larger and a different color.
Key takeaways
- Pseudo-classes describe states:
:hover,:focus,:active,:nth-child(),:not() - Pseudo-elements represent parts:
::before,::after,::first-letter,::selection - ::before and ::after require
content: Without it, nothing displays. - Pseudo-elements create "free" elements: You can add decorative content, checkmarks, icons, and more without extra HTML.
- Use : for pseudo-classes, :: for pseudo-elements: This is the modern CSS3 syntax. Older : syntax still works but :: is preferred.
- Structural selectors are powerful:
:nth-child(),:first-child,:last-child,:not()let you style elements based on position without extra classes.
Test your knowledge
You just mastered pseudo-classes and pseudo-elements.
Imagine what you'll build next.
Join the Best AI Programs โ three 5-month courses, fully online, with placement assistance. Week 1 is free.
Explore our AI-era courses โ