I'm trying to create a solid-color overlay that exactly matches the size of an image, and display text on that overlay. I'd like to do this with HTML and CSS only, if possible.
The image may be of any size, and will be sized to fit and centered within its parent container. Something like this (which does not work):
HTML:
<div class="img-overlay">
<img src="file.jpg">
<div class="overlay">Text will flow...</div>
</div>
CSS:
.img-overlay img {
margin: 0 auto;
max-height: 100%;
}
.overlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%
background-color: rgba(255,255,255,0.5);
}
I thought that HTML5's <figure>
tag might help here, but haven't had much success on that front. The following keeps the caption width pegged to the image width, but when I try to remove it from the document flow and position it over the image, it ends up to the left of the image due to the image centering via margin: 0 auto;
.
<figure>
<img src="file.jpg">
<figcaption>Text will flow...</figcaption>
</figure>