Skip to the page
css reference · selectors

CSS ::before / ::after

type
pseudo-element
applies to
a generated box inside the element, before or after its content
inherits
n/a (it inherits from its element like a child would)
common values
content: ""; content: "→"; content: attr(data-label)

Where it goes

On the end of a selector with two colons: .card::before. Set content (required), then style it. Most layouts give it position: absolute so it sits over or beside the tag without pushing text around.

Example

.quote::before {
  content: "“";
  font-size: 2em;
  color: gray;
}
.card::after {
  content: "";
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.3);
}

Common mistakes

  • Forgetting content. Without it the box does not appear at all, even with a size and a background. content: "" is enough.
  • Putting real text or meaning in it. A screen reader (software that reads the page aloud) mostly skips it. Anything a visitor needs belongs in the HTML.
  • Using it on an <img> or <input>. Tags that cannot hold children cannot hold these either. Wrap them in a <span> and attach it there.

Related

Updated 24 October 2026. Practice it in the css route, or read more on MDN.