Very short trick: how to center elements on the page, even on IE5

It's not that I always take it with Internet Explorer, it's just that damn thing every Web development (HTML and CSS) seems not work with the browser - still - the most common (one day someone will explain why). ; ad esempio ecco come posizionare un div al centro dello schermo: A very convenient method to place an item in the middle of the browser window is to the property sfrutture auto style margin , for example, here is how to position a div in the center of the screen:

1
2
3
"box" > < div id = "box">
p > < p > The content of the DIV </ p >
</ div >
1
width : 300px ; height : 200px ; margin : 0 auto } div # box {width: 300px; height: 200px; margin: 0 auto}

Unfortunately (needless to say) that does not work on IE5! One way to overcome this problem is to use an additional "defect" in the use of IE5 style text-align . In practice it would be:

1
2
: center } body {text-align: center}
width : 300px ; height : 200px ; margin : 0 auto ; text-align : left } div # box {width: 300px; height: 200px; margin: 0 auto; text-align: left}

In all honesty I personally refuse to write "this zozzata", but it may always come in handy. However the purpose of this "trick" was precisely bring to light the technique of margin auto !

3 comments to: "Very short trick: how to center elements on the page, even on IE5"

  1. December 5, 2008 Frederick :

    Just trick. When necessary, I try to avoid the text-align the body and create a div with text-align center and width 100% where then insert the div to center.

    You say that you refuse to accept this "zozzata" (and how to blame you) .. so how do you do when you need to hit? You plug your nose or have other methods?

  2. December 5, 2008 Giovambattista Fazioli :

    @ Fred: I know it will seem absurd, but when I can, IE does not consider it right! Indeed, to tell you the truth, now for me is basically the anti-IE browser! . However, as an alternative there are also tables, maybe even more "zozzata" the body with text-align:center . But yeah, you said well you ... when I am forced, I plug my nose :)

  3. December 8, 2008 Frederick :

    tables? I pretend not to have read .. ;)

    I just wanted to be sure there was some other method. Thank you for your reply! I think your a brave (not to be considered ie) .. I read the statistics of my sites, and unfortunately it seems to me that the time is not ripe yet. But I'm confident, you must stand still for long.

Leave a comment

TAG XHTML PERMITS: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre> // blocco generico <code></code> // blocco generico [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL 


Stop SOPA