Force Margin/Space Between List and Floated Image
If you’re displaying floated images in your posts, you may notice that margins of lists and other block-level elements seem to “collapse”, as shown in this screenshot from the 2013 redesign:
![[ Screenshot: Unordered list wrapping against floated image ]](https://perishablepress.com/wp/wp-content/images/2013/css-list-float-01.jpg)
And here is what is happening behind the scenes (Chrome browser):
![[ Screenshot: Unordered list wrapping against floated image (via Inspect Element) ]](https://perishablepress.com/wp/wp-content/images/2013/css-list-float-02.jpg)
As you can see, the list is simply ignoring the floated item and stretching across the entire width of the content area. Depending on the design, this may not be a huge deal, but there is an easy way to prevent it from happening. Just apply a width and overflow property to the list element:
.content ul, .content ol { width: auto; overflow: hidden; }
Applying that code, the margin kicks in and we get this:
![[ Screenshot: Unordered list with margin between floated image ]](https://perishablepress.com/wp/wp-content/images/2013/css-list-float-03.jpg)
..which looks like this under the hood:
![[ Screenshot: Unordered list with margin between floated image (via Inspect Element) ]](https://perishablepress.com/wp/wp-content/images/2013/css-list-float-04.jpg)
This trick also works on other block-level elements, such as <div>s. Here is another example from the recent redesign, which shows a <div> stretching full-width behind the floated element, again in Chrome:
![[ Screenshot: Div stretching full-width behind floated image ]](https://perishablepress.com/wp/wp-content/images/2013/css-list-float-05.jpg)
To get the popout <div> to sit next to the floated image — without floating it — we apply the simple fix:
.popout { width: auto; overflow: hidden; }
..which gives us this:
![[ Screenshot: Div sitting next to floated image ]](https://perishablepress.com/wp/wp-content/images/2013/css-list-float-06.jpg)
..and via Inspect Element:
![[ Screenshot: Div sitting next to floated image (via Inspect Element) ]](https://perishablepress.com/wp/wp-content/images/2013/css-list-float-07.jpg)
That’s pretty much it, although for this design I opted to let the popout divs stretch behind the floated images, just seems like a nice effect :)
This technique tested on latest Chrome 67+, Firefox 60+, and Opera 53+.
Perishable Press is operated by Jeff Starr, a professional web developer and book author with two decades of experience.
Here you will find posts about web development, WordPress, security, and
39 responses to “Force Margin/Space Between List and Floated Image”
Does this code snippet effect the max-width property, when you’re making a website responsive?
Thanks.