Normal Flow, Floats and Legacy Layout
Learning Objectives
- Understand normal document flow.
- Understand block and inline formatting.
- Use float and clear for appropriate layouts.
- Recognize older layout techniques and modern alternatives.
1. Normal Document Flow
Normal flow is the default way elements are laid out when they are not positioned or arranged by another layout system. Block-level boxes typically stack vertically, while inline content flows along lines within a block.
<div>First block</div>
<div>Second block</div>
<p>Paragraph text flows within lines.</p>2. Block and Inline Behavior
.block { display: block; }
.inline { display: inline; }
.inline-block { display: inline-block; }Block boxes typically start on a new line. Inline boxes participate in inline formatting. Inline-block elements participate inline but support box dimensions.
3. Float
Float was designed primarily to let content, such as text, wrap around an element.
.article-image {
float: left;
width: 180px;
margin: 0 16px 12px 0;
}Subsequent inline content can flow around the floated image. Float is still useful for this text-wrapping behavior, but it is usually not the best tool for full page layouts.
4. Clear
.footer {
clear: both;
}The clear property prevents an element from sitting alongside relevant floats on the specified side.
5. Containing Floats
.article {
display: flow-root;
}Display: flow-root establishes a new block formatting context and is a straightforward modern way to contain floats without adding unnecessary markup.
6. Legacy Clearfix
.clearfix::after {
content: "";
display: table;
clear: both;
}Clearfix is a historical technique for containing floated children. In new code, flow-root is often simpler.
7. Modern Layout Alternatives
.toolbar {
display: flex;
align-items: center;
gap: 16px;
}
.gallery {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 16px;
}