TK123 TK123 - 5 months ago 13
CSS Question

Is it possible to wrap the contents of this div based on one element over another?

I'm not sure if I was able to word the question correctly but what I'm trying to accomplish is simple to see through example.

Pease take a look at this:

http://codepen.io/anon/pen/XKKvBx

I basically want that to look like this:

http://codepen.io/anon/pen/xOOvJB

But without giving the wqrapping div a fixed width. So basically I'd like the wrapper to take the width of the image and not the overextending text. Is there any way to do this?

Also note I can't use absolute positioning as the image and text coming through from the database will be different each time.

Code from first link:

<div class="wrapper">
<img src="https://placekitten.com/300/300">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam ornare dictum ligula quis dictum. Nam dictum, eros sit amet imperdiet aliquet, ligula nisl blandit lectus, quis malesuada nunc ipsum ac magna. Vestibulum in magna eu sem suscipit molestie. Maecenas a ligula molestie, volutpat turpis et, venenatis massa. Nam aliquam auctor lectus ac lacinia. Nam consequat lacus porta odio hendrerit mollis. Etiam at congue est, eu fermentum erat. Praesent vestibulum malesuada ante. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</p>
</div>

.wrapper {
border: 1px solid red;
display: inline-block;
}


Code from second link:

<div class="wrapper">
<img src="https://placekitten.com/300/300">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam ornare dictum ligula quis dictum. Nam dictum, eros sit amet imperdiet aliquet, ligula nisl blandit lectus, quis malesuada nunc ipsum ac magna. Vestibulum in magna eu sem suscipit molestie. Maecenas a ligula molestie, volutpat turpis et, venenatis massa. Nam aliquam auctor lectus ac lacinia. Nam consequat lacus porta odio hendrerit mollis. Etiam at congue est, eu fermentum erat. Praesent vestibulum malesuada ante. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</p>
</div>

.wrapper {
width: 300px;
border: 1px solid red;
}

Answer

There is one "hacky" way, using another display property for your wrapper, if you force it to be table and 1% width the img will break that width and set his own size as the width wrapper:

.wrapper {
  border: 1px solid red;
  display: table;
  width: 1%;
}
<div class="wrapper">
  <img src="https://placekitten.com/300/300">
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam ornare dictum ligula quis dictum. Nam dictum, eros sit amet imperdiet aliquet, ligula nisl blandit lectus, quis malesuada nunc ipsum ac magna. Vestibulum in magna eu sem suscipit molestie.
    Maecenas a ligula molestie, volutpat turpis et, venenatis massa. Nam aliquam auctor lectus ac lacinia. Nam consequat lacus porta odio hendrerit mollis. Etiam at congue est, eu fermentum erat. Praesent vestibulum malesuada ante. Cum sociis natoque
    penatibus et magnis dis parturient montes, nascetur ridiculus mus.</p>
</div>