Pseudo-elements — CSS

A pseudo-element is an extra part of an element that is made with CSS, without changing the HTML. It is written with two colons (::). ::before adds content at…

You want the word "New" in front of a heading on your page, and an arrow after a link. You can add both without changing the HTML at all.

It works like putting a sticker on the cover of a book: the book stays the same, but its cover has something extra. In CSS, the book is the HTML element, and the sticker is a pseudo-element: an extra part of an element that you create and style with CSS. A pseudo-element is written with two colons, for example h2::before.

Stick some text in front of a heading

This example has one heading and a rule that adds text in front of it:

<h2>Course Features</h2>

<style>
h2::before {
  content: "New ";
  color: blue;
}
</style>

The result: the heading reads "New Course Features", and the word "New" is blue. The HTML is still only <h2>Course Features</h2>.

content is required on ::before, and on ::after (an extra part at the end of the content). Guess: what appears in front of a paragraph if you only write p::before { color: red; }? Nothing. Without content, the browser doesn't create the sticker at all, even when its other styles are written. For a sticker with no text, such as a decorative line, write content: "";. The text in content also has to be inside quote marks: content: "New";, not content: New;.

The pseudo-elements you'll use most

::first-letter and ::first-line style text that is already there, so they don't need content.

One colon or two?

A pseudo-class, like a:hover, is written with one colon. It selects the whole element in a certain situation: a link that is being hovered. A pseudo-element, like a::before, is written with two colons. It selects one part of the element: a sticker in front of the link's content.

Browsers still accept :before with one colon, so that old code keeps working. But the correct way to write it is ::before. Browsers don't recognise other forms, like :before:, @before or #before.

⚠️ Stickers that don't show, or can't be read

TL;DR: A pseudo-element is an extra part of an element that is made with CSS, without changing the HTML. It is written with two colons (::). ::before adds content at the start of an element, and ::after at the end. What they add is written in the content property, inside quote marks. content is required, even when it is empty (""). ::first-letter and ::first-line style text that is already there. One colon means a pseudo-class (an element in a certain situation), two colons mean a pseudo-element (a part of an element).

⚡ A sticker in front of the heading

Write:

h2::before {
  content: "New ";
  color: blue;
}

Meaning: h2::before is a pseudo-element: an extra part at the start of the content of the <h2>, made with CSS, like a sticker put in front of the heading. What the sticker says is written in content. The HTML <h2>Course Features</h2> does not change, but the heading reads "New Course Features". ::before adds content at the start of an element, ::after at the end.

⚡ No content, no sticker

Guess first: what appears if you only write p::before { color: red; }?

Nothing. Without content, the browser doesn't create the sticker at all.

Meaning: ::before and ::after need content. The text in content goes inside quote marks: content: "New";. For a sticker with no text, write content: "";.

⚡ One colon or two

Meaning: One colon means a pseudo-class: an element in a certain situation, for example a:hover (a link that is being hovered). Two colons mean a pseudo-element: a part of an element, for example a::before (a sticker in front of the link's content).

What you will learn