Sub topic 3
display: block
This chapter covers block-level boxes: how they size themselves, how they stack, and how margins behave between them. It assumes you know basic HTML elements, how to write CSS rules with selectors and properties, and the box model (content, padding, border, margin). It also assumes you have read the earlier chapters on what display controls and on default display values.
What a block box is
An element with display: block generates a block-level box. Three rules follow from that:
- The box starts on a new line and the content after it also starts on a new line.
- With
width: auto, the box stretches to fill the width of its containing block, which is usually the content area of its parent. - Width, height, margin, padding, and border all apply on all four sides.
Many elements are block by default, including div, p, h1 to h6, ul, ol, li (technically list-item, which behaves as block for layout), section, header, footer, and form. The value comes from the browser's default stylesheet. You can give any element display: block, and you can override the default on any block element.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Block basics</title>
<style>
.box {
background: #dbeafe;
border: 2px solid #1d4ed8;
padding: 0.5rem;
margin: 0.5rem 0;
}
.narrow {
width: 12rem;
}
</style>
</head>
<body>
<div class="box">Fills the full width</div>
<div class="box narrow">Fixed at 12rem, still on its own line</div>
<div class="box">Fills the full width again</div>
</body>
</html>The narrow box does not let the next box sit beside it. Even with free space on the right, the next block starts on a new line. A block box claims its whole row regardless of its width.
How block boxes stack
In normal flow, block boxes in the same container are laid out one after another from top to bottom, in source order. This is called block layout. Each box's margin edge begins where the previous box's margin edge ends. The exception is margin collapsing, covered below.
flowchart TDThe diagram below shows the structure of a typical container.
Anonymous block boxes
A block container holds either block-level children or inline-level content, not a mix. If you mix them, the browser wraps the loose inline content in an anonymous block box.
<div class="mixed">
Loose text before
<p>A real block</p>
Loose text after
</div>The two text runs each get an anonymous block box. You cannot select these boxes with CSS, but they explain why loose text breaks onto its own lines around the p.
Width: auto versus 100%
width: auto is the default for block boxes. It does not mean "100% of the parent". It means "fill the available space, after subtracting my own margin, border, and padding". Setting width: 100% instead fixes the content width to the parent's width, and the padding and border are added on top.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>auto vs 100%</title>
<style>
.parent {
width: 20rem;
background: #f3f4f6;
margin-bottom: 1rem;
}
.child {
padding: 1rem;
border: 4px solid #b91c1c;
background: #fee2e2;
}
.auto { width: auto; }
.full { width: 100%; }
</style>
</head>
<body>
<div class="parent"><div class="child auto">width: auto fits inside</div></div>
<div class="parent"><div class="child full">width: 100% overflows</div></div>
</body>
</html>With the default box-sizing: content-box, the second child is 20rem of content plus padding and border, so it sticks out of its parent on the right. There are two fixes. Remove the width and use auto, or set box-sizing: border-box so that the width includes padding and border.
*,
*::before,
*::after {
box-sizing: border-box;
}Height
Block height is auto by default. The box grows to fit its content, so you rarely need to set it. Two behaviors cause surprises:
- A percentage
heightonly works if the parent has a definite height. If the parent's height depends on its content, the percentage is treated asauto. - A fixed
heightsmaller than the content makes the content overflow. It is not clipped unless you setoverflow.
.card {
height: 6rem; /* fixed */
overflow: auto; /* scroll instead of spilling out */
}Prefer min-height when you want a floor but still want the box to grow with its content.
Centering a block horizontally
A block box with a set width and margin-left and margin-right set to auto takes the leftover space equally on both sides.
.page {
width: 40rem;
max-width: 100%;
margin-inline: auto;
}This only works when the width is not auto. With width: auto, the box already fills the container and there is no free space to split, so auto margins resolve to 0. Using max-width instead of width lets the box shrink on small screens while still centering on large ones.
Auto margins do not center a block vertically in normal flow. Vertical auto margins compute to 0.
Margin collapsing
Vertical margins of block boxes in normal flow can merge. The merged margin is the larger of the two, not their sum.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Margin collapsing</title>
<style>
.a { margin-bottom: 30px; background: #dcfce7; }
.b { margin-top: 20px; background: #fef9c3; }
</style>
</head>
<body>
<p class="a">First paragraph</p>
<p class="b">Second paragraph</p>
</body>
</html>The gap between the paragraphs is 30px, not 50px. Margin collapsing happens in three situations:
- Adjacent siblings. The bottom margin of one block meets the top margin of the next, as above.
- Parent and first or last child. If the parent has no padding, no border, and no separating content, the child's top margin merges with the parent's top margin. The child's margin appears to "leak" out of the parent.
- Empty blocks. A block with no height, padding, or border has its own top and bottom margins collapse together.
Some details matter in practice:
- Only vertical margins collapse. Horizontal margins never do.
- When one margin is negative, the result is the larger positive margin plus the negative one. When both are negative, the result is the more negative one.
- Padding or border on the parent stops parent and child margins from merging, because they separate the two margins.
- Margins do not collapse across floats, absolutely positioned boxes, inline-blocks, or between a flex or grid container and its items.
.panel {
display: flow-root; /* new formatting context: child margins stay inside */
}display: flow-root creates a new block formatting context, which contains the child's margins. A later chapter returns to flow-root.
Forcing an inline element to be block
Setting display: block on an element that is inline by default makes it behave as a block. A common case is a link that should be a full-width row.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Link as block</title>
<style>
a.row {
display: block;
padding: 0.75rem 1rem;
border-bottom: 1px solid #d1d5db;
text-decoration: none;
color: #1f2937;
}
a.row:hover { background: #f3f4f6; }
</style>
</head>
<body>
<a class="row" href="#one">First item</a>
<a class="row" href="#two">Second item</a>
<a class="row" href="#three">Third item</a>
</body>
</html>The whole row is clickable, because the link's box now spans the full width and includes its padding. The HTML meaning does not change. The element is still a link, and assistive technology still announces it as one. display changes layout, not semantics.
Note one exception. Some display values on certain elements, such as table elements with display: block, can cause browsers to drop table semantics from the accessibility tree. Test with a screen reader if you override display on tables, lists, or buttons.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `margin: 0 auto` does not center | Width is `auto`, so there is no free space | Set `width` or `max-width` |
| Child sticks out past its parent | `width: 100%` plus padding and border with `content-box` | Use `width: auto` or `box-sizing: border-box` |
| Gap smaller than the sum of two margins | Margin collapsing | Use padding, `gap` in a flex or grid parent, or one-sided margins |
| Child's top margin moves the parent down | Parent and first-child margin collapse | Add parent padding or border, or use `display: flow-root` |
| `height: 100%` has no effect | Parent height is not definite | Give the parent a height, or use `min-height` or another layout method |
A habit that avoids many of these bugs is to space blocks with margin in one direction only, for example margin-bottom on every block or a single margin-top. Then collapsing rarely comes into play.
Exercise
A div has width: auto, padding: 10px, and border: 2px solid, inside a parent that is 300px wide. A second div in the same parent has width: 100%, the same padding and border, and box-sizing: content-box. What is the content width of each box, and does either overflow?
Show answer
The first div's content width is 300 - 20 - 4 = 276px, and it fits exactly. The second div's content width is 300px, so its total width is 300 + 20 + 4 = 324px. It overflows the parent by 24px on the right. Using box-sizing: border-box on the second div, or switching it to width: auto, removes the overflow.
Exercise
Two sibling paragraphs have margin-bottom: 24px on the first and margin-top: 16px on the second. Both are block boxes in normal flow with no padding or borders between them. What is the vertical gap between them, and how would you get a 40px gap instead?
Show answer
The gap is 24px, because adjacent vertical margins collapse to the larger value. To get 40px, use a single margin of 40px on one side, or stop the collapse by putting the paragraphs in a flex or grid container, where item margins do not collapse.