designwithfocus

CSS

How to Add Pseudo Element in CSS With the ::before Hook

By Design With Focus Team 7 min read
A button with a small decorative mark drawn by a CSS pseudo-element

Short answer

Add a pseudo-element by writing ::before or ::after on a selector and setting content to something other than none. The box is the first or last child of that element, and it is inline unless you change display. If content is missing, normal, or none, the box is not rendered. Use it for decoration. Put real words in the HTML.

Last verified against the sources listed below.

Key takeaways

  • ::before is the first child of the element. ::after is the last child. Both need a content value to render.
  • MDN marks ::before and ::after widely available since July 2015. Browsers still accept the older :before form.
  • An empty content string can draw a shape with borders. It does not insert readable text.
  • Alternative text can follow a slash. A Working Draft allows an empty string so decorative text is not spoken.
  • Generated content is a poor home for words people must read. MDN says screen readers may skip it.

How to add pseudo element in css is a selector plus a content value. MDN’s ::before page says ::before creates a pseudo-element that is the first child of the selected element. It is inline by default, and it is there to add cosmetic content.

Nothing appears until content is set. If the property is missing, invalid, normal, or none, MDN says the box is not rendered. The steps below are the smallest version that actually shows up.

What do you need before a pseudo-element appears?

You need an element in the HTML, a stylesheet the page loads, and a selector that is not a replaced element. MDN says ::before and ::after cannot be applied to replaced elements such as images, because those contents come from outside the document and ignore the page’s styles.

You also need the double-colon name. Selectors Level 3 introduced ::before to distinguish pseudo-elements from pseudo-classes. Browsers still accept :before from CSS2.

MDN marks ::before widely available since July 2015. ::after has the same date.

An img is the wrong hook. The HTML spec says the img element represents an image embedded in the document. Those pixels come from the resource, which is why a pseudo-element does not restyle them.

If content points at an image file, that file is still a download. web.dev says images often account for most of the bytes on a page. Keep a decorative icon small.

The CSS Pseudo-Elements Module defines ::before as a styleable child immediately before the element’s actual content, and ::after as the child immediately after it. Both are fully styleable. There is no restricted property list. The initial value of display is inline, so the generated box sits in the line unless you change it.

How do you add a ::before pseudo-element?

Pick the element, name ::before, and set content. One action per step:

  1. Choose a real element, such as a button or a paragraph, not an image.
  2. Write a rule with ::before on that selector.
  3. Set content to a string, an image, or an empty string if you will draw the shape yourself.
  4. Add the properties that make the box visible, such as color, borders, or padding.
  5. Reload the page and confirm the mark is the first thing inside the element.
  6. If the words matter, move them into the HTML and leave the pseudo-element decorative.

The spec’s own example inserts a label this way:

p.note::before {
  content: "Note: ";
}

MDN’s checklist example uses an empty string instead, then draws a check with borders. The empty string still generates the box. It does not generate a word. That is the hook for an icon or an underline, not a sentence.

The CSS Button Generator builds the button rules in the browser. After you have a button, the same ::before hook can hold a mark or an underline. The generator does not insert the pseudo-element for you.

How do ::before and ::after differ?

::before is the first child. ::after is the last. MDN’s ::after page uses the same content rule: without a usable content value, the box is not rendered.

Piece ::before ::after
Where it sits First child of the element Last child of the element
Default display Inline Inline
Needs content Yes, or it is not rendered Yes, or it is not rendered
Paint order without z-index Under ::after Above ::before
Screen reader reliability Poor for real text Poor for real text

With no z-index, MDN says ::after appears above ::before because it is rendered later in the flow. They share the parent’s stacking context until you set z-index.

Both are suppressed when the originating element is replaced. You cannot hang a caption on an img with img::after and expect the spec’s generated child. Put the caption in HTML next to the image.

That split is what HTML is used for. The words stay in the document.

What CSS is used for is the paint, including the generated box. The pseudo-element does not replace the element that holds the label.

What belongs in the content property, including an empty alt?

content decides whether the pseudo-element exists and what it shows. MDN’s content page says that for ::before and ::after, normal computes to none. none means the pseudo-element is not generated.

Usable values include a quoted string, a url() image, counter(), attr(), and the quote keywords. Strings are concatenated. You cannot drop an HTML tag inside the string. MDN’s unicode example uses a backslash and hex digits because generated content is CSS, not markup.

Alternative text goes after a slash. MDN shows forms such as a symbol followed by "games", or an image URL followed by a quoted label. The text is meant for speech. It may also show up in some browsers.

The CSS Generated Content Module is a W3C Working Draft dated 4 December 2025, and the draft says it is a work in progress. It describes the same slash. An empty string as alternative text lets speech output skip decorative text.

Its image example is a star with the alt text “New!”. If the mark is decorative and the name lives elsewhere, the draft says an empty alt avoids reading the decoration aloud.

Use a real alt string when the icon carries a meaning that is not already in the element. Use the empty string only when the decoration repeats something the HTML already says.

Why is generated content a bad place for real information?

MDN’s accessibility note on ::before is direct: using it to add content is discouraged, because it is not reliably accessible to screen readers. The ::after page says the same thing.

The content property page explains the mechanism. CSS-generated content is not included in the DOM, so it is not represented in the accessibility tree for every browser and assistive-technology pair. If the information is critical, put it in the document. If the inserted content is not decorative, check that the information is still available with CSS turned off.

A quote mark, a colored underline, or a check drawn with borders can live in a pseudo-element. A button name, an error, or a price should not. The empty-string checkmark works because the list item text is already in the HTML. The pseudo-element only paints the mark.

What should you check when the pseudo-element is missing?

What you see Likely cause What to do
No box at all content is omitted, normal, or none Set content to a string or to ""
Rule seems ignored Selector is a replaced element such as img Put the text in HTML beside the image
Icon with no accessible name Image content has no slash text Add alt text after a slash, or keep the icon decorative
Words vanish with CSS off The only copy was in content Move the words into the element
::before covers the label Both boxes are in the line, or z-index is unset Change display or position, and set z-index if they overlap
Old sheet uses :before CSS2 single-colon form Leave it, or rewrite it as ::before

Start with content, then the selector, then whether the words were supposed to be in the HTML. The pseudo-element is a paint hook. It is not a second document.

Frequently asked questions

Why does the pseudo-element not show up?

MDN says a ::before or ::after box is not rendered when content is missing, invalid, normal, or none. It then behaves as if display were none. Add a content value, even an empty string if you only need a box to style with borders. Also check the selector. These pseudo-elements do not apply to replaced elements such as images, because the image contents come from outside the document.

Should you write ::before or :before?

Write the double colon. MDN says Selectors Level 3 introduced ::before so pseudo-elements look different from pseudo-classes. Browsers still accept the single-colon :before form from CSS2. New style sheets should use the double colon. The single colon is what you will see in older files, and it still matches the same ::before box.

Can a pseudo-element hold the only copy of a label?

It should not. MDN says using ::before or ::after to add content is discouraged because screen readers do not reliably announce it. The content property page adds that generated content is not in the DOM, so some browser and assistive-technology pairs will not speak it. If the words matter when CSS is off, put them in the HTML and use the pseudo-element for a mark that can disappear.

What is an empty alt on generated content?

The CSS Generated Content Module, a Working Draft dated 4 December 2025, lets you put alternative text after a slash. An empty string tells speech output to skip decorative text. The draft's example is a star image with the alt text New. MDN documents the same slash form, such as a string or a url followed by a slash and a quoted label. Treat the empty string as the decorative case, and do not rely on it for a required name.

Does ::after cover ::before?

They share the parent's stacking context unless you set z-index. MDN says that, with no z-index, ::after paints above ::before because it is generated later. Both are inline by default. The CSS Pseudo-Elements spec says there is no limit on which properties apply, so you can position either box. Positioning does not put the text into the HTML.

Sources