I have an <img>
over which I'd like to display a text overlay.
I'd like to display the text overlay at the bottom of the image - but I don't know the height of the image. (There are numerous images, all with the same standard width, but the width/height ratio differs from one image to the next.)
One approach is to use an absolutely positioned img::after
:
main > figure {
display:inline-block;
position:relative;
width:400px;
height:284px;
background-color:rgb(0,0,127);
}
main > figure img {
display:block;
position:relative;
width:376px;
height:200px;
margin:12px auto;
background-color:rgb(255,255,0);
}
main > figure img::after {
content:'Example text overlay';
position:absolute;
display:block;
bottom:0;
left:0;
width:100%;
height:36px;
background-color:rgb(255,0,0);
}
<main>
<figure>
<img src="" alt="Image of variable height" />
</figure>
</main>
So far, so good.
But as soon as I add the image src
. The ::after
pseudo-element disappears:
main > figure {
display:inline-block;
position:relative;
width:400px;
height:284px;
background-color:rgb(0,0,127);
}
main > figure img {
display:block;
position:relative;
width:376px;
height:200px;
margin:12px auto;
background-color:rgb(255,255,0);
}
main > figure img::after {
content:'test';
position:absolute;
display:block;
bottom:0;
left:0;
width:36px;
height:36px;
background-color:rgb(255,0,0);
}
<main>
<figure>
<img src="http://placehold.it/376x200" alt="Image of variable height" />
</figure>
</main>
I assume the pseudo-element must be underneath the image file.
But how can I display it above the image file?
I've tried messing about with z-index
to no avail.
No luck with opacity
either.