Today i discovered something interesting with div’s.
Imagine that you have the following code:
<div id='mainDiv'> <div style='float:left'>Div #1</div> <div style='float:right'>Div #2</div> </div>
And you wanted that the div named “mainDiv” to have a background color. Well in IE7 everything works fine with the xhtml code
above. But in FireFox the “mainDiv” there is an unusual behaviour.
Below is a image that ilustrates what you get in Firefox (on the left) and what you get using IE7 (on the right) with the xhtml code above:
Notice that the main div, in IE7, warps all content, but in Firefox the div remains with a minimum height.
Io solve the problem you could try (has i did) to put a height in the “mainDiv”. Although it worked (for the content that i
had inside the Div), you never know what the the height of the mainDiv is going to be.
The correct solution for the problem must be one that adapts the height the mainDiv to the maximum height of it’s content.
The solution is simple:
<div id='mainDiv'> <div style='float:left'>Div #1</div> <div style='float:right'>Div #2</div> <div style='clear:both'></div> </div>
Adding the empty div with the style property “clear” set to “both” makes the mainDiv’s height extend to the full height of
it’s childs, making the xhtml generic for all browsers.
(UPDATE)
Due to the several solutions in the comments i will comment there here one by one.
Alternate Solution 1: the clearFix : thank to Joao A.
The solution in in fact very elegant and we only need to add something to your style sheet. The solution objective is to add
the pseudo element :after into that
“mainDiv”:
#mainDiv:after {
content: ".";
line-height: 0;
height: 0;
visibility: hidden;
display: block;
clear: both;
}
The pseudo element objective is to add some content after the element “mainDiv”. So the soluction passes by adding the
content “.”, removing the height of the content, hide that content and finnally clear both side of the div. in fact we are
doing the same has the original soluction without adding the extra div to the markup.
Alternate Solution 2: adding the float:left style to the “mainDiv” element : thanks to Vítor Pires.
This solution is actually very simple:
The solution is simple:
<div id='mainDiv' style='float:left' > <div style='float:left'>Div #1</div> <div style='float:right'>Div #2</div> <div style='clear:both'></div> </div>
Alternate Solution 3: adding the overflow style to the “mainDiv” element : thanks to Jeff and href=’http://btm.anfo.pl/’ target=’_blank’>BTM.
very simple just like the previous one. Add the overflow style property with the value ‘hidden’ or ‘auto’.
The solution is simple:
<div id='mainDiv' style='overflow:hidden;' > <div style='float:left'>Div #1</div> <div style='float:right'>Div #2</div> <div style='clear:both'></div> </div>
The only difference bettween the usage of overflow:hidden and overflow:auto is just that if you have a fixed height in the mainDiv, with overflow:hidden, the content will the content that surpasses the size of the div will be hidden, but with overflow:auto a sroll will appear.
The Solution 2 and the solution 3 are actually very good (and simple) to use in specific situation. if you want the content of the mainDiv to appear below it, than use solution 3 (or 1). Otherside, if you want the content to appear side by side with the div, use the float property in the main element.
Are you sure that the IE7 behaviour is the correct one? It makes more sense for me FF’s behaviour, but I would have to check the CSS documentation to be sure…
The Firefox behaviour is the correct one as specified by the standard.
I’d rather use a clearfix class on the main div, instead of adding the extra markup.
Search google for ‘clearfix css’ (sorry, I’m on a phone without copy/paste
The simplest solution is to float the mainDiv as well.
Try this:
Div #1
Div #2
Of course you should float left or right depending on your layout.
This works on all browsers.
Second try (I imagine you strip tags):
|div id=’mainDiv’ style=’float:left’|
|div style=’float:left’|Div #1|/div|
|div style=’float:right’|Div #2|/div|
|/div|
#mainDiv {overflow:hidden;}
isn’t amazing that we are still discussing techniques in CSS to accomplish what was trivial back when we used tables for layout
@jarek jacek: that will fail in IE6/7 in 75% situations - when there are positioned elements in the div, when you’ll mix left/right float etc. + it’s better to use overflow:auto than hidden.
Mind Booster Noori: I also don’t believe that the behaviour in IE7 is the correct one according to the standard.
What i mean by: “(…) in IE7 everything works fine (…)” was “(…) in IE7 the result was the one i wanted(…)”.
Carlos: Thanks for the confirmation
Wow thanks. I just ran into this today while designing a theme for wordpress using YUI grids for the layout. The Blog navigation area is this exact scenario and I was shocked to see my beloved Firefox acting this way. I thought IE was rendering the xhtml correctly and FF was breaking it. My fix was to add a top border with the background color. It worked but I was not happy at all with this situation. STANDARDS people we need standards.
Thanks for this list of tips I used the float:left!
I’m glad this post helped you dameryworld. It was it’s propose
Nuno, I believe that you’ve forgotten the overflow style on the 3r example.
I believe the after pseudo-element won’t work in IE 6…but it should work in IE7
Hi Luís,
Yes i did forgot. Thanks for the correction.
I didn’t tested the above examples on IE6. They were only tested in IE7 and Firefox.
[...] Wrapp a div around other div’s with float [...]
The author uvazhuha for literacy)))
Стандартная тема блогу не идёт, чесс говоря
здесь видел ет gamebulletin.ru