Try adding:
display:inline-block;
in #divtwo. If that works, I would of course add some conditional commenting for IE7
Try adding:
display:inline-block;
in #divtwo. If that works, I would of course add some conditional commenting for IE7
I don't know if this fixes your real problem, but I fixed your sample like this:
This makes it look like your chrome sample both in Firefox and IE 7 (browsers I tested).
I am unsure what your ultimate goal is here, but I would suggest enclosing all four <div>
s inside a container element and applying a width to it, then removing the clear
style from #divthree
. Doing it this way will allow #divthree
and #divfour
to move below #divone
and #divtwo
without clearing them:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>IE Float Test</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<style type="text/css">
#divone, #divtwo, #divthree, #divfour
{
width: 100px;
height: 100px;
color: white;
font-size: 3em;
float: left;
}
#divone
{
background-color: red;
}
#divtwo
{
background-color: blue;
}
#divthree
{
background-color: green;
}
#divfour
{
background-color: purple;
}
#container {
width: 200px;
zoom: 1;
}
</style>
</head>
<body>
<div id="container">
<div id="divone">one</div>
<div id="divtwo">two</div>
<div id="divthree">three</div>
<div id="divfour">four</div>
</div>
</body>
</html>
The zoom
property on #container
is necessary to avoid the IE6/7 Escaping Floats Bug.
If the above solution isn't viable, you can add a <br>
or <div>
after #divtwo
with the style clear: left;
:
<div id="divone">one</div>
<div id="divtwo">two</div>
<br style="clear: left;" />
<div id="divthree">three</div>
<div id="divfour">four</div>
This is the technique used in a floated page layout example on westciv.com.
Several months later...
I gave up on trying to solve this problem with CSS alone. This is an IE7 bug that you cannot avoid without touching your HTML.
The ultimate application of this floating pattern was in a form, where two of the div
s were input elements and the other two div
s were their corresponding labels. The pattern was used multiple times in a large form and I really wanted to find an elegant CSS-only solution.
I eventually ended up using the Object Oriented CSS Framework and wrapping all the elements with additional div
s in order to create the desired layout, as OOCSS dictates. It was the only way to save my soul from IE7's CSS hell and OOCSS is not so bad once you complete your initial layout.
In truth, the general answer is that this is the sort of question you pose when you don't really know what you're doing with CSS. If you have to create a complex layout once in a blue moon then you probably don't know what you're doing; as was the case when I asked this question.
Although it is true that IE7 fails to render the CSS properly, it was also a case of mistaken scope on my part. CSS is not the ultimate layout language that a naive programmer would make it out to be and CSS is not truly independent of the structure of your HTML. Once again I've opted to take the easier way out by using the OOCSS framework when I should actually take the time to learn the fundamentals of CSS.
Alas, such is the consequence of deadlines.