Nullcreations.net | Ramblings & Photography of Jerrett Taylor

Clearing floated elements without extra markup

January 16th, 2008 | 0 comments

Clearing floating elements is pretty commonly handled by a a div or br tag with 'clear: both'.

Recently I ran into a problem with this; the layout I was working with was using floats for the sidebar and part of the main content had floated list of images. Using the clear both trick caused it to clear past the bottom of the sidebar.

While there are ways around that I've never been a big fan of adding the clearing divs, so I was pretty happy to find an alternative, and even happier to find one that's nice and simple.

1
2
3
4
<ul style="overflow: auto; zoom: 1;">
  <li style="float: left">A</li>
  <li style="float: left">B</li>
</ul>

Basically all you have to do is set the overflow to hidden, which forces the parent to expand to contain all it's children. The zoom: 1 is an IE6 specific property, which is needed to trigger IE's hasLayout.

There are lots of other posts on the topic, if you are feeling like doing some more reading.

0 Comments

Musings

04.14.08

Trying to decide where to travel to? Wikitravel.org Random Page has the answers!

02.23.08

sd's embedded_actions has a new home on github

02.18.08

Very cool.

02.17.08

Open Source Food? - looks like a nice recipe site!

02.15.08

For anyone using capistrano with more than one target for deployment (staging, production), check out cap multistage

02.15.08

Wow, I just realized how bad my code blocks break my blog in safari. I should fix that!

01.27.08

Mike has created another nice netbeans theme - go check it out, if you use netbeans!

01.21.08

A handy tool, Rubular is a web based regex editor

01.15.08

Dan put together a sweet code editor plugin for mce with syntax highlighting etc.

12.13.07

On OSX? Add RubyImporter to Spotlight and search your source!

12.04.07

sd has released a new version of embedded_actions

11.27.07

RejectConf videos are online thanks to confreaks!

11.19.07

Good news - we upgraded a project at work to rails 2.0 rc2 and the speed to run specs dropped from 30-35 seconds to 10-15 seconds!

11.08.07

Eigenclass.org has a great big list of Ruby 1.9 changes that is being kept up to date!

11.06.07

Rein is working on a trac replacement in ruby called eskort , and it looks cool. More info on his goals