DEV Community

Cover image for How to put an image inside pseudo elements in CSS?
MELVIN GEORGE
MELVIN GEORGE

Posted on • Originally published at melvingeorge.me

1

How to put an image inside pseudo elements in CSS?

Originally posted here!

To put an image as the content inside pseudo-elements like :before and :after, you can use the url() function in the CSS and pass the link to the image file as a string to the function.

/* Put image in Pseudo element ✅ */
div:after {
  content: url("path-to-my-image.jpg");
}
Enter fullscreen mode Exit fullscreen mode

For example, let use a real image to understand. Check out the image which we will be using for the example.

Now in the :after or :before pseudo elements for a p (paragragh) element, you can use it like this,

p:before {
  content: url("https://upload.wikimedia.org/wikipedia/commons/1/1d/Football_Pallo_valmiina-cropped.jpg");
}
Enter fullscreen mode Exit fullscreen mode

That's it! 🔥

See the above code live in JSBin

Feel free to share if you found this useful 😃.


Heroku

Deliver your unique apps, your own way.

Heroku tackles the toil — patching and upgrading, 24/7 ops and security, build systems, failovers, and more. Stay focused on building great data-driven applications.

Learn More

Top comments (0)

Jetbrains image

Build Secure, Ship Fast

Discover best practices to secure CI/CD without slowing down your pipeline.

Read more

👋 Kindness is contagious

Dive into this insightful write-up, celebrated within the collaborative DEV Community. Developers at any stage are invited to contribute and elevate our shared skills.

A simple "thank you" can boost someone’s spirits—leave your kudos in the comments!

On DEV, exchanging ideas fuels progress and deepens our connections. If this post helped you, a brief note of thanks goes a long way.

Okay