Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

What is the difference between ::before and ::after generated content?

Quick answer

Both ::before and ::after create generated content as immediate children of a selected element. ::before places it before that element’s content; ::after places it after. They’re useful for decoration, such as a small icon or quotation mark. Because generated content may not be available to assistive technology, put essential information in the HTML instead. [S1] [S2]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I’m choosing between ::before and ::after. What’s the actual difference?

Geez

The difference is their position relative to the selected element’s content: ::before comes first, and ::after comes last. Both are generated as immediate children of that element; neither means “before or after the element in the page.” [S1]

For example, if a paragraph contains Read this, a ::before decoration appears before those words within the paragraph, while an ::after decoration appears after them. Use whichever position fits the presentation. The choice does not make one pseudo-element more suitable for important information than the other.

Follow-up question

Could you show that with markup and CSS? I want a small label after a link.

Geez

Assuming the label is only a visual flourish and not information readers need, you could write:

<a class="guide-link" href="YOUR-DESTINATION">Read the guide</a>
.guide-link::after {
  content: " →";
}

The text Read the guide is in the HTML; the arrow is generated after it. The CSS content property supplies the generated text for the pseudo-element. [S2] Replace YOUR-DESTINATION with the link’s actual destination. If you want to put the decoration before the link text instead, change ::after to ::before.

Follow-up question

What if I want to add “opens in a new window” after the link instead of an arrow?

Geez

Put that warning in the HTML, not only in ::after. It conveys information about what happens when someone follows the link, so it should remain available as document content. Generated content may not be represented in the accessibility tree or announced by some combinations of browsers and assistive technology; it is also not part of the DOM. [S2] The source does not establish exactly how every browser or assistive technology will handle a particular generated string. When information is critical, include it in the main document and ensure it remains available if CSS is disabled. [S2]

For example, add a visible label or other appropriate HTML content to communicate the warning. A CSS-generated version could be an optional visual repeat, but should not be the only place the warning exists.

Follow-up question

Can you give me a quick way to decide whether to use generated content at all?

Geez

Ask whether someone needs the content to understand the link or page. If yes, put it in the HTML; if it is purely decorative, ::before or ::after may be a reasonable fit. MDN gives cosmetic examples such as icons and quotation marks. [S1]

A practical test is to read the page with the decoration removed: if the message is incomplete or misleading, make that message document content instead. You can still use a pseudo-element for optional visual styling. This is a decision aid, not a guarantee about how every assistive-technology setup will present generated content. The key distinction is placement; neither pseudo-element makes essential information more dependable than the other.

A practical checklist

  • Choose ::before for decoration placed before the selected element’s content, or ::after for decoration placed after it. [S1]

  • Keep essential labels, warnings, and instructions in the HTML rather than only in generated content. [S1] [S2]

  • Check whether the page’s meaning remains clear when CSS is disabled. [S2]

  • Use generated text for optional visual details, such as a decorative arrow or quotation mark. [S1]

  • Consider how the decoration fits the surrounding content; do not rely on it to carry a message people need.

A few more useful answers

Are `::before` and `::after` inserted into the HTML?

No. Their content is generated by CSS and is not part of the DOM. [S2] Keep information readers need in the document itself.

Can `::before` or `::after` be used on an image?

The cited reference says these pseudo-elements cannot be applied to replaced elements such as <img>. [S1] This guide’s example uses a link instead.

What does `content: none` do?

For these pseudo-elements, content: none prevents the pseudo-element from being generated; normal computes to none for ::before and ::after. [S2]

Sources

References used for this guide.

  1. [S1] MDN: ::after CSS pseudo-element
    developer.mozilla.org · Checked
  2. [S2] MDN: CSS content property and generated-content accessibility
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work