Is "margin: 0 auto" default for CSS divs?
I just took over a new project. The guy before me coded a lot o开发者_高级运维f:
div#headertop {
margin: 0 auto;
}
Is this really needed or is this the default anyway?
No, it is not default, unless he also set
div {
margin: 0 auto;
}
This is needed to center divs
on the page, provided there is also a width
set. The top is given no margin
and the left and right are set automatically in relation to the page/containing element (based on the width of the div
)
All current browsers have margin: 0
as the default margin for div
elements.
margin: 0 auto
is not the same thing -- if the div has a fixed width, margin: 0 auto
creates a div
that is horizontally centered in its container.
margin: 0 auto;
is a common practice for horizontally centering a particular element.
The default for a div
is to be 100% wide, so centering it horizontally as a default does not make any sense.
精彩评论