CSS inline-grid vs grid: Key Differences Explained
CSS Grid provides two primary display values to establish a grid
formatting context: display: grid and
display: inline-grid. While both produce an identical
internal grid layout for their child elements, they differ fundamentally
in how the parent container interacts with adjacent elements and behaves
within the document flow. Understanding the distinction between these
two values comes down to the difference between block-level and
inline-level formatting contexts.
Container Flow and Document Interaction
The primary difference between grid and
inline-grid lies in how the grid container itself is
rendered relative to surrounding content:
display: grid(Block-Level): Creates a block-level container. By default, it takes up the full width of its parent container and breaks onto a new line, pushing following elements below it, similar to a standard<div>.display: inline-grid(Inline-Level): Creates an inline-level container. It only occupies as much width as its content requires (shrink-to-fit) and allows other inline or inline-block elements to sit on the same line alongside it, similar to an<span>orinline-blockelement.
Internal vs. External Behavior
The children of both grid and inline-grid
containers behave identically. Once inside the container:
- Direct children become grid items.
- All grid-specific properties work the same way, including
grid-template-columns,grid-template-rows,gap,justify-items, andalign-items. - Grid items do not behave like inline elements; they always follow
grid layout rules regardless of whether the parent container is
gridorinline-grid.
The difference is strictly external: display: grid
affects the page layout as a block, whereas
display: inline-grid integrates into the inline flow.
Width Handling and Fractional Units
Because display: grid defaults to 100% width of its
parent, fractional units (fr) have a defined space to
divide immediately.
With display: inline-grid, width is determined by
content:
- If an
inline-gridcontainer has no explicitly declared width and usesfrunits (such asgrid-template-columns: 1fr 1fr), the container collapses to the minimum content width of the items rather than expanding to fill the viewport or parent. - To make
frunits expand dynamically inside aninline-grid, a fixed width,min-width, ormax-widthmust generally be defined on the container.
Vertical Alignment
Because an inline-grid container participates in an
inline formatting context, it responds to the CSS
vertical-align property. You can use properties like
vertical-align: middle, top, or
baseline to align the entire grid container relative to
adjacent text or inline components. A standard
display: grid container ignores vertical-align
entirely because it is a block-level element.
When to Use Each
- Use
display: gridfor full-page structures, primary layout sections (such as headers, sidebars, and main content areas), and any standalone component designed to take up full horizontal space. - Use
display: inline-gridfor micro-layouts that need to sit alongside text or other inline components, such as complex interactive buttons with multiple text lines and icons, inline badges, status indicators, and form field groupings.