I have a problem where the figcaption
is wider than the img
and hence sticking out to the right of it. I hardcoded the width
of the caption and it works, but only for images of the same width.
HTML
<figure>
<img src="http://i.imgur.com/tVeUqlH.jpg"/>
<figcaption>Vader enjoying the beach.</figcaption>
</figure>
CSS
figure {
border: 1px solid black;
display: inline-block;
padding: 3px;
margin: 10px;
}
figure img {
vertical-align: top;
}
figcaption {
text-align: center;
border: 1px dotted blue;
width: 120px;
}
jsFiddle
<figcaption>: The Figure Caption element The <figcaption> HTML element represents a caption or legend describing the rest of the contents of its parent <figure> element.
The <figcaption> tag defines a caption for a <figure> element. The <figcaption> element can be placed as the first or last child of the <figure> element.
Use text-align: center on figcaption to align the test to the center. Save this answer.
A figure can be used with or without a figcaption . However, without a caption, or an alternate means of providing an accessible name (e.g. aria-label ) a figure may not provide much value in conveying its semantics alone. In some cases, it may not convey any semantics at all if its given no accessible name.
You can use display: table-caption;
.
.imagecaption {
padding: 3px;
margin: 10px;
float: left;
border: 1px solid black;
}
figure {
display: table;
margin: 0px;
}
figure img {
display: block;
}
figcaption {
display: table-caption;
caption-side: bottom;
text-align: center;
border: 1px dotted blue;
}
<div class="imagecaption">
<figure>
<img src="http://i.imgur.com/tVeUqlH.jpg"/>
<figcaption>Vader enjoying the beach.</figcaption>
</figure>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With