How to center a div with CSS in Mozilla Firefox and Internet Explorer

You know, when it comes CSS (Cascade Style Sheets, or style sheets) Internet Explorer browser is among the worst.

Do not parse correctly (by the book W3C) CSS properties and reads them in his own way.

Firefox is much more adherent to the official standard, but not everyone uses Firefox.

So, when you need to center an element <div> or another things get complicated. To avoid the typical <center> </ center> and have the compatibility of all browsers there is a way, but you have to do a little trick.

Since I happen to write it here as it is often personal reminder, perhaps useful to someone else.

The "trick" is that.

We must create 2 <div> one inside the other, like this:

<div id="contenitore1">
<div id="contenitore2">Contenuto, testo, foto, ecc...</div>
</div>

Then you need to create styles in the header of the page or in a file. Css in part:

# {Contenitore1
text-align: center; / * for Internet Explorer * /
}

# {Contenitore2
margin: 0 auto; / * standard alignment in CSS * /
text-align: left; / * compensate center alignment * /
/ * ADDED ONLY TO SEE BETTER EXAMPLE: * /
border: 2px solid # 000;
width: 50%;
}

That's it, the trick is all here.

Good alignment!

Related Articles:

[?]
Share This

7 Responses

  1. Andrea Says:

    You are my savior. Grandeeeeeeeeee

  2. Mark Says:

    You were very clear! Thanks a lot!

  3. andre Says:

    Grazieeeeeeee finalmentee has moved! you're great!

  4. en666 Says:

    Tested, works perfectly ... damn IE ...

  5. Andrea Says:

    thank you, good advice! you made me save time with an elegant solution ;)

  6. Scelzen Says:

    Hello I put a slideshow on my blogger widget
    but with the classic center> <center
    only works on chrome
    and not on mozilla and explorer
    what do you think?
    http://www.mondotorrent.blogspot.com
    take a look and let me know what you can do to please! Regards

  7. GabryXL Says:

    I was not no saint was to be pulled down: D thanks, I finally resolved and the myriad of posts that are on the internet, this is the fastest, most intuitive and effective, even for a nabbo like me ... thanks again

Leave a Comment

Please note: Comment moderation is enabled and May delay your comment. There Is No Need To Resubmit your comment.

Improve the web with Nofollow Reciprocity.