Understanding margin 0 auto

What exactly is happening when you type margin 0 auto

The most common way to center elements in CSS, is to write margin:0 auto;, or to use the margin-left:auto; margin-right:auto; which is almost the equivalent of the first deceleration.

The Margin shorthand property sets all four values in one deceleration.

margin:0 auto;

The above declaration sets the top margin to 0, and the left and right margins to auto. This is because the first value is copied for the opposite value. This means that a declaration like the below is just a waste of bandwidth.

margin:0 auto 0 auto;

When you Remember that the order of the declaration is: top, right, bottom, left, and that the first value just gets copied, it will be a lot easier to save yourselves from unnecessary CSS codes.

Post comment

Links that you insert are not nofollowed, but will be removed by admins if they are considered spam.

[url=Absolute URL for page]TITLE[/url]

You should insert code boxes around code examples, which will be automatically syntax highlighted.

[code1 html|css|javascript|php|sql]Your Code Here[/code1]

You may want to read our Privacy Policy before submitting your comment.