Saturday, June 14, 2014

CSS > margin: 0 auto

You may have ever wondered why there're float: left; and float: right; but no float: center;, there's actually a easy way to center an element: margin: 0 auto;, which will automatically counts margin-left and margin-right according to parent's width while margin-top and margin-bottom remain 0.

Similarly, if you want to center the element vertically too, just set margin: auto auto;.

No comments:

Post a Comment