CSS: Keep paragraphs together while wrapping (floating)

Viewed 26
  • I have a floating image on the left side.
  • Elements on the right side (text, images ...) should wrap around it.
  • The title of an image should not be wrapped separately from the image, when the screen becomes too small).

Image: Window is big enough

(grey background added for clear distinction of the element) enter image description here

Code

<!DOCTYPE html><html><body>

<div style="float:left"> 
  <img style="width:150px; height: auto" src="chrome://branding/content/about-logo@2x.png" /> 
</div>
<p>
  foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo 

  <div style="background-color: lightgrey;">
  Image title<br>
  <img style="width:120px; height: 40px" src="chrome://branding/content/about-logo@2x.png" />
  </div>
</p>

</body></html>

Image: Window is too small (actual)

enter image description here

Image: Window is too small (expected)

enter image description here

1 Answers

To keep your paragraphs together in this situation, you need display: inline-block on the parent element.

Code

<!DOCTYPE html>
<html>
<body>

<div style="float:left"> 
  <img style="width:150px; height: auto" src="chrome://branding/content/about-logo@2x.png" /> 
</div>
<p>
  foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo 

  <div style="background-color: lightgrey;display: inline-block;">
    Image title<br> 
    <img style="width:120px; height: 40px" src="chrome://branding/content/about-logo@2x.png" />
  </div>
</p>


</body>
</html>

Generalization

(without furhter research I think) you could maybe generalize to display: inline-block on a div will make it keep the paragraphs together

<div style="display: inline-block;">
  Image title<br> 
  <img src="" />
</div>
Related