CSS display: flex vs inline-flex
To use Flexbox, set display: flex or display: inline-flex on the parent. Both arrange its direct children as flex items. In normal flow, the values differ in whether the container itself behaves as a block or participates in a line of surrounding text.
Distinguish the container’s outer and inner layout
| Value | Outer layout of the container | Inner layout of its children |
|---|---|---|
flex |
Block-level in normal flow | Flexbox |
inline-flex |
Inline-level in normal flow | Flexbox |
A block-level flex container with auto width fills the available width in normal block flow. An inline-flex container sizes using its content and the space available. A container that is itself a flex or grid item can have different sizing and outer display behavior. Both values use row by default. See CSS Display · Outer Display Type and CSS Flexbox · Flex Containers.
Compare the same items in three layouts
Save the code as flex-display.html and open it. The three layouts use the same items and need no additional files.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Compare flex container display values</title>
<style>
body { margin: 20px; font: 16px/1.6 system-ui, sans-serif; color: #203348; }
* { box-sizing: border-box; }
h1 { font-size: 24px; }
h2 { margin: 18px 0 8px; font-size: 18px; }
.examples { max-width: 560px; }
.demo { padding: 8px; border: 1px solid #7d9db2; background: #edf4f8; }
.item { padding: 8px; background: #176b86; color: white; }
.normal .item + .item { margin-top: 8px; }
.flex { display: flex; gap: 8px; min-height: 100px; }
.inline-flex { display: inline-flex; gap: 8px; vertical-align: middle; }
.inline-line { margin: 0; }
</style>
</head>
<body>
<h1>Compare display layouts</h1>
<div class="examples">
<h2>Normal block flow</h2>
<div class="demo normal">
<div class="item">A</div><div class="item">B</div><div class="item">C</div>
</div>
<h2>display: flex</h2>
<div class="demo flex">
<div class="item">A</div><div class="item">B</div><div class="item">C</div>
</div>
<h2>display: inline-flex</h2>
<p class="inline-line">Before <span class="demo inline-flex"><span class="item">A</span><span class="item">B</span><span class="item">C</span></span> After</p>
</div>
</body>
</html>
In the first box, the items stack. In the second, the children form a row while their container still takes the available width. In the third, the box can sit between the words. Change only the second box’s display to block to see its children stack again, then restore it.
Now add width: 100% to .inline-flex. The contents still use Flexbox, but the box itself asks for the full line width, so the words may wrap to other lines. Remove the width declaration after comparing.
Understand stretching and nested flex containers
The second container has a minimum height of 100px. Its items have no explicit height, so the default cross-axis stretch can make them fill the height inside the box. Add the rule below to put them at the top and let them use their content height. Remove it to restore the original. For a nested layout, make the immediate parent of the grandchildren another flex container.
.flex {
align-items: flex-start;
}
Handle wrapping and long text
The default flex-wrap: nowrap keeps items on one line. Use flex-wrap: wrap on the container if items can move to another line. To keep one flex line while breaking a long URL inside an item, adjust its min-width and overflow-wrap as shown below. See CSS Flexbox · Automatic Minimum Size for automatic minimum sizing.
.flex .item {
min-width: 0;
overflow-wrap: anywhere;
}
Try adding a long unbroken string to an item and applying the rule. The item can shrink farther and the text can wrap inside it. If you need to change the direction of the items themselves, see the responsive flex-direction example.




