Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<figure>&<figcaption> with floating image, figcaption wraps, and article text wraps around image/caption

Tags:

html

css

figure

Searched the web for weeks but can't find the answer that satisfies all my needs (only partial), help is very welcome.

What I want and have accomplished:

  • Plain HTML5 compliant and CSS
  • Show an image within an article
  • Image must have a caption
  • Caption must below the image
  • Caption must be limited to horizantal size of image
  • Caption may be longer than one line and text should wrap to next line (still within size of image)
  • Image and caption must float as a group to left or right (think of using <figure class="left">)
  • Text of article must wrap around the image and caption
  • Image size varies (first image is e.g. 200px, 2nd image somewhere else in text is 320px etc.)

And now I like to add those requirements:

  • I don't like to add the original width of the image like <figure class="left" style="width:200px"> but only if there is no other way.
  • Responsive design: when the image width will occupy more that 50% of the display width, it must be limited to 50% of the display width.
  • When the display width is 320px or below, the image must not float but must a block level element, so no text wrap of article around the image.

Where I left:

figure {
  display: inline
}

figcaption {
  display: block
}

figure.left {
  float: left
}

figure.right {
  float: right
}
<p>This is a part of the text of the article and at this point a image is inserted at the left side
  <figure class="left" style="width:250px">
    <img src="image.png" alt="img txt">
    <figcaption>image caption and a lot of text</figcaption>
  </figure>
  and the article text goes on and on so that it will wrap around the image</p>

(I left out the padding/margins and such to make it look better.)

like image 534
luvTweb Avatar asked Oct 22 '22 05:10

luvTweb


2 Answers

Try the following css and look when you resize the browser, the text wrap on the image:

.left {
  float: left;
  border: 5px solid #BDBDBD;
  background: #E0E0E0;
  padding: 5px;
  margin: 0px;
}

figcaption {
  text-align: center;
}
like image 117
SRL Avatar answered Oct 24 '22 01:10

SRL


Maybe this feels too html3 to you, but I found this answer:

http://www.sitepoint.com/forums/showthread.php?1049396-How-to-force-this-figcaption-element-to-respect-its-parent-s-width-boundaries

figure {
  display: table;
}
figcaption {
  display: table-caption;
  caption-side: bottom;
}

I don't think this is forbidden by HTML5 or CSS3 and it certainly seems to work for me.

Leaving aside the responsive design requirements - I feel like that's a separate question to which I don't have a good CSS-only answer.

like image 42
Julian Avatar answered Oct 24 '22 03:10

Julian