Sub topic 13
Where flex, grid, flow-root, and contents fit in
This chapter assumes you know the earlier material in this tutorial: the block, inline, inline-block, and none values, and the CSS box model (content, padding, border, margin). It shows how the newer display values extend that model. You will see which problems from earlier chapters they solve and which new problems they introduce.
The two-part model behind display
The values you have used so far can be read as a pair of choices. The first choice is the outer display type. It controls how the element's own box behaves among its siblings. It is either block-level (starts on a new line, fills the available width) or inline-level (sits in a line of text). The second choice is the inner display type. It controls how the element lays out its children.
Modern CSS lets you write both parts. The single-keyword values you already know are shorthand for two-keyword forms.
| Single keyword | Outer type | Inner type | Notes |
|---|---|---|---|
| `block` | block | flow | Children use normal flow |
| `inline` | inline | flow | Box sits inside a line |
| `inline-block` | inline | flow-root | Inline-level, but a self-contained block inside |
| `flex` | block | flex | Children become flex items |
| `inline-flex` | inline | flex | A flex container that sits in a line |
| `grid` | block | grid | Children become grid items |
| `inline-grid` | inline | grid | A grid container that sits in a line |
| `flow-root` | block | flow-root | Block that contains its floats |
| `contents` | none | none | The element's own box disappears, its children stay |
| `none` | none | none | Neither the element nor its children render |
Browser support for the two-keyword syntax, such as display: inline flow-root, is good in current browsers. The single-keyword forms remain the common way to write these values, so use them unless you want to make the two parts explicit.
display: flex
Setting display: flex makes the element a block-level box whose children are laid out along one axis, by default in a row. The element itself behaves like a block among its siblings. The change applies to its direct children, which become flex items.
Three effects matter for this tutorial.
- Flex items are blockified. A
spanthat is a direct child of a flex container gets a computeddisplayofblock. This meanswidth,height, and vertical margin all apply to it, which is not true of a normal inlinespan. - Whitespace between items is not rendered. The gap you met with inline-block does not appear in a flex container, because whitespace-only text between items is ignored.
- Properties that only make sense in a line are ignored.
floatandvertical-alignhave no effect on flex items.
Here is the navigation fix from earlier chapters, written with flex. The gap property sets the spacing, so the markup whitespace no longer matters.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Flex row</title>
<style>
.row {
display: flex;
gap: 0.5rem;
list-style: none;
padding: 0;
}
.row li {
width: 6rem;
padding: 0.5rem;
background: #dde;
text-align: center;
}
</style>
</head>
<body>
<ul class="row">
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
</body>
</html>The li elements are already block-level by default, so the width would apply either way. The flex container is what places them in one row, and the spacing is exactly the gap value.
Use inline-flex when you want the container itself to sit inside a line of text, for example an icon plus a label inside a paragraph.
display: grid
Setting display: grid also creates a block-level container, but its children are placed into rows and columns that you define. Flex lays out along one axis. Grid lays out along two. Grid items are blockified in the same way as flex items, and the same whitespace and vertical-align rules apply.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Grid</title>
<style>
.cards {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 1rem;
}
.cards div {
padding: 1rem;
background: #def;
}
</style>
</head>
<body>
<div class="cards">
<div>A</div>
<div>B</div>
<div>C</div>
<div>D</div>
<div>E</div>
</div>
</body>
</html>This produces three equal columns, and the fifth item wraps to a second row. Choose flex when content size should drive the layout, such as a toolbar. Choose grid when you want to define the structure first and fit content into it, such as a page or a card gallery.
display: flow-root
flow-root is the value to reach for when a block needs to contain what is inside it. It makes a block-level box that establishes a new block formatting context. A block formatting context is a region of layout whose contents do not interact with the outside. Two practical results follow.
- The box grows to include floated children. Without it, a parent holding only floats collapses to zero height.
- Margins of the first and last child do not collapse through the parent's edges.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>flow-root</title>
<style>
.panel {
border: 2px solid #333;
}
.panel.fixed {
display: flow-root;
}
.panel img,
.panel .thumb {
float: left;
width: 80px;
height: 80px;
background: #aac;
}
</style>
</head>
<body>
<p>Without flow-root, the border hugs nothing:</p>
<div class="panel"><div class="thumb"></div></div>
<p>With flow-root, the border surrounds the float:</p>
<div class="panel fixed"><div class="thumb"></div></div>
</body>
</html>In the first panel the float does not count toward the parent's height, so the border collapses to a thin line. In the second, display: flow-root makes the panel enclose the float. Older code used hacks such as overflow: hidden or a clearfix pseudo-element for this job. flow-root does it without side effects like clipping.
This also explains the table above. inline-block is an inline-level box with a flow-root interior, so its contents never leak out into the surrounding line. That is why an inline-block accepts width and height and keeps its children together.
display: contents
display: contents removes the element's own box but keeps its children. The element generates no box, so the children are laid out as though they were children of the element's parent. In the DOM and the CSS cascade, the element still exists. Inheritance still passes through it, and its selectors still match.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>contents</title>
<style>
.toolbar {
display: flex;
gap: 1rem;
}
.group {
display: contents;
border: 3px solid red;
padding: 2rem;
}
.toolbar button {
padding: 0.5rem 1rem;
}
</style>
</head>
<body>
<div class="toolbar">
<div class="group">
<button>Cut</button>
<button>Copy</button>
</div>
<button>Paste</button>
</div>
</body>
</html>All three buttons become flex items of .toolbar. The .group wrapper contributes nothing to layout, and its border and padding are not drawn, because there is no box to draw them on.
This differs from display: none in one respect. With none, the element and all its descendants disappear. With contents, only the element's own box disappears and the descendants still render.
Gotchas to check
- A flex or grid container only changes its direct children. Grandchildren keep their normal display types unless the child is also a container.
- The blockification of flex and grid items means a
spanchild can suddenly acceptwidth. This is intended behavior, but it can surprise you when you remove the container later. display: contentsignores the element's box properties, includingwidth,margin,padding,border, andbackground. It does not ignore inherited properties likecolorandfont-size, which still flow to the children.- A
displayvalue set on an element also overrides the browser default for hidden-style elements. For example, settingdisplay: flexon an element that carries the HTMLhiddenattribute makes it visible again, because your rule beats the browser's[hidden] { display: none }default.
Choosing among the values
Use the earlier chapters' rules first, then extend them.
- Flowing text or a short run of inline content: leave it
inline. - A stacked content block: leave it
block. - A small item in a line that needs width, height, or padding, such as a badge:
inline-block, orinline-flexif its contents also need aligning. - A set of items in one row or column, with spacing that does not depend on markup whitespace:
flex. - A two-dimensional arrangement:
grid. - A block that must contain its floats or isolate its margins:
flow-root. - A wrapper that should disappear from layout while its children stay:
contents, with accessibility testing. - An element that must take no space and be unavailable to everyone:
none.
Exercise
A parent div contains only one child, which is floated left with a fixed height of 100px. The parent has a visible border but renders as a thin line. Which single display value on the parent fixes this, and why does it work?
Show answer
Set display: flow-root on the parent. It creates a new block formatting context, and a block formatting context includes floated children when it computes its height. The parent's border then surrounds the 100px float.
Exercise
You wrap three buttons in a div and set display: contents on the div, along with a red border and padding of 2rem. The div sits inside a flex container. What happens to the border and padding, and how are the buttons laid out?
Show answer
The border and padding are not drawn, because display: contents means the div generates no box. The three buttons become flex items of the outer flex container, as though the div were not there.