DisgruntledGoat
DisgruntledGoat

Reputation: 72530

Limit text to the width of sibling image / auto width in CSS

I am essentially trying to create a version of the "figure" element (upcoming in HTML5), whereby I have an image with a short description below it.

However, I want to limit the width of this entire element to that of the image, so the text isn't wider than the image (wrapping to multiple lines if necessary).

Basic HTML:

<div class="figure">
<img src="..." alt="..." width="..." height="..." /><br />
A description for the image
</div>

I'm well-versed with CSS but I can't think of any pure CSS solution, without adding a style="width:100px" to the div to match the image width.

Update: After a bit of searching and thinking, the best method seems to be using an inline width on the div. I will keep the width attribute on the image, in case I wish the div to be a bit wider than the image (for example to accomodate a longer caption).

This approach also means I could have two images side-by-side with a caption below. If I have a set of images the same size, I can of course add an extra style to each div.

Thanks to everyone who answered!

Upvotes: 28

Views: 24647

Answers (6)

sayandcode
sayandcode

Reputation: 3204

You can also acheive this using the following solution proposed by Temani Afif in his blog post (All credits to him, I just don't want the solution to be forgotten)

<div class="box">
   <img>
   <h1>Lorem ipsum dolor ..</h1>
</div>
.box {
  display: inline-block;
}
h1 {
  width: 0;
  min-width: 100%;
}

Make the container inline-block, and makes the h1 (or whatever text tag you use) occupy the space dictated by the sibling element. It's essentially a hack, but it works! No unintended semantic consequences like the table solutions

Upvotes: 3

Antony Scott
Antony Scott

Reputation: 21998

Stylesheet

div.figure img,
div.figure div.caption {
    width: 100%;
}
div.figure div {
    overflow: hidden;
    white-space: nowrap;
}

note: to enable wrapping just remove that last css line

HTML

<div class="figure" style="width:150px;">
    <img src="logo.png" alt="logo" />
    <div class="caption">A description for the image</div>
</div>

I've checked it in Chrome, Firefox and IE7 and it looks good in all three. I realise this has the width on the div and not the img, but at least you only need to set the width in one place. Short of using css-expressions (IE only) I can't see a way of setting the outer divs width to the width of the first child element.

Upvotes: 3

Hedley Smith
Hedley Smith

Reputation: 1417

This could also be accomplished using 'display: table-caption' for the caption, as follows:

HTML

<div class="wrapper">
  <img src="image.jpg" />
  <div class="caption">My caption...</div>
</div>

Stylesheet

.wrapper {
  display: table;
}

.caption {
  display: table-caption;
  caption-side: bottom;
}

This block can also be floated left of right of other text. I've tested this in IE8+. Here's a JSBin example: http://jsbin.com/xiyevovelixu/1

Upvotes: 24

user73912
user73912

Reputation: 49

I had the same problem and after reading this decided to use an inline-style on the surrounding element. Seems the better solution over using a table to me.

Upvotes: 2

Angel
Angel

Reputation: 401

For setting the width to match the image automatically you could use

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

This makes the div behave like a table (not supported in Internet Explorer). Or you could use a table instead of the div. I don't think there is another way of setting the width automatically.

Edit: The simplest way is to forget about the auto width and set it by hand. If it is really needed you can use JavaScript or a table. In this case the use of a table is not so ugly because you are addressing a limitation of the HTML version. In the case of server-side scripting you could also set the width when generating the page.

Upvotes: 9

doekman
doekman

Reputation: 19268

You could use the display:table solution for all other browsers, and a CSS Behaviour for Internet Explorer.

Upvotes: 0

Related Questions