[web] how to center div?

Started by
23 comments, last by ArchG 17 years, 10 months ago
Ah, I'd been needing this! I usually end up sticking my real div inside a div align:center div, which just seemed needless.
gsgraham.comSo, no, zebras are not causing hurricanes.
Advertisement
Quote:Original post by Sander
Quote:Original post by DigiDude
Quote:Original post by capn_midnight
<div align="center">blah</div>
What browser are you using? When I do that in FireFox the div stays left-aligned and the text inside is centered.


That's what it *should* do. The margin: auto is the correct way to do it, but IIRC Internet Explorer doen't like it (no surprise here...)
Only in quirks mode. Just stick any old doctype in and it works fine.

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

Quote:Original post by benryves
Quote:Original post by Sander
Quote:Original post by DigiDude
Quote:Original post by capn_midnight
<div align="center">blah</div>
What browser are you using? When I do that in FireFox the div stays left-aligned and the text inside is centered.


That's what it *should* do. The margin: auto is the correct way to do it, but IIRC Internet Explorer doen't like it (no surprise here...)
Only in quirks mode. Just stick any old doctype in and it works fine.
I used an HTML 4 doctype and an XHTML doctype and it still didn't work.

F-R-E-D F-R-E-D-B-U-R...G-E-R! - Yes!
I just stick <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"> on as that's what my editor defaults to and have no problems whatsoever.
Note that it's the very first on the page that is sent to the browser (no whitespace).
Example

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

To complete the list of options, another way of centering a (fixed width) div is to place its left side at 50% and then move it half its width back to the right by setting a negative margin:
<div style="position:relative;">  <div style="position:absolute;width:500px;left:50%;margin-left:-250px;">centered</div></div>
The align attribute for div was depreciated in 4.01. It is now not supported in XHTML 1.0 Strict. The "correct" way is with an auto margin on the left and right. Or to put it within a div that has
style="text-align: center;"
Quote:Original post by tstrimp
The align attribute for div was depreciated in 4.01. It is now not supported in XHTML 1.0 Strict. The "correct" way is with an auto margin on the left and right. Or to put it within a div that has
style="text-align: center;"


I would suggest doing this, as I have found most cross-browser rendering problems come from using margins, even between firefox and opera. As long as I avoid using the margin stylings completely, everything renders mostly the same.

[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

Regular margin styling has never been a problem for me. Using negative margings can get a bit funky at times though.

<hr />
Sander Marechal<small>[Lone Wolves][Hearts for GNOME][E-mail][Forum FAQ]</small>

Quote:Original post by Sander
Regular margin styling has never been a problem for me. Using negative margings can get a bit funky at times though.


It wasn't for me until I realized that for some reason my floated divs were dropping below the left hand column in IE. Also known as the IE double margin bug.
Yeah, I found out about that one the hard way myself as well [grin]

<hr />
Sander Marechal<small>[Lone Wolves][Hearts for GNOME][E-mail][Forum FAQ]</small>

This topic is closed to new replies.

Advertisement