Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a figcaption be restricted to the width of a responsively sized image?

Tags:

Say I have the following HTML:

<figure>
<img alt="Sun" src="sun.gif" width="256" height="256" />
<figcaption>The Sun - a sinister and destructive force. Image from
the SOHO research facility.</figcaption>
</figure>

If I want the text to wrap to the width of the image, I need this CSS:

figure {
    display: table;
    width: 1px;
}

If I want the image to be "responsive" — that is, be no bigger than the viewport — I need this CSS too:

img {
    max-width: 100%;
}

But combining these two results in a terrible mess! Now that the img's parent has an explicit width set, the max-width causes the whole figure to be really, really tiny (although not quite 1px).

So is it possible to use CSS (2 or 3) to achieve both an image caption wrapped to no wider than the image, and an image no wider than the viewport?

like image 389
detly Avatar asked Apr 22 '12 01:04

detly


People also ask

How do I set the width and height of a responsive image?

To make an image responsive, you need to give a new value to its width property. Then the height of the image will adjust itself automatically. The important thing to know is that you should always use relative units for the width property like percentage, rather than absolute ones like pixels.

What does the Figcaption tag do?

The <figcaption> HTML element represents a caption or legend describing the rest of the contents of its parent <figure> element.

Which attribute you can use to adjust the width of your image?

The width attribute specifies the width of an image, in pixels. Tip: Always specify both the height and width attributes for images. If height and width are set, the space required for the image is reserved when the page is loaded.

Does a figure need a Figcaption?

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.


2 Answers

I came across the same issue and was able to come up with a fairly neat solution, inspired by this.

HTML:

<figure>
   <img src="http://www.placehold.it/300x150" alt="" />
   <figcaption>Make me as long as you like</figcaption>
</figure>​

CSS:

figure {
   display: table;
   padding: 5px;
   background-color: #fff;
   font-size: .875em;
   
}

figure img {
   display: block;
   max-width: 100%;
}

figcaption {
   display: table-caption;
   caption-side: bottom;
   background: #fff;
   padding: 0 5px 5px;
}​

This ensures the figcaption does not exceed the width of the figure, whilst allowing you to keep max-width on the image. Works in all good browsers and IE8+.

The figure's width will be whatever the native width of the image is, unless that width is restricted in some other way.

like image 79
CherryFlavourPez Avatar answered Sep 20 '22 05:09

CherryFlavourPez


So is it possible to use CSS (2 or 3) to achieve both an image caption wrapped to no wider than the image, and an image no wider than the viewport?

Yes. Just use viewport units on the img - max-width: 100vw; instead of % - max-width: 100%;

DEMO

Just for completeness:

There are 2 other techniques to get the text to wrap to the width of the image:

1) Set display: table-caption; on the figcaption

DEMO

2) Set width: min-content on the figure

DEMO

like image 39
Danield Avatar answered Sep 20 '22 05:09

Danield