Ioverflow: hidden
(and overflow: auto
) clearing hack.
Jeff Starr’s recent and excellent post – The New Clearfix Method – and the ensuing comments were enough to finally prompt me to write about it here.
(And since I started writing this, Jonathan Snook has started a Twitter dialog about overflow vs. clearfix).
While the clearfix method is a tried and true hack,
I’ve always disliked muddying up my HTML markup with crufty “clearfix” classes strewn about.
So I ended up using overflow: hidden
as much as I could.
But overflow: hidden
is not without its drawbacks.
Although there is no extra class to apply in the HTML (win!),
there may be situations when you want to have child elements positioned partially (or entirely)
outside of their overflow: hidden
wielding parent (or other ancestor) container.
In these cases, the container with overflow: hidden
will clip the element that you’re trying to partially (or entirely) position outside of it.
(Case in point: If you use suckerfish dropdowns, try setting overflow: hidden
on the outermost list).
So although overflow: hidden
is not usable in all situations, I used to prefer it over using the “clearfix” class method.
But in reality I ended up with both: I had overflow: hidden
where I could get away with it,
and “clearfix” classes where I couldn’t use overflow: hidden
.
It bothered me a bit to mix and match two different clearing methods,
but I was happy to have fewer “clearfix” classes violating my otherwise semantic markup Kumbaya festival.
Then in his presentation at An Event Apart 2008 in San Francisco,
Dan Cederholm suggested using the class name “group” rather than “clearfix” –
a suggestion which later made it into his book Handcrafted CSS.
It’s a minor thing, but I do like the improved semantics (in most cases) of the “group” class name over the “clearfix” class name,
so I adopted this approach and felt okay about using it as a fallback when overflow: hidden
wasn’t feasible.
In his talk and in his book, Cederholm also walks through the “big long list” idea of applying the rules for the clearfix (renamed to “group”) class directly to the selectors that need them in the CSS.
This keeps the HTML more pure while avoiding the drawbacks of overflow
– a total win-win.
But it does mean extra bloat in the CSS.
Probably okay for smaller sites, but it can quickly get unruly with larger sites.
I tried this approach on a few projects and decided the CSS bloat was indeed too much,
so I stuck with the mix of overflow: hidden
and the “group” class.
The Catalyst
But I’m now saying goodbye to overflow: hidden
and the deciding factor for me is CSS3.
Specifically box-shadow
.
At least in the sense that box-shadow
was the first property I noticed being negatively impacted by the use of overflow: hidden
.
Like the positioned child elements mentioned above, box-shadow
can get clipped when the parent (or other ancestor) element has overflow
applied.
And there are several other things to consider as we move forward using more CSS3.
Text-shadow
and transform
can also potentially be clipped by overflow: hidden
(see the demo).
As we rely more and more on CSS3 properties we can rely less and less on overflow
as a clearing method.
So I’ll be abandoning overflow and relying upon clearfix (Jeff Starr edition)
via a mix of the “.group” class in the markup and the “big long list” approach.
Suggested Reading
- Handcrafted CSS: More Bulletproof Web Design (book) by Dan Cederholm. Includes and example of the ‘.group’ clearfix class.
- Bulletproof Web Design: Improving flexibility and protecting against worst-case scenarios with XHTML and CSS (book) by Dan Cederholm.
- Designing with Web Standards (3rd Ed.) (book) by Jeffrey Zeldman.
- The New Clearfix Method (article) by Jeff Starr.