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
Selector | Type | Example | Description |
---|---|---|---|
:hover | Pseudo-class | a:hover | Style on mouse hover |
:active | Pseudo-class | button:active | Style when clicked |
:focus | Pseudo-class | input:focus | Style active input |
:first-child | Pseudo-class | li:first-child | Select first list item |
::before | Pseudo-element | p::before | Add content before |
::after | Pseudo-element | p::after | Add content after |
::first-letter | Pseudo-element | p::first-letter | Style first letter |
::first-line | Pseudo-element | p::first-line | Style first line |
Practice Challenge
- 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).
- Style the first item using
- Create a paragraph.
- Make the first letter larger and red using
::first-letter
. - Add “Read more…” after the paragraph using
::after
.
- Make the first letter larger and red using
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.