Centering with CSS

S

somebody

I want to center a div using CSS. Is it possible? Horizontal centering that
is.

See I have a div that is 700px wide, and I want to center it in the middle
of the page.
 
M

Matthias Gutfeldt

somebody said:
possible.

I come here because I CBF reading. Just give me the answer.

You asked whether it's possible. The answer is yes. Next question?


Matthias
 
S

somebody

Matthias Gutfeldt said:
You asked whether it's possible. The answer is yes. Next question?

Ok, how do i do it? I tried Google before posting of course and tried using
padding 20% on the block or margin 20% on the body and it wasn't working.
 
D

Dylan Parry

somebody said:
Ok, how do i do it? I tried Google before posting of course and tried
using padding 20% on the block or margin 20% on the body and it wasn't
working.

If you bother to read the other reply in the thread you will find the
answer.
 
L

Leif K-Brooks

somebody said:
See I have a div that is 700px wide, and I want to center it in the middle
of the page.

div {
margin : auto;
}

But you shouldn't have a fixed-width div.
 
J

Jeremy C. Morgan

I want to center a div using CSS. Is it possible? Horizontal centering that
is.

See I have a div that is 700px wide, and I want to center it in the middle
of the page.

if you do

text-align: center;

in the body class, you can. Thats the only way I know of.


:: Jeremy Morgan ::
Self Proclaimed Expert
Web Developer
http://www.webfootcentral.com
 
S

Steve Pugh

And if the browser window is less than 700 pixels wide? Do you want to
the div to still be centered?
if you do

text-align: center;

in the body class, you can. Thats the only way I know of.

That only works in buggy browsers. Good browsers would center the
contents of the div within the div but leave the div itself in its
default left alignment.

The standard compliant way would be to apply margin-left: auto; and
margin-right: auto; to the div to be centered. This will work in
Gecko, Opera, IE5 Mac and IE6 Win (assuming Standards mode is
triggered by an appropriate Doctype). To make it work in IE5 we do
indeed need to add text-align center to the div's parent element, but
we then need to add text-align: left; to the div itself in order to
keep the text within the div left aligned in browsers that actually
properly understand basic CSS.

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top