PDA

View Full Version : Basic CSS question regarding Margin: auto



jamessillcock
Sep 20th, 2010, 01:38 PM
Hi,

Sorry if this is a really basic answer to this, but if on a div the Margin is set to auto (margin: auto) what does this mean? Does this give the div a default number of pixels as the margin? If so, does anyone know exactly the pixel number that the margin is given?

effpeetee
Sep 20th, 2010, 03:07 PM
Hi,

Sorry if this is a really basic answer to this, but if on a div the Margin is set to auto (margin: auto) what does this mean? Does this give the div a default number of pixels as the margin? If so, does anyone know exactly the pixel number that the margin is given?
This may help. Click here. (http://archivist.incutio.com/viewlist/css-discuss/89909)Frank

Donkey
Sep 20th, 2010, 04:03 PM
margin: auto only works if you also give the item being centred a width. This can be in fixed measurements like pixels or "fluid" measurements like percentages. The "auto" setting then takes the width of the centred item from the width of it's container, divides it by two and applies the result to the left and right margin. There is no default margin width equivalent to auto, it varies depending on the browser window size.

If you are centring a container inside a body which is 100% wide then the container must be less than 100% wide otherwise it will not be centred.

e.g.

an 80% wide container will have margins of 10% each side.

a 700px wide container will have margins of 50px each side in an 800px wide window, but if the window is 1200pixels wide then the margins will be 250px each side.