I have a responsive slideshow-type layout with captions below each image.
I'm attempting to get the caption to be the same width as the image. The problem is that the image is scaled to fit in the browser vertically, and my captions are the getting the width of the image prior to scaling.
#big_container {
display:block;
position:relative;
width:100%;
padding-bottom:40%;
white-space:nowrap;
overflow-x:scroll;
overflow-y:hidden;
}
#big_container>div {
position:absolute;
top:0;
right:0;
bottom:0;
left:0;
}
.little_container {
display:inline-block;
height:100%;
width:100%;
text-align:center;
}
#big_container figure {
display:inline-block;
height:100%;
margin:0;
}
figure img {
max-height:calc(100% - 40px); /* subtract height of caption */
}
figcaption {
display:block;
width:100%;
text-align:left;
box-sizing:border-box;
margin:0;
padding:10px;
line-height:20px;
background-color:#ddd;
}
<div id="big_container">
<div>
<div class="little_container">
<figure>
<img src="http://placekitten.com/500/440">
<figcaption>
have a kitty!!1
</figcaption>
</figure>
</div>
<div class="little_container">
<figure>
<img src="http://placekitten.com/450/400">
<figcaption>
moar kitty!
</figcaption>
</figure>
</div>
<div class="little_container">
<figure>
<img src="http://placekitten.com/300/440">
<figcaption>
too many kitty..
</figcaption>
</figure>
</div>
</div>
</div>
How can I make a caption which is scaled based on the width of a fluid image?
I'm hoping for a pure-css solution.
Update
It turns out my above attempt partially works in chrome and opera, but exhibits some odd behavior.
I haven't found any bug reports on the subject, but I can't help wondering if this might be considered a bug in the browser.
For clarity, here's a brief outline of my exact requirements:
- Caption element must be the same width as the image (it'd be nice to be able to left or right align the caption text to the edge of the image)
- Image must not be cropped or stretched
- Image and caption must both fit in their container (which may be fluid), using as much room as possible.
- Above rules should hold true for images of any dimension
- CSS only (compatibility with old browsers not a major concern, but it is a plus)
The html markup can be changed.