日本語

CSS inline-flex vs flex: Differences and When to Use Each

​​CSS​Flexbox,HTML,Playwright

display: flex and display: inline-flex lay out their children in exactly the same way. The difference is how the container itself behaves in its surrounding context. flex breaks the line before and after itself and stretches to the full width of its parent. inline-flex sits on the same line as the surrounding text and shrinks to its content's width. When you want a tag or button with an icon and text to sit inside a sentence or next to other inline content, use inline-flex.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and Chromium 153. I opened HTML and CSS-only pages in Playwright and measured positions and sizes with getBoundingClientRect(). The font was Noto Sans CJK JP at 16px, with a line height of 24px. I did not test Firefox or Safari.

The difference is only on the outside

The MDN description of display says that flex "behaves like a block-level element," that inline-flex "behaves like an inline-level element," and that both lay out their contents using the flexbox model. display takes two keywords, one for the outer display type (how the box sits relative to its neighbors) and one for the inner display type (how its contents are laid out). flex is the same as block flex, and inline-flex is the same as inline flex. The inner flex is shared, and only the outer block or inline differs.

flex inline-flex
Line break before and after Yes No
Width when width is not set Full width of the parent Width of the content
How children are laid out Flexbox Flexbox (same)
How to center it width: fit-content to shrink it, then margin: 0 auto text-align: center on the parent
Two in a row Stacked vertically Placed side by side (a line break between the tags creates a space)

In Chromium, display: block flex gave a computed value of flex. display: inline flex gave inline-flex.

I placed the same tag, with the same CSS, inside a sentence, changing only display. Here is what happened:

Two rows of a screen. In the row for display: flex, two tags, each with a round icon and text, each sit on their own line at the full width of the parent, with the sentence split around them. In the row for display: inline-flex, the same tags sit on the same line as the sentence and shrink to their content's width
Chromium screen showing a tag with an icon and text placed inside a sentence, with only display changed

The flex tag split the sentence before and after itself and stretched to the full width of the parent. The inline-flex tag fit on the same line as the sentence, at the width of its content. In both cases the round icon and the text sit side by side and are vertically centered, because the inner layout is the same.

The children are laid out the same way

I placed two 60×30px children and one 60×50px child in a container, with gap: 8px. The positions and sizes of the children, measured from the container's top-left corner, were the same for flex and inline-flex.

Child Position (left, top) Size
First (0, 0) 60 × 30px
Second (68, 0) 60 × 30px
Third (136, 0) 60 × 50px

Reading the display of the child <span> elements with getComputedStyle() gave block in both containers. A child inside an inline-flex container is not laid out like text. gap and align-items work the same in both. However, rules that use leftover space produce different results, as shown below.

The width and the line break differ

I placed a container with two children inside a 400px-wide parent, with text before and after it.

display Container position Container width Parent height
flex Left edge of the next line 400px 78px
inline-flex Same line as the preceding text 128px 30px

With flex, the content formed three lines: the preceding text (24px), the container (30px), and the following text (24px). The parent's height was 78px. With inline-flex, the container sat directly after the preceding text. Its width was 128px, which is the two children plus the gap (60 + 8 + 60).

When I set display: flex on a <span>, it also broke the line before and after, in the same way. If you want a <span> in the middle of a sentence to sit side by side with its neighbors, and flex breaks the line there, switch to inline-flex.

With flex-wrap: wrap, if the content does not fit, inline-flex also expands up to the parent's width. I put seven 60px children (420px in total) into the container with flex-wrap: wrap. The inline-flex container moved to the left edge of the next line and wrapped onto two rows at a width of 400px. The position and size matched flex.

flex-grow and space-between do not work in inline-flex

An inline-flex container shrinks to fit its content, so there is no leftover width to distribute among the children. I compared two 60px children inside a 400px parent, with rules that use leftover space.

display flex-grow: 1 on the first child justify-content: space-between on the container
flex The first child grew to 340px The second child moved to the right edge (at 340px)
inline-flex Stayed at 60px The second child stayed at 60px
inline-flex with width: 300px The first child grew to 240px The second child moved to the right edge (at 240px)

When I added width to the inline-flex container, the leftover width was distributed as it is with flex. For a layout that spreads items to the left and right, or a child that fills the remaining width, use flex, or give the inline-flex container a width.

Centering works differently

I compared ways to center a 128px-wide container inside a 400px parent.

display text-align: center on the parent margin: 0 auto on itself width: fit-content; margin: 0 auto on itself
flex Not centered (width 400px) Not centered (width 400px) Centered
inline-flex Centered Not centered Not centered

The flex container stays 400px wide, so margin: 0 auto alone has nothing to center. With width: fit-content, it shrinks to its content's width, and its left edge moved to 136px, which centered it. The inline-flex container is an inline-level box that sits on the same line as text, so text-align: center on the parent centered it.

Space from line breaks and space below

Because inline-flex sits on the same line as text, it shows spacing that flex does not.

When I wrote two inline-flex containers with a line break between their tags, a 3.59px horizontal space appeared between them. With no whitespace between the tags, the space was 0px. The line break between the tags is rendered as a whitespace character between the text. The same markup with flex placed the second container below the first.

I placed one 50px-tall container inside a parent and measured the parent's height.

Container Parent height Space below
flex 50px 0px
inline-flex (child with text) 50px 0px
inline-flex (child without text) 55px 5px
inline-flex (child without text) with vertical-align: top 50px 0px
inline-flex (child without text) with vertical-align: middle 50px 0px

Only when the child had no text did 5px of space remain below the container. If an inline-flex container holds only non-text children, such as an icon-only button, and a thin space appears below it, set vertical-align to top or middle.

When the child has text, the text inside the container lines up at the same height as the surrounding text. When I placed an inline-flex container with padding: 8px 12px in the middle of a sentence, the bottom edge of the text box inside the container and the bottom edge of the preceding text were both 32px from the top.

Which one to choose

For horizontal layouts that structure a page, such as headers, rows inside cards, or a single row of a form, use flex to fill the parent's width. space-between to spread items left and right, and flex-grow to fill the remaining space, also work as expected.

For small components placed inside text or on the same line as other components, such as icon tags, labels, buttons, and count badges, use inline-flex. It shrinks to its content's width and lines up with the surrounding text. Positioning it with the parent's text-align works the same way as for text.

The MDN article on basic flexbox concepts also explains that you can choose between inline-flex and flex to display the container inline or as a block. If you are unsure, decide based on this question: does this component need to break the line before and after it?

​Posted by 86