CSS, Web Development

How to counter IE6 extra width / height bug?

As always, IE6 is really a big headache for web developers like me. One of the old IE6 bug is the extra width / height bug on CSS. Actually it is not just extra, but it actually doubles the padding or margin.

To counter that bug, it requires a simple rule:

If your element has width, you must not apply any horizontal margin or padding. Example, if your div has width, then you must not put margin-left, margin-right, padding-left and padding right.

For height, you must not put any vertical margin or padding like margin-top, margin-bottom, padding-top and padding bottom.

What if you really have to put width and margins? Well, to save your IE6, add some extra wrapper. Example:

If you have this markup:

<div class="some-container"> 
    <span class="lbl1">your label here</span> 
    <span class="lbl2">another label</span> 
</div> 

And of course wants to apply some width and margins like this:

.some-container {
    display: block;
    width: 500px;
    float: left;
}

.lbl1 {
    width: 100px;
    margin-left: 10px;
}

.lbl2 {
    width: 200px;
    maring-left: 10px;
}

That would perfectly work in Firefox, Safari and other modern browsers like IE7 and IE8. However, in IE6, that 10px margin would become 20px, which for me is a big deal.

To solve that, we must have some additional markup. Apply width to an element, then inside that element, apply the margin.

<div class="come-container"> 
    <div id="div1"> 
        <span>your label here</span> 
    </div> 
    <div id="div2"> 
        <span>another label</span> 
    </div> 
</div> 

And the CSS would be:

.some-container {
    float: left;
    width: 500px;
}

.div1 {
    width: 100px;
}

.div1 span {
    margin-left: 10px;
}

.div2 {
    width: 200px;
}

.div2 span {
    maring-left: 10px;
}

That would make my file much larger, but just to serve IE6, you still have to consider it. For me, I would choose to make my design run on most browser, than having is load fast but ugly.

Anyway, that is just a small piece of markup, which would not reach 1KB perhaps, so come to think of it? Will you still support IE6?

2 thoughts on “How to counter IE6 extra width / height bug?”

  1. Thanks for visiting bro! Display inline for a float? Hmmm…. Would that validate anyway?

    However, my CSS for the container is incorrect. Got to change it, since it is floated but has no width.

    Thanks for posting the link bro! Bookmarked 🙂

Leave a reply

Your email address will not be published. Required fields are marked *