📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials CSS Mastery Margin and Padding

Margin and Padding

5 min read
margin adds space outside an element. margin: 0 auto centres a block element horizontally. Vertical margins between adjacent elements collapse to the larger value. padding adds space inside the element's border.

Margin and Padding

/* Shorthand: top right bottom left */
margin:  10px 20px 10px 20px;
padding: 10px 20px;       /* top/bottom | left/right */
margin:  10px;            /* all sides */

margin-top:    16px;
margin-right:  auto;      /* auto centers block elements */
margin-left:   auto;

/* Remove browser defaults */
margin: 0;
padding: 0;