Web Analytics

CSS Pseudo Elements

Intermediate ~9 min read

What are Pseudo Elements?

Pseudo-elements style specific parts of an element or insert content. They use double colons :: (single colon also works for legacy support).

Before & After

::before and ::after insert content before or after an element. Requires the content property!

HTML
CSS
JS

Decorative Icons

Use ::before to add icons or decorative elements without cluttering HTML.

HTML
CSS
JS

First Letter

::first-letter styles the first letter of a block element. Perfect for drop caps!

HTML
CSS
JS

First Line

::first-line styles the first line of text in a block element.

HTML
CSS
JS

Clearfix with After

A classic use of ::after is the clearfix hack for containing floated elements.

HTML
CSS
JS
Important: ::before and ::after MUST have a content property, even if it's empty: content: '';

Summary

  • ::before - Insert content before element
  • ::after - Insert content after element
  • ::first-letter - Style first letter
  • ::first-line - Style first line
  • content property is required for ::before/::after
  • Use for decorative elements, not essential content

Quick Quiz

Which property is required for ::before and ::after?

A
display
B
content
C
position
D
text