Does :before not work on img elements?

I’m trying to use the :before selector to place an image over another image, but I’m finding that it simply doesn’t work to place an image before an img element, only some other element. Specifically, my styles are:

.container
{
   position: relative;
   display: block;
}

.overlay:before
{
    content: url(images/[someimage].png);
    position: absolute;
    left:-20px;
    top: -20px;
}

and I find that this works fine:

<a href="https://stackoverflow.com/questions/5843035/[url]" class="container">
  <span class="overlay"/>
  <img width="200" src="https://stackoverflow.com/questions/5843035/[url]"/>
</a>

but this does not:

<a href="https://stackoverflow.com/questions/5843035/[url]" class="container">
  <img width="200" src="https://stackoverflow.com/questions/5843035/[url]" class="overlay"/>
</a>

I can use a div or p element instead of that span, and the browser correctly overlays my image over the image in the img element, but if I apply the overlay class to the img itself, it doesn’t work.

I’d like to get this working because that extra span offends me, but more importantly, I’ve got about 100 blog posts that I’d like to modify, and I can do this in one go if I could just modify the stylesheet, but if I have to go back and add an extra span element in between the a and img elements, this will be a lot more work.

15 Answers
15

Leave a Comment