3

I have HTML:

<figure style="display:inline-block;">
   <img src="https://i.imgur.com/k1Crowy.jpg" width="200">
   <figcaption> This is some random text that should not expand the figure tag beyond the width of the img tag </figcaption>
</figure>

I want the <figure> tag to have the width of the <img> tag, which works just fine.

The problem is the content of the <figcaption> tag expands the width of the <figure> tag. How can I make it so the <figcaption> will be limited to the width of the <img> tag. So 200px in above example.

I can't edit the HTML. Can only adjust CSS.

What I've tried so far

  1. position:absolute approach on <figcation>. Problem is that it won't expand the height of the <figure> tag.
  2. Obviously, I can set a fixed width to <figure> tag (like width:200px), but I can't predict the width of the image.
  3. jQuery approach where I grab the width of the image and add it to the figure tag. But I want to avoid a JS solution.
Henrik Petterson
  • 6,862
  • 20
  • 71
  • 155

0 Answers0