⬅ Back

PSEUDO-ELEMENTS

Decorative effects on a website are not always large images or bright visual blocks.

Sometimes they are very small and simple details, for example:

Very often, these decorative elements need to appear exactly:

For that, CSS uses pseudo-elements.

1. What Pseudo-elements Are

Pseudo-elements let us add decorative parts to an element without creating extra empty tags in HTML.

The two most common pseudo-elements are:

They are used to create decorative content connected to an existing element.

Main idea:

So instead of adding extra <span> or <div> tags only for decoration, we can use pseudo-elements in CSS.

2. ::before and ::after

::before creates a pseudo-element before all the content of an element.

::after creates a pseudo-element after all the content of an element.

Basic Structure

.box {
  /* element styles */
}

.box::before {
  /* styles for the before pseudo-element */
}

.box::after {
  /* styles for the after pseudo-element */
}

This means:

3. Default Behavior

By default, pseudo-elements are inline elements.

That means:

So if you want a pseudo-element to have:

you usually need to change its display type.

Common options:

Example

.box::before {
  content: "";
  display: block;
  width: 40px;
  height: 4px;
  background-color: orange;
}

Here the pseudo-element becomes a small decorative line.

4. The content Property

The content property is required for ::before and ::after.

Without content, the browser does not create the pseudo-element.

This is one of the most important rules.

Example

.box::before {
  content: "Hello";
}

Here is the exmaple. I created div number:2,3,4,5,6. Ihave not created div number 1 and numbet 7. I used before and after pseudo-elements. Now it is looking as div1 and 7 are exist, but not it looks the same, thanks to css and pseudo-elements

Second
Third
Fourth
Fifth
Sixth

Here is the code:

<div class="exercise1-pseudo-elements">
  <div class="parent-box-pseudo-elements">
    <div class="second-box-pseudo-elements">Second</div>
    <div class="third-box-pseudo-elements">Third</div>
    <div class="fourth-box-pseudo-elements">Fourth</div>
    <div class="fifth-box-pseudo-elements">Fifth</div>
    <div class="sixth-box-pseudo-elements">Sixth</div>
  </div>

  <style>
    .parent-box-pseudo-elements {
      display: flex;
      gap: 10px;
    }

    .second-box-pseudo-elements::before {
      content: "First";
      margin-right: 10px;
    }

    .sixth-box-pseudo-elements::after {
      content: "Seventh";
      margin-left: 10px;
    }
  </style>
</div>

If no text content is needed, use an empty string:

.box::before {
  content: "";
}

This creates the pseudo-element, even though it does not contain visible text.

So:

5. Example with HTML

HTML:

<div class="box">
  Lorem, ipsum dolor sit amet consectetur adipisicing elit. Officia itaque quia
  nobis fugit amet adipisci, corrupti animi, iusto eius a sed totam voluptas
  porro. Dolorem aliquid rerum magnam eligendi aspernatur.
</div>

CSS example:

.box::before {
  content: "“";
  font-size: 30px;
  color: orange;
}

.box::after {
  content: "”";
  font-size: 30px;
  color: teal;
}

What happens:

This is useful for quote blocks or decorative typography.

6. Where Pseudo-elements Are Used

Pseudo-elements are mainly used for decorative effects.

Common examples:

Example

.title::after {
  content: "";
  display: block;
  width: 60px;
  height: 3px;
  background-color: currentColor;
  margin-top: 8px;
}

Meaning:

7. Important Selector Rule

In one selector, you can use only one pseudo-element.

And it must come after a simple selector such as:

Correct:

.box::before {
  content: "";
}

Also correct:

p.note::after {
  content: "";
}

Not correct:

.box::before::after {
  content: "";
}

Why?

Because one selector cannot target two pseudo-elements in a chain like that.

8. Shared Styles for ::before and ::after

Sometimes we want both pseudo-elements to share the same styles.

Wrong way:

.box::before::after {
  font-size: 30px;
}

This is incorrect.

Correct way:

.box::before,
.box::after {
  font-size: 30px;
}

Then we can define separate content and colors:

.box::before {
  content: "This text is in before";
  color: orange;
}

.box::after {
  content: "This text is in after";
  color: teal;
}

This is the proper pattern:

9. Full Example

HTML:

<div class="box">
  Decorative pseudo-elements example
</div>

CSS:

.box {
  padding: 20px;
  border: 1px solid #ccc;
}

.box::before,
.box::after {
  font-size: 30px;
}

.box::before {
  content: "«";
  color: orange;
  margin-right: 8px;
}

.box::after {
  content: "»";
  color: teal;
  margin-left: 8px;
}

What happens:

10. Empty Decorative Pseudo-elements

Very often pseudo-elements do not contain text at all.

Instead, they are used as empty decorative blocks.

Example

.button::before {
  content: "";
  display: inline-block;
  width: 10px;
  height: 10px;
  background-color: red;
  border-radius: 50%;
  margin-right: 8px;
}

Meaning:

This is a very common pattern.

Another example:

.card::after {
  content: "";
  display: block;
  width: 100%;
  height: 2px;
  background-color: #ddd;
  margin-top: 16px;
}

Meaning:

11. Why Pseudo-elements Are Useful

Pseudo-elements are useful because they:

Instead of this:

<div class="title">
  <span class="decor-line"></span>
  Heading
</div>

we can often do this:

.title::before {
  content: "";
  display: inline-block;
  width: 20px;
  height: 2px;
  background: black;
}

So the HTML stays simpler.

12. Important Warning

Do not use pseudo-elements to add real textual content.

Pseudo-elements are intended only for decorative effects.

Why is this important?

Because text inside pseudo-elements:

So if the text is important for meaning, it should be written in HTML, not in ::before or ::after.

Good use:

Bad use:

13. Good and Bad Examples

Good example:

.quote::before {
  content: "“";
  color: gray;
}

Why good?

Because the quote mark is decorative.

Bad example:

.price::before {
  content: "Special offer: ";
  color: red;
}

Why bad?

Because "Special offer" is meaningful text and should be in HTML.

Correct version:

<p class="price">Special offer: $19</p>

Then CSS can style it normally.

14. Practical Examples

Example 1. Decorative dot before text

.list-item::before {
  content: "";
  display: inline-block;
  width: 6px;
  height: 6px;
  background-color: black;
  border-radius: 50%;
  margin-right: 8px;
}

Meaning:

Example 2. Decorative line after heading

.heading::after {
  content: "";
  display: block;
  width: 50px;
  height: 3px;
  background-color: orange;
  margin-top: 10px;
}

Meaning:

Example 3. Quotation marks around text

.quote::before {
  content: "“";
}

.quote::after {
  content: "”";
}

Meaning:

15. Short Summary

::before

::after

content

Default behavior:

If you need size or block-like shape:

Main use:

16. Mini Cheat Sheet

::before   -> before the content
::after    -> after the content
content    -> required
""         -> empty decorative pseudo-element
display:block / inline-block -> gives size and geometry

17. Very Short Practical Rules

Use pseudo-elements for decoration, not for real content.
Always remember: no content = no pseudo-element.

18. Common Mistakes

Mistake 1: Forgetting content.

Problem: the pseudo-element will not appear.

Mistake 2: Trying to give width and height to a default inline pseudo-element.

Problem: it may not behave as expected.

Solution: use display: block or display: inline-block.

Mistake 3: Using pseudo-elements for meaningful text.

Problem: that text is not dependable as real content.

Mistake 4: Trying to write:

.box::before::after

Problem: this is invalid for shared styling.

Solution: write:

.box::before,
.box::after

19. Final Summary

Pseudo-elements ::before and ::after let us add decorative effects before or after an element without creating extra tags in HTML.

They are very useful for:

Important things to remember:

Super short memory line:

::before = before, ::after = after, content = required, pseudo-elements = decoration only
⬅ Back