user1040259 user1040259 - 1 month ago 11
CSS Question

CSS margin auto center with top margin - VALID?

Is this valid CSS to center the div and also apply a top margin?

div {
margin: 0 auto;
margin-top: 30px;
}

Answer

Use the following to specify margins:

div { margin : 30px auto 0 auto; } /* margin: [top] [right] [bottom] [left] */

Or, it's shorthand:

div { margin: 30px auto; } /* expands to  margin:30px auto 30px auto; */

Or, list them out explicitly:

div { margin-left: 30px margin-right: 30px; margin-top: auto; margin-bottom: auto; }

The choice is yours.