Sub topic 1
How elements flow by default
This chapter covers normal flow, the layout every element follows until you change its position. Every later positioning value is defined by how it departs from this default, so it helps to be exact about it first.
Prerequisites
This chapter assumes you already know:
- Basic HTML structure and CSS selectors.
- The CSS box model: content, padding, border, and margin.
- CSS length units such as
px,%, andrem.
What normal flow is
Normal flow is the browser's default layout algorithm. If you write no layout CSS, the browser places elements one after another in the order they appear in the HTML. Each element's position depends on the elements before it, so adding or resizing one element moves the ones that follow.
Every element has a position value. The default is static, which means "place this element in normal flow and ignore top, right, bottom, left, and z-index." You rarely write position: static, but you will use it to cancel a positioning rule set elsewhere.
Block and inline boxes
Normal flow treats two kinds of boxes differently.
| Box type | Typical elements | How it is placed | Width and height |
|---|---|---|---|
| Block | `div`, `p`, `h1`, `ul`, `section` | Starts on a new line and stacks vertically | Fills the available width by default. You can set width and height. |
| Inline | `span`, `a`, `strong`, `em` | Sits in a line of text and wraps at line ends | Sized by its content. `width` and `height` have no effect. |
Block boxes stack from top to bottom inside their parent. Inline boxes run from left to right (in a left-to-right language) and wrap onto a new line when the line is full.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Normal flow</title>
<style>
.block { background: #dbeafe; margin-bottom: 8px; padding: 8px; }
.inline { background: #fde68a; }
</style>
</head>
<body>
<div class="block">Block one</div>
<div class="block">Block two</div>
<p>
Text with an <span class="inline">inline span</span> and
<span class="inline">another span</span> in the same line.
</p>
</body>
</html>Open this in a browser. The two blocks sit on separate lines and stretch across the page. The spans stay inside the sentence and only take up the width of their text.
How the flow is calculated
The browser works through the document in order. The diagram shows the decisions it makes for each element.
Three consequences follow from this model:
- An element takes up space. Its parent grows to contain it, and later siblings are pushed out of its way.
- Vertical margins between adjacent block siblings can collapse. Two touching margins of 20px and 30px produce a 30px gap, not 50px.
- The parent's height usually comes from its children. If you do not set a height, adding content makes the parent taller.
Why this matters for positioning
Setting position to anything other than static changes how an element relates to this flow. The next chapters cover each value in detail, but the contrast is worth stating now:
relativekeeps the element's space in the flow. Only the drawn position changes.absoluteandfixedremove the element from the flow. Siblings behave as if it does not exist.stickystays in the flow, but it can be held in place during scrolling.
The key question for each of these is whether the element still takes up space where it would normally sit.
Seeing flow break and recover
You can test the idea with a short experiment. Add a class to the second block in the earlier example:
.block.moved {
position: absolute;
top: 0;
right: 0;
}The second block leaves the flow. Block three, or the paragraph if there is no third block, moves up into the gap it left. That jump is the visible sign that the element no longer takes up space.
Exercise
A div has position: static and top: 40px. Where does the browser draw it, and why?
Show answer
It is drawn at its normal flow position. Offset properties like top only apply to elements whose position is not static, so the browser ignores top: 40px.
Exercise
Why does setting width: 200px on a span inside a paragraph not change how it looks, and what is one way to make the width apply?
Show answer
A span is an inline box, and inline boxes ignore width and height. Changing it to display: inline-block or display: block makes the width apply.