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.

Key distinction:
Pseudo-classes describe states (like :hover). Pseudo-elements describe parts (like ::before). Modern CSS uses single colon (:) for pseudo-classes and double colon (::) for pseudo-elements, though older browsers allow : for both.

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)
interactive-states.css Hover, focus, active, visited

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 formula 2n+1 works for any pattern.
  • :not(selector) โ€” Negation pseudo-class. Targets elements that do NOT match the selector.
structural-selectors.css First, last, nth-child, not

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.

โœ๏ธ Try It Yourself โ€” Button States & Lists
Preview

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: "";

pseudo-elements.css ::before, ::after, ::first-letter, ::first-line, ::selection, ::placeholder

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.

Browser compatibility note:
All major browsers support both : and :: for pseudo-elements. Use :: (double colon) for new code โ€” it's the CSS3 standard and makes the intent clearer.

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.

โœ๏ธ Try It Yourself โ€” ::before & ::after
Preview
Challenge: Style a Pricing Table

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 :hover effect that highlights rows on mouseover
  • ::before checkmarks on each feature
  • ::first-letter drop cap on a description paragraph
  • Custom ::selection highlight 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

Question 1 of 5
Which pseudo-class activates when you hover your mouse over an element?
Question 2 of 5
What does :nth-child(2n) select?
Question 3 of 5
How do you add decorative content before an element using CSS?
Question 4 of 5
What is the difference between : (single colon) and :: (double colon)?
Question 5 of 5
Which pseudo-element styles the highlight color when text is selected?
0
/5
Great work! You understand pseudo-classes and pseudo-elements.
Africa & India's Best AI Academy
๐Ÿค– AI-first ๐ŸŽ“ Placement Assistance

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 โ†’