Margin 0 auto in Internet Explorer

I dont know how I missed this fix(!) of so common problem for these long days. Whenever I started some template related work i found IE the most irritating. What I am talking about is using Margin: 0 auto, mostly used to center a container ( DIV or table anything ). But before few days ago I believed that IE is not gonna support this easy way and i must have to write some additional IE fixing in my css files. However now i got to know that the DOCTYPE type declaration is the thing I was using incorrectly. As soon as I fixed my DOCTYPE declarations the IE started working perfectly. However in a nutshell it is really a better practice to use proper DOCTYPE in your web applications. Please refer to the wiki for detail understanding of using DOCTYPE. Here I am giving a code snap. Just run it in your IE with and without the DOCTYPE declaration at top and watch out the differences.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html>
    <head>
    <style type="text/css">
    .container{
        margin: 0 auto;
        border: 1px solid blue;
        height: 200px;
        width: 200px;
    }
    </style>
    </head>
    <body>
        <div class="container">
            This is a long test to check the cenerification of a DOM container in IE
        </div>
    </body>
</html>

You know what!! after this I started liking IE a little bit ;-D

Comments

  1. Yeah, good tip. I almost always discarded the importance of the DOCTYPE element. Good to know it makes IE behave in a better way.

    ReplyDelete

Post a Comment

Popular posts from this blog

Ajax form submission with tiny MCE editor

Samsung BADA, Starting with sample application

Bada tutorial: Application basics