New Bookstore! Save 20% on books with discount code: LAUNCH
Web Dev + WordPress + Security

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 ]

And here is what is happening behind the scenes (Chrome browser):

[ Screenshot: Unordered list wrapping against floated image (via Inspect Element) ]

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 ]

..which looks like this under the hood:

[ Screenshot: Unordered list with margin between floated image (via Inspect Element) ]

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 ]

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 ]

..and via Inspect Element:

[ Screenshot: Div sitting next to floated image (via Inspect Element) ]

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+.

Jeff Starr
About the Author
Jeff Starr = Web Developer. Security Specialist. WordPress Buff.
Blackhole Pro: Trap bad bots in a virtual black hole.

39 responses to “Force Margin/Space Between List and Floated Image”

  1. This is such an elegant solution Jeff. Thank you for sharing! I’ve recently come across exactly this problem and addressed it by overriding the list styles after images. I much prefer your solution though. Great!

  2. Great article, Jeff! Thanks. I have been trying to fix this for quite a long time. Now I don’t have to do “work-arounds” on individual articles. The site looks great too! (I like the “Do it” button.)

  3. If I can memorize this technique, this will be one of the greatest assets. Thanks!

  4. Karl J. Gephart 2013/08/27 7:43 pm

    Very nice article! You’ve saved me a lot of heavily coded work-arounds! Thanks!

  5. Peter Mumford 2013/08/27 7:56 pm

    brilliant! I’ve been stumped by this one for ages.

  6. Pavel Linhart 2013/08/27 9:58 pm

    Been using this for a few years now, quite handy – you can even make oldIE do the same trick by just adding zoom: 1; or any other safe hasLayout trigger that doesn’t break anything else.

    Also you should not need to set width to auto unless it has been set before :)

    However, it has its obvious drawbacks – now with text|box-shadows and other effects the hidden overflow is a bit of a bummer… The same effect can be emulated by some clever display: table + display: table-cell stuff, but that requires a bit more code and suffers from its own problems.

  7. Yes. Thank you so much for this tip, Jeff. I’m sure I’ll use it frequently.

  8. I never thought that this could be remedied so simple and clean. Thanks a lot!

  9. Paul Radzkov 2013/08/28 1:33 am

    In some cases when you have a lot of text in each li or small floated image it is better to set overflow:hidden on list items to avoid empty space under the image:

  10. Just remember that the text in the block element will no longer flow around the floated element. Which is kind of the idea with floats in the first place.

  11. I’ve been looking for a simple solution for this for ages.
    Thank you.

  12. I also have an additional remark :)

    This method is good when a list content shouldn’t be under the floated image. Because it will never be under it.

Comments are closed for this post. Something to add? Let me know.
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 more »
USP Pro: Unlimited front-end forms for user-submitted posts and more.
Take a screenshot with Firefox (no extension required). Open Developer Tools Settings and enable the “Take a screenshot” button. Then click the button :)
Take a screenshot with Chrome (no extension required). Open DevTools, type Cmd + Shift + P, then type screenshot.
After 10 years working on my 2010 iMac, my upgrade finally arrived. Shiny new iMac shipped from Ireland :)
Too much caffeine weirds me out. But I love the taste of coffee. So once in a while I enjoy a small cup of decaf. Hits the spot.
Chris Coyier is a truly awesome person. One of the finest people I've ever worked with. Just #gottasayit
Excel won't open CSV file because SYLK format? Open it with text editor and add an apostrophe ' at the beginning of the file, save changes, done.
Displaying too many social media buttons and links all over the place imho makes you look desperate and frankly kinda sad.
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.