Margin is the space outside an element's border.
Individual sides
margin-top, margin-right, margin-bottom, margin-left
Shorthand
margin: 20px;— all sidesmargin: 10px 20px;— top/bottom, left/rightmargin: 10px 20px 30px;— top, left/right, bottommargin: 10px 20px 30px 40px;— top, right, bottom, left (clockwise)
Centre a block
width: 300px; margin: 0 auto; centres a block horizontally.
Margin collapse
Vertical margins of two blocks touching each other combine: 20px and 30px give a 30px gap, not 50px.
Example
Example — Centring with margin auto
HTML
<style>
.card {
width: 260px;
margin: 20px auto;
padding: 16px;
background: #fff7d6;
border: 1px solid #e0c96b;
text-align: center;
}
</style>
<div class="card">I am centred with margin: 20px auto;</div>