Careeroza
Margins & Padding
Shorthand Values
| Shorthand | Means |
|---|---|
margin: 10px; | all four sides 10px |
margin: 10px 20px; | top & bottom 10px, left & right 20px |
margin: 10px 20px 30px; | top 10px, left & right 20px, bottom 30px |
margin: 10px 20px 30px 40px; | top, right, bottom, left (clockwise) |
margin: 0 auto; | centers a block element that has a width |
Padding uses exactly the same patterns. Single sides: margin-top, padding-left, etc.
Centering and Margin Collapse
Example
Vertical margins between blocks collapse: 30px and 20px give a 30px gap, not 50px:
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<style>
.center { width: 60%; margin: 0 auto; background: #d0ebff; padding: 8px; }
.a { margin-bottom: 30px; background: #ffe8cc; }
.b { margin-top: 20px; background: #d3f9d8; }
</style>
</head>
<body>
<div class="center">Centered with margin: 0 auto</div>
<p class="a">margin-bottom: 30px</p>
<p class="b">margin-top: 20px (the gap above is 30px)</p>
</body>
</html>Result