Click to See Complete Forum and Search --> : Centering a div in FF and IE


Sunny G
09-21-2005, 12:34 PM
Hi everybody!
I'm sure this question has been asked a million times, so I'll ask it again for the 1,000,001th time. How can I center a single div in a page using only CSS, so that it will work in FF and IE? In the past I've done this:

body {
text-align:center;
}
div {
margin:auto;
}
It works in IE, but not Firefox.
Any ideas?

Fang
09-21-2005, 12:59 PM
Give it a width

Sunny G
09-21-2005, 02:21 PM
Give it a width
In the troublesome page it has width:auto; already.

jogol
09-21-2005, 02:55 PM
Centered with fixed width (http://intensivstation.ch/files/templates/temp09.html)
Centered with dynamic width (http://intensivstation.ch/files/templates/temp10.html)

toicontien
09-21-2005, 03:01 PM
Sunny G, DIV tags are block elements and take up as much width as allowed by their containing block. This is the default behavior if the DIV is not floated or positioned. So width: auto; takes the browser default (as much width as allowed). You need to specify a set width, something in px, %, em, en, in, cm, etc.

Floated and positioned block elements work a little differently. They only take up as much width as their contents' longest line of text/inline elements, and as much height as needed to display the contents.

Kravvitz
09-21-2005, 05:19 PM
How to Center Elements with CSS (in IE5+/Win and other browsers) (http://www.dynamicsitesolutions.com/css/center_element/)

Sunny G
09-30-2005, 01:12 PM
Soooo... I gave all that a try and I had no luck. That div still won't center.
What else can I do?

Kravvitz
09-30-2005, 02:22 PM
Did you set a width on the div?

Please show us your code.