/    Sign up×
Community /Pin to ProfileBookmark

Keeping containers from collapsing: Float left/right vs overflow hidden

We’ve all seen this before:

[code=html]
<div id=”outer”>
<div style=”float: left; width: 50%;”>Left Col</div>
<div style=”float: left; width: 50%;”>Right Col</div>
</div>
[/code]

The inner divs will float left, but #outer will collapse. In the past I have used this technique to keep #outer from collapsing:

[CODE]
#outer {
float: left;
width: xx;
}
[/CODE]

It works fine, but what I end up with is a whole pile of nested block elements that are floating left/right to keep themselves from collapsing.

I’ve seen people use this technique before, and it seems to work fine:

[CODE]
#outer {
overflow: hidden;
}
[/CODE]

What do you guys think is the proper way to handle the situation? It comes up SO often that for years I used the former technique without even thinking about it.

to post a comment
CSS

5 Comments(s)

Copy linkTweet thisAlerts:
@slaughtersOct 21.2009 — Clear your first float. [CODE]<div id="outer">
<div style="float: left; width: 50&#37;; clear:left">Left Col</div>
<div style="float: left; width: 50%;">Right Col</div>
</div>[/CODE]


**EDIT **

For the sake of tidiness you should probably clear:right your very last column too
Copy linkTweet thisAlerts:
@jarcoalauthorOct 21.2009 — Clear your first float. [CODE]<div id="outer">
<div style="float: left; width: 50%; clear:left">Left Col</div>
<div style="float: left; width: 50%;">Right Col</div>
</div>[/CODE]


**EDIT **

For the sake of tidiness you should probably clear:right your very last column too[/QUOTE]


This does not appear to deliver the desired results.

I have done some research/testing in the meantime, and it appears that the overflow solution does not work with IE 6/7, so I guess that's off the table. I did find this (http://stackoverflow.com/questions/218760/how-do-you-keep-parents-of-floated-elements-from-collapsing) which does bring to light some other solutions, none of them are terribly pleasant though.
Copy linkTweet thisAlerts:
@slaughtersOct 21.2009 — This does not appear to deliver the desired results...[/QUOTE]Oops. In my test I had another tag in the container DIV which was not floating.

But - the overflow:hidden in the container seems to work for IE7 (well - IE8 compatibility view since I no longer have IE7 installed) if you are not in quirks mode: http://www.stansight.com/tmp.html
Copy linkTweet thisAlerts:
@jarcoalauthorOct 21.2009 — Oops. In my test I had another tag in the container DIV which was not floating.

But - the overflow:hidden in the container seems to work for IE7 (well - IE8 compatibility view since I no longer have IE7 installed) if you are not in quirks mode: http://www.stansight.com/tmp.html[/QUOTE]


Yeah that works in my IE 7 browser as well, so it looks like it's just IE 6 where it fails. I would kill not to have to support either of those browsers, but the boss says both -_-.
Copy linkTweet thisAlerts:
@nabecaOct 22.2009 — for what I have seen from good codes, they never add styles divs. Just give an ID and do your work in css.

Btw, you can do this.

"outer" float left

"innerleft" float left

"innerright" float right

floating the "outer" to the left is essential here.

also, do not forget to do "clear:both or right/left" for the element that comes after "outer"
×

Success!

Help @jarcoal spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...