Margins, overflow, and visibility
W3C CSS Snapshot 2026
Learn where a box takes up space and what happens to content at its edges.
On this page
Center a block with auto margins
A width smaller than the containing block leaves space at the sides. margin-inline: auto divides that space equally. In horizontal writing, margin: 0 auto does the same and also sets top and bottom margins to zero.
How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Center a block with auto margins</title>
<style>
.card { width: min(240px, 100%); box-sizing: border-box; margin-inline: auto; padding: 10px; background: #def5f5; }
</style>
</head>
<body>
<p class="card">Desk Lamp</p>
</body>
</html>
What to expect The card stays centered and shrinks on narrow previews.
Observe vertical margin collapse
In normal block flow, adjoining vertical margins can collapse into one. When both are positive, the larger one sets the gap. Flex and grid items keep their margins separate.
How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Observe vertical margin collapse</title>
<style>
.first { margin: 0 0 24px; }
.second { margin: 16px 0 0; }
p { background: #def5f5; }
</style>
</head>
<body>
<main><p class="first">Pen</p><p class="second">Notebook</p></main>
</body>
</html>
What to expect The gap is 24px. Add main { display: grid; } to get 40px.
Choose an overflow value
visible can draw beyond the box. hidden clips and allows programmatic scrolling. clip prevents scrolling. auto allows users to scroll when content exceeds the box. scroll makes a scroll container even when all content fits.
How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Choose an overflow value</title>
<style>
.card { width: min(220px, 100%); height: 58px; overflow: auto; border: 1px solid teal; padding: 6px; box-sizing: border-box; }
</style>
</head>
<body>
<p class="card" tabindex="0">Pen<br>Notebook<br>Desk Lamp<br>Folder<br>Stapler</p>
</body>
</html>
What to expect Scroll inside the card to see all the products. Compare auto with hidden and visible.
Hide with display or visibility
display: none removes the box from layout. visibility: hidden hides the box and keeps its layout space.
How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Hide with display or visibility</title>
<style>
.row { display: flex; gap: 4px; margin-bottom: 6px; }
.kept > :nth-child(2) { visibility: hidden; }
.removed > :nth-child(2) { display: none; }
.row > div { background: #d8efeb; padding: 6px; border: 1px solid teal; }
</style>
</head>
<body>
<div class="row kept"><div>Pen</div><div>Notebook</div><div>Folder</div></div><div class="row removed"><div>Pen</div><div>Notebook</div><div>Folder</div></div>
</body>
</html>
What to expect The first row leaves a gap for Notebook. The second row has no Notebook gap.