Adding a background image with the CSS ::after pseudo-element is a powerful technique that allows developers to create decorative effects, overlays, icons, and visual enhancements without adding extra HTML elements. Whether you’re building a hero section, creating image overlays, or adding custom icons to buttons, the ::after pseudo-element can help keep your markup clean while improving design flexibility.

In this guide, you’ll learn how to add a background image using CSS ::after, understand how pseudo-elements work, troubleshoot common issues, and explore practical real-world examples.

What Is the CSS ::After Pseudo-Element?

The ::after pseudo-element creates generated content that appears after the content of an HTML element.

Basic syntax:

.element::after {
    content: "";
}

Unlike regular HTML elements, pseudo-elements are created entirely with CSS. They are commonly used for:

One of the biggest advantages of using ::after is that you don’t need to add unnecessary HTML elements just for styling purposes.

Subscribe to our Newsletter

Stay updated with our latest news and offers.
Thanks for signing up!

How Does a Background Image Work in CSS ::After?

To display a background image inside a pseudo-element, you must:

  1. Create the pseudo-element with content.
  2. Define width and height.
  3. Position the element.
  4. Apply the background image.

Basic example:

.box {
    position: relative;
}

.box::after {
    content: "";
    position: absolute;
    width: 150px;
    height: 150px;
    background-image: url("image.png");
    background-repeat: no-repeat;
    background-size: contain;
}

Without the content property, the pseudo-element will not appear.

Complete Example: Adding a Background Image Using CSS ::After

HTML:

<div class="box">
    Sample Content
</div>

CSS:

.box {
    position: relative;
    padding: 40px;
}

.box::after {
    content: "";
    position: absolute;
    top: 10px;
    right: 10px;
    width: 100px;
    height: 100px;
    background-image: url("background-image.png");
    background-repeat: no-repeat;
    background-size: cover;
    background-position: center;
}

This creates a decorative image positioned inside the container.

How to Add a Background Image Behind Content Using ::After

A common use case is placing an image behind text or content.

HTML:

<section class="hero">
    <h1>Welcome to Our Website</h1>
</section>

CSS:

.hero {
    position: relative;
    z-index: 1;
    padding: 100px 40px;
}

.hero::after {
    content: "";
    position: absolute;
    inset: 0;
    background-image: url("hero-background.jpg");
    background-size: cover;
    background-position: center;
    z-index: -1;
}

Benefits:

  • No extra markup
  • Clean HTML structure
  • Easier maintenance
  • Better design control

Creating a Dark Overlay Using CSS ::After

Many websites use overlays to improve text readability.

.hero {
    position: relative;
}

.hero::after {
    content: "";
    position: absolute;
    inset: 0;
    background: rgba(0,0,0,0.5);
}

You can combine this with a background image to create professional hero sections.

Example:

.hero::before {
    content: "";
    position: absolute;
    inset: 0;
    background-image: url("hero.jpg");
    background-size: cover;
}

.hero::after {
    content: "";
    position: absolute;
    inset: 0;
    background: rgba(0,0,0,.5);
}

How to Add Icons Using CSS ::After

The ::after pseudo-element is frequently used for icons.

HTML:

<a class="button" href="#">
    Learn More
</a>

CSS:

.button {
    position: relative;
}

.button::after {
    content: "";
    display: inline-block;
    width: 16px;
    height: 16px;
    margin-left: 8px;
    background-image: url("arrow.svg");
    background-size: contain;
    background-repeat: no-repeat;
}

This technique is useful for:

  • Navigation arrows
  • Download icons
  • External link indicators
  • CTA buttons

Responsive Background Images in CSS ::After

Responsive design is essential for modern websites.

Use:

.element::after {
    content: "";
    background-size: cover;
    background-position: center;
}

For better mobile responsiveness:

@media (max-width: 768px) {

    .element::after {
        width: 80px;
        height: 80px;
    }

}

Best practices:

  • Use SVG when possible.
  • Compress image files.
  • Avoid oversized images.
  • Use responsive dimensions.

CSS ::Before vs ::After for Background Images

Both pseudo-elements work similarly, but they serve different purposes.

Feature::Before::After
Appears Before ContentYesNo
Appears After ContentNoYes
Supports Background ImagesYesYes
Suitable for DecorationsYesYes
Useful for OverlaysYesYes
Requires Content PropertyYesYes

Choose ::before when the visual element should appear before content and ::after when it should appear after content.

Real-World Examples of CSS ::After Background Images

Hero Section Background

.hero::after {
    content: "";
    position: absolute;
    inset: 0;
    background-image: url("hero.jpg");
    background-size: cover;
}

Decorative Corner Element

.card::after {
    content: "";
    position: absolute;
    top: 0;
    right: 0;
    width: 60px;
    height: 60px;
    background-image: url("corner.svg");
}

Button Arrow Icon

.button::after {
    content: "";
    background-image: url("arrow.svg");
}

Ribbon Design

.ribbon::after {
    content: "";
    background-image: url("ribbon.png");
}

Section Overlay

.section::after {
    content: "";
    position: absolute;
    inset: 0;
    background: rgba(0,0,0,.4);
}

Common Problems and Fixes

Problem 1: Background Image Not Showing

Cause:

content: "";

is missing.

Solution:

Always define the content property.

Problem 2: Width and Height Are Missing

Pseudo-elements require dimensions.

width: 100px;
height: 100px;

Without dimensions, the element may not be visible.

Problem 3: Parent Element Isn’t Positioned

Solution:

.parent {
    position: relative;
}

This ensures proper positioning.

Problem 4: Incorrect Image Path

Verify your image path:

background-image: url("images/photo.jpg");

Check folder structure and file names carefully.

Problem 5: Z-Index Issues

If the image appears behind other elements:

z-index: 1;

Or:

z-index: -1;

Adjust layering as needed.

Problem 6: Background Image Appears Distorted

Use:

background-size: cover;

or

background-size: contain;

depending on your design requirements.

Best Practices for Using Background Images in CSS ::After

Follow these recommendations:

  • Use SVG graphics whenever possible.
  • Compress image files for faster loading.
  • Keep HTML clean and semantic.
  • Use responsive image techniques.
  • Avoid excessive decorative images.
  • Test across devices and browsers.
  • Use meaningful z-index values.
  • Optimize for accessibility.

When Should You Use CSS ::After Instead of Extra HTML?

Use CSS ::after when:

  • Adding decorative graphics
  • Creating overlays
  • Showing icons
  • Building hover effects
  • Adding visual accents

Avoid using pseudo-elements for content that is important for accessibility or SEO because generated content is not part of the document structure.

Browser Compatibility

The CSS ::after pseudo-element is supported in all modern browsers, including:

  • Google Chrome
  • Mozilla Firefox
  • Microsoft Edge
  • Safari
  • Opera

This makes it a reliable solution for modern web development projects.

Conclusion

Using a background image in CSS ::after is an efficient way to create overlays, decorative graphics, icons, and advanced visual effects without cluttering your HTML. By understanding positioning, sizing, z-index management, and responsive design principles, you can build cleaner, more maintainable interfaces while enhancing the user experience.

Whether you’re creating hero sections, custom buttons, overlays, ribbons, or decorative backgrounds, the CSS ::after pseudo-element remains one of the most useful tools in modern front-end development.

Frequently Asked Questions (FAQs)

Can I add a background image using CSS ::after?

Yes. You can add a background image with CSS ::after by using content: "", setting width and height, and applying background-image.

Why is my CSS ::after background image not showing?

The most common reasons are missing content, no width or height, wrong image path, missing position: relative on the parent, or z-index issues.

Is ::after better than adding an image in HTML?

Use ::after for decorative images, icons, overlays, and design effects. Use HTML <img> when the image is meaningful content or important for accessibility.

Can I use SVG images in CSS ::after?

Yes. SVG images work very well with ::after, especially for icons, arrows, badges, and decorative shapes.

How do I position a background image in ::after?

Use CSS positioning properties like position: absolute, top, right, bottom, left, or inset. The parent element should usually have position: relative.

Can I make a CSS ::after background image responsive?

Yes. Use background-size: cover, background-position: center, percentages, flexible units, and media queries for different screen sizes.

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

::before inserts generated content before an element’s content, while ::after inserts generated content after it. Both can be used for background images and decorative effects.

Can I create an overlay using CSS ::after?

Yes. You can create overlays by setting ::after to cover the parent element with position: absolute, inset: 0, and a semi-transparent background color.

Does CSS ::after work on all elements?

It works on most normal HTML elements, but it does not work properly on replaced elements like <img>, <input>, and <iframe>.

Is using ::after good for SEO?

Yes, when used for decoration. But important images or meaningful text should not be added only with ::after, because pseudo-element content is not ideal for SEO or accessibility.

This page was last edited on 22 June 2026, at 6:03 pm