Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Figcaption no wider than the image

Tags:

html

css

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.

enter image description here

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

like image 516
Vader Avatar asked Feb 08 '15 12:02

Vader


People also ask

What does Figcaption mean?

<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.

What does Figcaption mean in HTML?

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.

How do I center a Figcaption under an image?

Use text-align: center on figcaption to align the test to the center. Save this answer.

Can I use Figcaption without figure?

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.


1 Answers

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>
like image 193
NETCreator Hosting - WebDesign Avatar answered Oct 10 '22 00:10

NETCreator Hosting - WebDesign