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>
h2::beforeis the selectorh2, two colons, thenbefore. It means: an extra part at the start of the content of the<h2>.content: "New ";is what the sticker says: text inside quote marks. The space after New leaves a gap before the heading text.color: blue;only colours the sticker. The heading's own text stays as it was.
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
::beforeadds content at the start of an element.::afteradds content at the end of an element. For example,a::after { content: " »"; }puts a » after a link.::first-letterstyles the first letter, for example a big letter at the start of a paragraph.::first-linestyles the first line.
::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
- Text without quote marks. The browser ignores
content: →;. Writecontent: "→";. - A pseudo-element on
<img>or<input>. These two elements have no::beforeor::after, so the sticker doesn't show. - Important information in
content. Some screen readers don't read the text incontent, and that text can't be selected to copy. Usecontentfor decoration only. - Decoration that gets read aloud. The opposite problem is common too. A link that says "Read more", with
a::after { content: " »"; }, is read by a screen reader in Chrome as "Read more »". For decoration, give it an empty alternative text after a slash:content: " »" / "";. The » still shows, but it is not read aloud.
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).