Solved

Margin is too large in IE6

Posted on 2004-10-13
5
386 Views
Last Modified: 2008-02-01
I've got a simple test page that contains 4 divs.  The first 3 are floated left and have margin-left of 50px, and the last one clears the float and also has a margin-left of 50px.  What I expect is for the first and last divs to line up, since they both have the same margin on the left side.  Mozilla seems to do this, IE6 does not.  I've got a strict doc type, so I assume it's not a quirksmode issue.  I assumed it was an IE bug having to do with float touching the clearing element, but even if I set a margin-top on .clear, the divs didn't line up.  What am I missing?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
<style type="text/css">
.myFloat {
  float: left;
  margin-left: 50px;
  width: 150px;
  height: 175px;
  border: 1px solid black;
}

.clear {
  clear: both;
  width: 600px;
  margin-left: 50px;
  margin-right: 50px;
}
</style>
</head>

<body>
<div class="myFloat">TEST</div>
<div class="myFloat">TEST</div>
<div class="myFloat">TEST</div>

<div class="clear">CLEAR</div>
</body>
</html>
0
Comment
Question by:dakyd
  • 2
  • 2
5 Comments
 
LVL 3

Accepted Solution

by:
Kupi earned 250 total points
ID: 12305840
It's an IE bug but there's a workaround ;)

Just add a "display: inline;" to your myFloat class declaration, like this:

.myFloat {
  display: inline; /* prevent IE doubled float-margin bug */
  float: left;
  margin-left: 50px;
  width: 150px;
  height: 175px;
  border: 1px solid black;
}

For more info please visit:
http://www.positioniseverything.net/explorer/doubled-margin.html
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 12309343
The bug in IE is that it does the clear wrong.  To line them up just float the "clear" div:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
<style type="text/css">
.myFloat {
  float: left;
  margin-left: 50px;
  width: 150px;
  height: 175px;
  border: 1px solid black;
}

.clear {
  float: left;
  width: 600px;
  margin-left: 50px;
  margin-right: 50px;
}
</style>
</head>

<body>
<div class="myFloat">TEST</div>
<div class="myFloat">TEST</div>
<div class="myFloat">TEST</div>

<div class="clear">CLEAR</div>
</body>
</html>
0
 
LVL 19

Author Comment

by:dakyd
ID: 12309359
Yup, that did the trick.  Funny, I was looking through Holly & John's site before I posted, but I didn't see that section.  Thanks though, that makes one more random IE bug I know how to kill.  Cheers.
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 12309377
changing the display to inline is not necessary and my cause other rendering problems if you have to put other elements inside the final div.

Cd&
0
 
LVL 19

Author Comment

by:dakyd
ID: 12309452
Oops, sorry Cd&, I didn't notice your post there.  According to the CSS2 spec (and the link that Kupi posted), when an element is positioned with float: left:

The element generates a block box that is floated to the left. Content flows on the right side of the box, starting at the top (subject to the 'clear' property). The 'display' is ignored, unless it has the value 'none'.

The last line, in this case, indicates that there won't be any rendering problems, but for some odd reason, it means that IE behaves properly.

I did try your suggestion, but it creates a horizontal scrollbar in IE, which wasn't the original intention.  Thanks for the input, though.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating a CSS block that only applies to printing By default, all of your CSS applies to every possible view of your page - whether on screen, printed, landscape, touch-screen, or whatever.  You can, however, add CSS that only applies under certai…
When applying CSS to your HTML, there are many different ways to select which element(s) the CSS applies to.  Some of these selectors are more commonly known and used than others - Here are the more common ones: #X - Matches an ID of X .X - Matche…
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question