Sub topic 13
Where flex, grid, flow-root, and contents fit in
This chapter assumes you know the CSS box model, can write selectors and rules, and have read the earlier chapters on block, inline, inline-block, and none. Here you will see how four newer display values relate to those four.
Outer and inner display
A display value describes two things at once.
- The outer display type says how the box behaves among its siblings. It is either block-level or inline-level.
- The inner display type says how the box lays out its own children. Examples are normal flow, flex, and grid.
The keywords you already know bundle both. block means a block-level box with normal flow inside. inline-block means an inline-level box with normal flow inside. The newer values change the inner type, the outer type, or both.
| Value | Outer type | Inner layout | Main use |
|---|---|---|---|
| `flex` | block-level | flex | One-dimensional rows or columns |
| `inline-flex` | inline-level | flex | A flex container inside a line of text |
| `grid` | block-level | grid | Two-dimensional rows and columns |
| `inline-grid` | inline-level | grid | A grid container inside a line of text |
| `flow-root` | block-level | normal flow, new formatting context | Containing floats and margins |
| `contents` | none of its own | none of its own | Remove the box, keep the children |
flex and grid replace many inline-block hacks
Earlier you fixed the whitespace gap between inline-block items. A flex container avoids the problem. Whitespace between flex items is not rendered, because each child becomes a flex item and not part of a line of text.
.row {
display: flex;
gap: 0.5rem;
}
.row > li {
padding: 0.5rem 1rem;
}Flex items ignore their own display for outer behavior. A span that is a child of a flex container accepts width and height even though its default is inline. The browser treats it as a flex item.
Use flex when you lay out items along one axis, such as a navigation bar. Use grid when you need to control rows and columns together, such as a page layout or a card gallery.
.gallery {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 1rem;
}flow-root contains its children
A block box does not grow to surround floated children. The parent collapses, often to zero height. display: flow-root makes the box create a new block formatting context. It then contains its floats and stops its children's margins from collapsing through it.
<div class="card">
<img class="thumb" src="photo.jpg" alt="A mountain lake">
<p>Text beside the floated image.</p>
</div>.card {
display: flow-root;
border: 1px solid #999;
}
.thumb {
float: left;
margin-right: 1rem;
}Without flow-root, the border would wrap only the paragraph. Older code used overflow: hidden or a clearfix for this. flow-root does the job without side effects such as clipped content.
contents removes the box but keeps the children
display: contents makes the element generate no box of its own. Its children are laid out as if they were children of the element's parent.
.wrapper {
display: contents;
}This differs from display: none, which removes the element and all its descendants. With contents, the element's own border, padding, background, and size no longer apply. The children still render.
Choosing among them
- Text-level content with no sizing needs:
inline. - A stacked section or paragraph:
block. - A small element in a line of text that needs width, height, or vertical padding:
inline-block. - Items in a row or column, or aligned along one axis:
flex. - A layout with both rows and columns:
grid. - A parent that must contain floats:
flow-root. - A wrapper that should not affect layout:
contents. - An element that should not exist in layout:
none.
Exercise
A div has a floated image inside it, and the div's border runs through the image instead of surrounding it. Which single display value on the div fixes this, and why?
Show answer
display: flow-root. It creates a new block formatting context, so the div grows to contain its floated children.
Exercise
How does display: contents differ from display: none on a wrapper div that holds three paragraphs?
Show answer
With contents, the div generates no box but the three paragraphs still render as if they were children of the div's parent. With none, the div and its paragraphs are all removed from layout.