Experience the powerful AI writing right inside WordPress
Show stunning before-and-after transformations with image sliders.
Improve user engagement by showing estimated reading time.
Written by Mahmuda Akter Isha
Showcase Designs Using Before After Slider.
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.
::after
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.
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.
To display a background image inside a pseudo-element, you must:
content
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.
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.
A common use case is placing an image behind text or content.
<section class="hero"> <h1>Welcome to Our Website</h1> </section>
.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:
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); }
The ::after pseudo-element is frequently used for icons.
<a class="button" href="#"> Learn More </a>
.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:
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:
Both pseudo-elements work similarly, but they serve different purposes.
Choose ::before when the visual element should appear before content and ::after when it should appear after content.
::before
.hero::after { content: ""; position: absolute; inset: 0; background-image: url("hero.jpg"); background-size: cover; }
.card::after { content: ""; position: absolute; top: 0; right: 0; width: 60px; height: 60px; background-image: url("corner.svg"); }
.button::after { content: ""; background-image: url("arrow.svg"); }
.ribbon::after { content: ""; background-image: url("ribbon.png"); }
.section::after { content: ""; position: absolute; inset: 0; background: rgba(0,0,0,.4); }
Cause:
content: "";
is missing.
Solution:
Always define the content property.
Pseudo-elements require dimensions.
width: 100px; height: 100px;
Without dimensions, the element may not be visible.
.parent { position: relative; }
This ensures proper positioning.
Verify your image path:
background-image: url("images/photo.jpg");
Check folder structure and file names carefully.
If the image appears behind other elements:
z-index: 1;
Or:
z-index: -1;
Adjust layering as needed.
background-size: cover;
or
background-size: contain;
depending on your design requirements.
Follow these recommendations:
Use CSS ::after when:
Avoid using pseudo-elements for content that is important for accessibility or SEO because generated content is not part of the document structure.
The CSS ::after pseudo-element is supported in all modern browsers, including:
This makes it a reliable solution for modern web development projects.
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.
Yes. You can add a background image with CSS ::after by using content: "", setting width and height, and applying background-image.
content: ""
background-image
The most common reasons are missing content, no width or height, wrong image path, missing position: relative on the parent, or z-index issues.
position: relative
Use ::after for decorative images, icons, overlays, and design effects. Use HTML <img> when the image is meaningful content or important for accessibility.
<img>
Yes. SVG images work very well with ::after, especially for icons, arrows, badges, and decorative shapes.
Use CSS positioning properties like position: absolute, top, right, bottom, left, or inset. The parent element should usually have position: relative.
position: absolute
top
right
bottom
left
inset
Yes. Use background-size: cover, background-position: center, percentages, flexible units, and media queries for different screen sizes.
background-size: cover
background-position: center
::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.
Yes. You can create overlays by setting ::after to cover the parent element with position: absolute, inset: 0, and a semi-transparent background color.
inset: 0
It works on most normal HTML elements, but it does not work properly on replaced elements like <img>, <input>, and <iframe>.
<input>
<iframe>
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
Your email address will not be published. Required fields are marked *
Comment *
Name *
Email *
Website
Save my name, email, and website in this browser for the next time I comment.
How many people work in your company?Less than 1010-5050-250250+
By proceeding, you agree to our Privacy Policy