← Back to CSS
29

Pseudo-elements `::before` and `::after`: effects, badges and modern patterns

Use `::before` and `::after` to build decorations, overlays and UI details without adding extra HTML.

📘 Theory

Fundamentals: `content` and flow

Without `content`, the pseudo-element does not exist.

1

`::before` and `::after` behave like inline elements by default.

2

Set `content: ""` when you want an empty box that can still be styled and positioned.

3

You can switch them to `block` or `inline-block` depending on the pattern you are building.

Positioning and layers

Absolute positioning unlocks overlays and more expressive decoration.

Give the parent `position: relative` so the pseudo-element has a clear anchoring context.

Use `inset` to cover the whole element when creating overlays or ambient highlights.

`pointer-events: none` helps decorative layers avoid blocking clicks or hover on the real component.

  • Handle `z-index` carefully so you do not bury useful content.
  • Combine opacity and blur for softer surfaces.
  • Avoid overlays that reduce readability.

`attr()`: pulling data from HTML

Great for lightweight labels, states and tiny tooltips.

1

`attr()` can read `data-*` attributes and turn them into small interface labels.

2

That makes it especially useful for badges, state labels or simple helper text where the real content already exists in the markup.

3

Keep the text short so it remains readable and does not overflow awkwardly.

Decorative effects and microinteractions

Underlines, glows and separators often belong here.

1

Pseudo-elements are perfect for animated underlines, guide lines and small highlight treatments that would otherwise clutter the markup.

2

Use `transform` and `transition` for smooth movement, and combine gradients with opacity if you want a more contemporary feel.

Best practices

Treat them as decoration, not as the primary source of meaning.

1

Important content should still live in the HTML for accessibility and SEO.

2

Screen readers do not reliably expose pseudo-element text as primary content.

3

If a pseudo-element adds color or visual emphasis, keep contrast high enough that the effect still supports readability.

🧪 Learn by doing

Example Card badge Add a label without injecting extra markup into the card.
Example Decorative quote mark Add a large opening quote with `::before`.
Example Animated underline A modern hover effect built with one pseudo-element.
Example Tooltip with `attr()` Use a `data-tip` attribute to surface helper text.
Example Interactive demo: advanced hover buttons Explore several hover patterns using `::before`, `::after`, layered effects and transitions for more expressive CTAs.

🏁 Challenges

Challenge Challenge: build a ribbon Create a decorative ribbon using `::before` and `::after`.

🧰 Resources

Test

Check your knowledge with a test about CSS.

Test for CSS

What is this?

I'm Cristian Eslava and I sometimes build websites so both you and I can learn and experiment. culTest

I made this in February 2026 to make learning easier for my students. The idea is to learn web development by practicing and to keep expanding the project with new topics, tests and challenges.

It draws inspiration from MDN, W3Schools, CodePen, Manz and many other web development references. I wanted to combine useful theory, runnable examples, challenges and the testing system I had already built for culTest. culTest

If you liked it, if you didn't, or if you want to get in touch, write to me at cristianeslava@gmail.com