htmlcssinternet-explorer-6

IE6 layout issue - absolute positioning


The following HTML looks as required in Firefox 2 & 3 and IE7. The Left button is on the left, the Right button is on the right, and the text in the middle is...in the middle!

However on IE6 the Left button is misaligned - it appears center-aligned.

Can anyone suggest why??

<!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>
    <title>Layout problem!</title>
    <style type="text/css">
        DIV#Footer
        {
            padding: 10px;
            color: #fff;
            background-color: #484848;
            position: relative;
            text-align: center;
        }
        DIV#Footer INPUT
        {
            margin: 5px 15px;
            position: absolute;
            top: 0px;
        }
        DIV#Footer INPUT.right
        {
            right: 0px;
        }
        DIV#Footer INPUT.left
        {
            left: 0px;
        }
    </style>
</head>
<body>
    <div id="Footer">
        <input class="left" type="button" value="Left" />
        Some text in the middle
        <input class="right" type="button" value="Right" />
    </div>
</body>
</html>

(I have been using the IE Developer tool to try and analyse and fix this, to no avail...)


Solution

  • You must trigger the hasLayout property (an IE thing…) of #footer. Width and height trigger it, if you don't want to set a width or height, you can use the IE-only zoom property in CSS.

    <!DOCTYPE html>
    <html>
    <head>
        <title>Layout problem!</title>
        <style type="text/css">
            div#footer
            {
                padding: 10px;
                color: #fff;
                background-color: #484848;
                position: relative;
                text-align: center;
                zoom: 1;
            }
            div#footer input
            {
                margin: 5px 15px;
                position: absolute;
                top: 0;
            }
            div#footer input.right
            {
                right: 0;
            }
            div#footer input.left
            {
                left: 0;
            }
        </style>
    </head>
    <body>
        <div id="footer">
            <input class="left" type="button" value="Left">
            Some text in the middle
            <input class="right" type="button" value="Right">
        </div>
    </body>
    </html>
    

    IIRC, in IE, elements have two different layout behaviors, one if hasLayout is true, and another if it's false. Making sure that it's set to true can fix a lot of weird layout issues like this one.