1

Sorry if this question already exists, but I didn't manage to find it. I am wondering why img:after in CSS is not working. I tried setting the display of the img to be block and what not, but it refuses to work every time...Any ideas? I am trying to place a background image after an image (for oldschool shadow), but it works only if I place the image in a div/span.

That's what I've been trying to run:

HTML:

<!DOCTYPE html>
<html>
    <head>
        <title>Exercise2</title>
        <link rel="stylesheet" type="text/css" href="exercise2.css">
    </head>
    <body>
        <div id="container">
            <img src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcSJxP3c9QZ--VxiX0M1p9shvBa0KDLGjTjlqC3V4ygH_zA6vn-Bvw">
        </div>
    </body>
</html>

CSS:

#container {
    width: 100%;
}

img {
    display: block;
}

.shadow:after {
    display: block;
    position: absolute;
    right: 0;
    top: 0;
    top: 10%;
    content: "";
    height: 90%;
    width: 6px;
    background: url(pictures/shadow_side.png) repeat-y 0% 0%;
}
Sampson
  • 265,109
  • 74
  • 539
  • 565
Boyan Hristov
  • 1,067
  • 3
  • 15
  • 41

3 Answers3

3

Think of the :after pseudo-element as something like :last-child. It adds a pseudo-element within the containing element, after the element's document tree. Images have an empty content model, meaning you cannot place another element (or pseudo-element) inside them.

Sampson
  • 265,109
  • 74
  • 539
  • 565
  • This is not quite correct. The determining factor is not whether an element has a empty content model, but whether it is a replaced element or not. As proof, see http://jsfiddle.net/ZeAj4/ where the void element `meta` is used yet its `:before` pseudo-element content is rendered nonetheless. – Alohci Dec 28 '13 at 02:03
  • @Alohci That makes sense; do you know of a place in the specification that states that more explicitly? [The specification](http://www.w3.org/TR/selectors/#gen-content) only connects them to the *content* of an element. – Sampson Dec 28 '13 at 15:48
  • I don't unfortunately. I have to say I've always found the specs rather sparse on the matter of replaced elements. It seems like they fall down the gap between HTML and CSS, so I've taken to just testing for de-facto behaviour. – Alohci Dec 28 '13 at 19:38
2

The specs says

Note. This specification does not fully define the interaction of :before and :after with > replaced elements (such as IMG in HTML). This will be defined in more detail in a future > specification.

So that would probably mean, that :after and :before don't work with img elements

Igor Šarčević
  • 3,481
  • 1
  • 19
  • 21
1

Images contain nothing inside them (they close themselves). And since the :before and :after append stuff inside the element (check out this awesome article by Chris Coyier), you cannot simply just add some stuff by the :before and :after selectors in an <img> tag.

And even if you could, your <img> tag has no class="shadow", which it should have.

Lucas
  • 16,930
  • 31
  • 110
  • 182