Sub topic 1
How Elements Flow by Default
Prerequisites
This chapter assumes you already know:
- Basic HTML structure (elements, nesting) and CSS selectors.
- The CSS box model: content, padding, border, and margin.
- Common length units such as
px,%, andrem.
You don't need any knowledge of the position property yet. This chapter explains the layout that positioning changes.
What normal flow is
Normal flow is the layout the browser uses when you write no layout CSS. The browser reads your HTML in order and places each element after the previous one. Every other positioning tool in this tutorial is defined relative to this behavior, so it pays to understand it precisely.
Two rules do most of the work:
- Block-level elements stack vertically, one under another, from the top of their container.
- Inline-level elements sit side by side in a line and wrap to a new line when they run out of room.
Each element's display value decides which rule applies. A <div>, <p>, <ul>, and <h1> default to display: block. A <span>, <a>, <em>, and <strong> default to display: inline. You can change this with CSS, and the HTML tag name does not matter once you do.
Every element also has a position value. Its initial value is static, which means "lay this element out in normal flow." The offset properties top, right, bottom, left, and inset, and z-index, have no effect on a static element. Setting one of them and seeing nothing happen is one of the most common beginner errors, and this is the reason.
Block layout
A block-level box behaves like this:
- It starts on a new line and forces whatever follows onto a new line.
- Its
widthisauto, which means it stretches to fill the full width of its container's content box. - Its
heightisauto, which means it is only as tall as its content. - Its
width,height,padding,border, and all four margins are honored.
Because width: auto fills the container, a block does not need width: 100%. In fact, width: 100% plus horizontal padding or margin can make the box overflow its parent, since the percentage applies to the content box under the default box-sizing: content-box. Leaving the width as auto makes the browser subtract padding, border, and margin for you.
Inline layout
Inline content is laid out into line boxes. A line box is an invisible horizontal strip, as tall as its tallest item. The browser fills a line box with inline content until the next piece does not fit. Then it starts a new line box below. This is how a long sentence wraps, and an inline element such as <a> can even break across two lines.
Inline boxes have these rules:
widthandheightare ignored.- Horizontal margin and padding work.
- Vertical padding and border are drawn but do not push neighbouring lines apart.
- Vertical margin is ignored.
Inline-block combines the two models. An inline-block element sits in a line like text, but inside it behaves like a block, so it honors width, height, and vertical margins.
| `display` value | Starts a new line | `width` and `height` apply | Vertical margins apply |
|---|---|---|---|
| `block` | Yes | Yes | Yes (and they can collapse) |
| `inline` | No | No | No |
| `inline-block` | No | Yes | Yes (they do not collapse) |
The following page shows all three in one place. Save it as an .html file and open it in a browser.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Normal flow</title>
<style>
.box {
background: #dbeafe;
border: 1px solid #1d4ed8;
padding: 8px;
margin: 8px 0;
}
.tag {
background: #fde68a;
width: 300px; /* ignored: inline */
margin: 20px; /* only left and right take effect */
}
.chip {
display: inline-block;
width: 80px;
height: 40px;
background: #bbf7d0;
}
</style>
</head>
<body>
<div class="box">Block one</div>
<div class="box">Block two</div>
<p>
Text with <span class="tag">an inline span</span> inside a paragraph,
followed by <span class="chip">chip</span> <span class="chip">chip</span>.
</p>
</body>
</html>The two .box elements stack and fill the page width. The .tag span ignores its width and gets 20px of space on its left and right only. The two chips sit on one line and keep their 80px by 40px size.
Margin collapsing
Vertical margins of block boxes in normal flow can collapse. Instead of adding together, they overlap and the result is a single margin. Only vertical margins collapse. Horizontal margins never do.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Margin collapsing</title>
<style>
.a { margin-bottom: 30px; background: #fecaca; }
.b { margin-top: 20px; background: #bfdbfe; }
</style>
</head>
<body>
<div class="a">A</div>
<div class="b">B</div>
</body>
</html>The gap between A and B is 30px, not 50px. The rule for two adjoining margins is:
- Both positive: the larger one wins.
- Both negative: the one with the larger absolute value wins.
- One positive and one negative: the two values are added together.
Margins collapse in three situations:
- Adjacent siblings. The bottom margin of one block and the top margin of the next, as above.
- Parent and first or last child. If the parent has no border, padding, or content between its edge and the child's margin, the child's margin "escapes" through the parent. The parent then appears to have that margin instead, and the child does not move inside it.
- Empty blocks. A block with no height, padding, or border has its own top and bottom margins collapse together.
The second case causes the most confusion. You add margin-top to a child, and the whole parent moves down rather than the child moving inside it. Any of the following stops the escape:
- Give the parent a border or padding.
- Make the parent a new block formatting context, for example with
display: flow-root. - Make the parent a Flexbox or Grid container. Flex and Grid items never collapse their margins with each other or with their container.
.parent {
display: flow-root; /* contains the child's margins and floats */
}flow-root has no visual side effects, which makes it the cleanest fix. A block formatting context is a region of the page in which block boxes are laid out independently of the outside. Margins do not collapse across its boundary.
Inline gaps you did not ask for
Because inline and inline-block elements are laid out as text, whitespace in your HTML counts. In the earlier example, the space between the two <span class="chip"> elements renders as a gap about as wide as a space character. Deleting the whitespace between the tags removes the gap, and so does making the parent a flex container.
A similar surprise happens with images. An <img> is inline by default and sits on the text baseline. Below the baseline, the line box reserves room for descenders, which are the tails of letters like "g" and "y". This makes a few pixels of empty space appear under an image that is the only content of a block. Setting img { display: block; } or img { vertical-align: top; } removes it.
How elements leave the flow
Normal flow is a default, not a rule you must follow. Several CSS features change how an element takes part in the flow:
floatmoves a box to one side and lets inline content wrap around it. The float is taken out of the normal vertical stacking.position: absoluteandposition: fixedremove the element from flow entirely. Its siblings are laid out as if it did not exist, and it takes up no space.position: relativeandposition: stickykeep the element in flow. Its original space is reserved, and only the painted position shifts.display: noneremoves the element from layout and from painting.visibility: hiddenhides it but keeps its space.
Whether an element takes up space in the flow is the central question for the next chapters. Keep it in mind as you read them.
Common errors
widthorheightdoes nothing. The element is probablydisplay: inline. Change it toblockorinline-block.- Vertical margin on a link or span has no effect. Inline boxes ignore vertical margins. Use
inline-block, or use padding. - Spacing between stacked blocks is smaller than expected. The margins collapsed. Check whether you want one larger gap, or use
gapin a flex or grid container. - A child's
margin-topmoves the parent. Parent-child margin collapse. Usedisplay: flow-rooton the parent. top: 10pxorz-index: 5does nothing. The element is stillposition: static. These properties need a positioned element, which the following chapters cover.
Summary of the model
- Normal flow places block boxes top to bottom and inline content left to right, wrapping into line boxes.
position: staticis the initial value and means the element stays in normal flow, with offsets andz-indexignored.- Vertical margins between block boxes can collapse, and
display: flow-root, padding, or a border on the parent stops the parent-child case. - Elements leave the flow with
float,absolute, orfixed. Relative and sticky elements stay in the flow.
Exercise
A <span> styled with width: 200px; margin-top: 30px; shows no change on the page. Give the cause and two ways to fix it.
Show answer
A <span> is display: inline by default. Inline boxes ignore width, height, and vertical margins. Fix it by setting display: inline-block (or display: block if it should sit on its own line). Alternatively, make its parent a flex container, which turns the span into a flex item that honors width and margins.
Exercise
Two stacked blocks have margin-bottom: 40px on the first and margin-top: 25px on the second. How large is the gap between them? What is the gap if the second margin is -10px instead?
Show answer
The gap is 40px, because adjoining positive margins collapse to the larger one. With -10px, one margin is positive and one is negative, so they are added together and the gap is 30px.
Exercise
A child div inside a parent div has margin-top: 24px. The parent has no padding or border. Instead of the child moving down inside the parent, the whole parent moves down. Why does this happen, and what one-line change to the parent fixes it?
Show answer
The child's top margin collapses with the parent's top margin because nothing separates them, so the combined margin appears outside the parent. Adding display: flow-root to the parent makes it a new block formatting context, which stops the collapse and keeps the 24px margin inside the parent.