Tsallaka zuwa abun ciki
Justice Tech School

Margin Collapse and Auto Margins

Vertical margins of neighbouring blocks collapse into the larger one. margin: 0 auto centres a block with a set width. Negative margins pull elements closer.

Example
<style>
.a, .b { background: #1e293b; padding: 6px } .a { margin-bottom: 30px } .b { margin-top: 20px } .c { width: 120px; margin: 10px auto; background: #0f766e; text-align: center }
</style>
<div class="a">30px margin below</div><div class="b">20px above (gap is 30, not 50)</div><div class="c">centred</div>

Exercise

Centre a block horizontally.

margin: 0 ;

Quiz Test

What happens to 30px and 20px vertical margins?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.