</> Code Corner

CSS · CSS Tutorial · Lesson 9

CSS Margins

Space outside an element's border.

Margins create space around elements, outside of any defined borders.

Example

<style>
div { border: 1px solid black; background: lightblue; }
.a { margin: 25px 50px 75px 100px; }
.b { width: 300px; margin: 20px auto; }
</style>
<div class="a">top 25, right 50, bottom 75, left 100</div>
<div class="b">Centered with margin: auto</div>
Shorthand Meaning
margin: 25px; All four sides
margin: 25px 50px; Top/bottom 25, left/right 50
margin: 25px 50px 75px; Top 25, left/right 50, bottom 75
margin: 25px 50px 75px 100px; Top, right, bottom, left (clockwise)

Margin Collapse

Keep reading “CSS Margins”

Sign up to read the full lesson (1 more parts), try every example in the live editor, and take the 5-question quiz.

Chat with us 👋