CSS Pseudo-classes and Pseudo-elements

Sometimes, you need to style elements in specific states or style only part of an element without adding extra HTML. That’s where pseudo-classes and pseudo-elements come in. They give you extra control over styling, making your designs more interactive and polished.

What Are Pseudo-classes?

Pseudo-classes define a special state of an element – like when a user hovers over a link, or when a form input is focused.

Syntax:

selector:pseudo-class {
  property: value;
}

Common Pseudo-classes

  • :hover → When the mouse pointer is over an element
  • :active → When an element is being clicked
  • :focus → When an input field is active
  • :first-child → Selects the first child of a parent
  • :last-child → Selects the last child

Example:

a:hover {
  color: red;
}
input:focus {
  border: 2px solid blue;
}

What Are Pseudo-elements?

Pseudo-elements let you style specific parts of an element (like the first letter, or content before/after an element).

Syntax:

selector::pseudo-element {
  property: value;
}

Common Pseudo-elements

  • ::before → Adds content before an element
  • ::after → Adds content after an element
  • ::first-letter → Styles the first letter of text
  • ::first-line → Styles the first line of text

Example:

p::first-letter {
  font-size: 2em;
  color: green;
}

p::after {
  content: " ✔";
  color: blue;
}

Pseudo-class vs. Pseudo-element

  • Pseudo-class (:) → Targets a state (hover, focus, active).
  • Pseudo-element (::) → Targets a part of the element (first-letter, before, after).

👉 Think of pseudo-classes as conditions, and pseudo-elements as sub-parts.

Combining Pseudo-classes and Pseudo-elements

You can even combine them for advanced styling.

Example:

a:hover::after {
  content: " (click me)";
  color: gray;
}

👉 Adds “(click me)” only when the link is hovered.

Common Mistakes Beginners Make

  • Using single colon : for pseudo-elements (modern syntax is ::).
  • Forgetting to add content for ::before and ::after.
  • Overusing pseudo-elements instead of adding meaningful HTML.
  • Confusing pseudo-classes (:hover) with pseudo-elements (::before).

Quick Reference Summary

SelectorTypeExampleDescription
:hoverPseudo-classa:hoverStyle on mouse hover
:activePseudo-classbutton:activeStyle when clicked
:focusPseudo-classinput:focusStyle active input
:first-childPseudo-classli:first-childSelect first list item
::beforePseudo-elementp::beforeAdd content before
::afterPseudo-elementp::afterAdd content after
::first-letterPseudo-elementp::first-letterStyle first letter
::first-linePseudo-elementp::first-lineStyle first line

Practice Challenge

  1. Create a list of 5 items.
    • Style the first item using :first-child (make it bold).
    • Style the last item using :last-child (make it italic).
  2. Create a paragraph.
    • Make the first letter larger and red using ::first-letter.
    • Add “Read more…” after the paragraph using ::after.

Conclusion

Pseudo-classes and pseudo-elements make CSS much more powerful by letting you style states and sub-parts of elements without extra HTML. They are essential for creating interactive and elegant designs.

Sharing Is Caring: