Understand how elements occupy space in normal flow.
Normal flow is the browser’s default layout. Block elements stack one after another, while text and inline elements share lines.
The examples here use horizontal writing, with text read from left to right.
.notes {
max-width: 36rem;
padding: 1rem;
border: 1px solid green;
}With the browser's usual display styles:
strong, and the link share lines inside those blocks.The layout follows the content's structure. You do not need to give each paragraph a coordinate.
The section has no set height, so it grows with its content. If its available width shrinks, the paragraphs wrap onto more lines and become taller. Later blocks move downward to make room.
This is a useful default for articles and cards with variable text. Test the example with a longer heading or another paragraph before deciding that the section needs a fixed height.
A containing block is the box CSS uses to calculate sizes and positions. Here, the paragraphs use the parent section’s content area as their reference.
.notes p {
width: 50%;
}With a 16-pixel root font size, the section’s maximum content width of 36rem is 576 pixels. At that width each paragraph measures 288 pixels across. Narrow the section to 400 pixels of content width and each paragraph measures 200 pixels. They still stack: making blocks narrower does not automatically place them side by side.
Positioning can change which ancestor supplies this reference. The Positioning and Stacking lesson traces the reference for a label anchored to a card.
In a separate example, use two adjacent paragraphs in ordinary block flow:
.first-note {
margin: 0 0 24px;
}
.second-note {
margin: 16px 0 0;
}Their adjoining vertical margins collapse into one margin. Because both are positive, the gap is 24 pixels, the larger value, rather than 40 pixels.
Margin collapse has boundaries and conditions:
If the section has a top border or top padding, its first child's top margin cannot collapse through that edge.
When collapsing margins include negative values, combine the largest positive value with the most negative value. If the second paragraph's top margin changes from 16px to -8px, the collapsed margin becomes 24px - 8px = 16px. If all the collapsing margins are negative, use the most negative value: -24px and -8px collapse to -24px.
Absolute and fixed positioning remove a box from normal flow: its former place is not reserved for neighboring content. Relative positioning keeps the original space even when the box is visually offset. Floats allow surrounding inline content to wrap around them.
Those behaviors solve particular layout problems. Begin with normal flow, then choose another behavior when the content needs it.
Try it yourself
HTML + CSS