CSS grid-auto-flow Property
The CSS grid-auto-flow property controls how automatically placed grid items are arranged within a grid container.
It determines whether the grid auto-placement algorithm fills rows or columns and whether it attempts to fill earlier gaps using dense packing. The property is useful for galleries, card layouts, dashboards, and other grid designs where items are placed without assigning individual grid positions.
grid-auto-flow Syntax
The grid-auto-flow property accepts a placement direction, the dense keyword, or a combination of both.
Basic Syntax
selector {
grid-auto-flow: row;
}
Row-Based Placement
.container {
display: grid;
grid-auto-flow: row;
}
Column-Based Placement
.container {
display: grid;
grid-auto-flow: column;
}
Dense Row Placement
.container {
display: grid;
grid-auto-flow: row dense;
}
Dense Column Placement
.container {
display: grid;
grid-auto-flow: column dense;
}
Dense Keyword Alone
.container {
display: grid;
grid-auto-flow: dense;
}
When dense is used without a direction, row-based placement is assumed.
CSS-Wide Keywords
.container {
grid-auto-flow: initial;
}
.container {
grid-auto-flow: inherit;
}
.container {
grid-auto-flow: unset;
}
.container {
grid-auto-flow: revert;
}
.container {
grid-auto-flow: revert-layer;
}
The property must be applied to a grid container established with display: grid or display: inline-grid.
Property Values
The grid-auto-flow property supports the following placement values.
| Value | Description |
|---|---|
row |
Places automatically positioned items across rows, creating additional rows as necessary. This is the initial value. |
column |
Places automatically positioned items down columns, creating additional columns as necessary. |
dense |
Uses row-based placement with dense packing, allowing later items to fill earlier gaps when they fit. |
row dense |
Places items by rows and attempts to fill earlier unoccupied cells. |
column dense |
Places items by columns and attempts to fill earlier unoccupied cells. |
inherit |
Inherits the computed value from the parent element. |
initial |
Resets the property to row. |
unset |
Resets the non-inherited property to row. |
revert |
Reverts the property to the value established by an earlier cascade origin. |
revert-layer |
Reverts the property according to the cascade layer rules. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | row |
| Inherited | No |
| Applies to | Grid containers |
| Computed value | As specified |
| Animation type | Discrete |
| Percentages | Not applicable |
| Included in shorthand | grid |
The row and column keywords determine the placement direction. The optional dense keyword changes how the algorithm handles previously unfilled spaces.
How It Works
CSS Grid uses an auto-placement algorithm to position grid items that do not have fully specified grid coordinates.
Create a Grid Container
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 12px;
}
Default Placement
By default, the browser places items across the first row, then continues to the next row when the available columns are filled.
Change the Placement Direction
.container {
display: grid;
grid-template-rows: repeat(2, 100px);
grid-auto-flow: column;
}
With column-based placement, the browser fills the available rows in one column before continuing to the next column.
Automatic Placement Cursor
The placement algorithm maintains a position within the grid while determining where each auto-placed item fits.
Grid Item Spans
Items spanning multiple rows or columns may leave spaces that are too small for the next item. The placement algorithm determines whether those spaces remain empty or can be filled by later items.
Implicit Track Creation
When additional tracks are needed, the browser creates implicit rows or columns. Their sizes are controlled by grid-auto-rows and grid-auto-columns.
Important Consideration
The grid-auto-flow property controls placement behavior. It does not directly define the size of rows, columns, or gaps.
Row-Based Placement
The row value places automatically positioned items across rows, following the grid container's inline direction.
Three-Column Layout
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-flow: row;
gap: 12px;
}
Items fill the available columns in the first row before continuing to subsequent rows.
Automatic Row Sizes
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-flow: row;
grid-auto-rows: 100px;
}
Each implicitly generated row uses a track size of 100 pixels.
Two-Column Layout
.container {
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-auto-flow: row;
}
Default Behavior
The row value is the initial setting, so it can be omitted when the default placement behavior is appropriate.
Important Consideration
Row-based placement does not guarantee that every row is completely filled. Items with different spans can leave unoccupied cells.
Column-Based Placement
The column value places automatically positioned items down columns, following the grid container's block direction.
Two-Row Layout
.container {
display: grid;
grid-template-rows: repeat(2, 100px);
grid-auto-flow: column;
grid-auto-columns: 150px;
}
The first two items occupy the first column. The next two occupy the second column, and additional columns are generated as needed.
Single-Row Layout
.container {
display: grid;
grid-template-rows: 100px;
grid-auto-flow: column;
grid-auto-columns: 180px;
}
With one row, each additional automatically placed item requires another column.
Horizontal Scrolling Layout
.card-list {
display: grid;
grid-template-rows: 1fr;
grid-auto-flow: column;
grid-auto-columns: 220px;
gap: 16px;
overflow-x: auto;
}
Flexible Column Sizes
.container {
display: grid;
grid-template-rows: repeat(2, 100px);
grid-auto-flow: column;
grid-auto-columns: minmax(150px, 1fr);
}
Important Consideration
Column-based placement may create horizontal overflow when the generated columns require more width than the container provides.
Dense Packing
The dense keyword tells the auto-placement algorithm to consider earlier unoccupied spaces when placing later items.
Row Dense
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-flow: row dense;
}
Column Dense
.container {
display: grid;
grid-template-rows: repeat(3, 100px);
grid-auto-flow: column dense;
}
Dense Without a Direction
.container {
display: grid;
grid-auto-flow: dense;
}
This is equivalent to row-based dense placement.
When Dense Packing Helps
Dense packing is most noticeable when grid items have different spans. A large item may not fit into an earlier space, while a smaller item later in the source order may fit.
Dense Packing Does Not Resize Items
The algorithm searches for suitable unoccupied cells. It does not change an item's specified row or column span to make it fit.
Dense Packing and Visual Order
A later item may be displayed before an earlier item if it fits into a previously skipped space.
Important Consideration
Use dense packing only when the visual order can differ from the source order without confusing the meaning or interaction sequence of the content.
Sparse Packing
Sparse packing is the default behavior when the dense keyword is not specified.
Sparse Row Placement
.container {
display: grid;
grid-auto-flow: row;
}
Sparse Column Placement
.container {
display: grid;
grid-auto-flow: column;
}
How Sparse Placement Works
The placement cursor generally moves forward through the grid. When an item cannot fit at the current position, the algorithm advances until a suitable location is found.
Earlier Empty Spaces
Previously skipped spaces may remain unoccupied even when a later item could fit into them.
Sparse vs. Dense
| Behavior | Sparse | Dense |
|---|---|---|
| Default behavior | Yes | No |
| Revisits earlier gaps | Generally no | Yes, when possible |
| May visually reorder auto-placed items | Less likely | Yes |
| Useful for | Predictable placement progression | Packing mixed-size items |
Important Consideration
Sparse placement does not prevent all visual reordering. Explicit item positions and other grid placement rules can still produce a visual arrangement that differs from the source order.
Spanning Grid Items
Grid items can span multiple rows or columns. Their dimensions affect how the auto-placement algorithm finds available space.
Span Two Columns
.wide-item {
grid-column: span 2;
}
Span Two Rows
.tall-item {
grid-row: span 2;
}
Span Rows and Columns
.featured-item {
grid-column: span 2;
grid-row: span 2;
}
Mixed-Size Grid Items
.gallery {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-auto-rows: 100px;
grid-auto-flow: row dense;
gap: 12px;
}
.gallery .wide {
grid-column: span 2;
}
.gallery .tall {
grid-row: span 2;
}
Placement Gaps
When a spanning item cannot fit into the remaining space in a row or column, the algorithm searches for another valid position. This can leave empty cells.
Important Consideration
Dense packing can reduce gaps caused by spanning items, but it does not guarantee a completely filled grid.
Implicit Grid Tracks
The grid-auto-flow property works with implicit grid tracks when additional rows or columns are needed.
Implicit Rows
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-flow: row;
grid-auto-rows: 120px;
}
Additional rows are generated as items fill the available columns.
Implicit Columns
.container {
display: grid;
grid-template-rows: repeat(2, 100px);
grid-auto-flow: column;
grid-auto-columns: 180px;
}
Additional columns are generated as items fill the available rows.
Automatic Track Sizing
.container {
display: grid;
grid-auto-flow: row;
grid-auto-rows: auto;
}
Flexible Implicit Tracks
.container {
display: grid;
grid-template-rows: repeat(2, 100px);
grid-auto-flow: column;
grid-auto-columns: minmax(150px, 1fr);
}
Track Sizing Responsibilities
| Property | Purpose |
|---|---|
grid-auto-flow |
Controls automatic placement direction and packing behavior. |
grid-auto-rows |
Controls implicit row sizes. |
grid-auto-columns |
Controls implicit column sizes. |
grid-template-rows |
Defines explicit row tracks. |
grid-template-columns |
Defines explicit column tracks. |
Important Consideration
Implicit tracks can be generated by automatic placement or by explicitly positioning an item beyond the explicit grid.
Explicit and Automatic Placement
CSS Grid allows explicitly positioned items and automatically positioned items to exist in the same container.
Explicit Item Placement
.featured {
grid-column: 1 / 3;
grid-row: 1 / 3;
}
Automatic Placement for Remaining Items
.container {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-auto-flow: row;
}
Items without definite positions are placed according to the auto-placement algorithm while accounting for occupied grid cells.
Partially Positioned Items
.sidebar {
grid-column: 1;
}
The column is specified, but the row remains automatic. The browser determines a suitable row position.
Named Grid Areas
.container {
display: grid;
grid-template-areas:
"header header"
"sidebar main";
grid-template-columns: 200px 1fr;
}
.header {
grid-area: header;
}
Placement Priority
Items with definite grid positions occupy their assigned cells. The auto-placement algorithm places other items according to the remaining available positions and the grid placement rules.
Important Consideration
The grid-auto-flow property does not override explicitly assigned grid coordinates.
The grid Shorthand
The grid-auto-flow property is one of the six longhand properties included in the grid shorthand.
Longhand Example
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: 100px;
grid-auto-flow: row;
}
Equivalent Shorthand
.container {
display: grid;
grid: auto-flow 100px / repeat(3, 1fr);
}
Column-Based Shorthand
.container {
display: grid;
grid: repeat(2, 100px) / auto-flow 150px;
}
Dense Shorthand
.container {
display: grid;
grid: auto-flow dense 100px / repeat(3, 1fr);
}
Shorthand Reset Behavior
The grid shorthand resets all six grid-related longhand properties, including values not explicitly represented in the declaration.
Important Consideration
Use grid-auto-flow directly when changing only automatic placement behavior without resetting other grid settings.
Writing Direction
The placement direction follows the grid container's writing mode and text direction.
Left-to-Right Layout
.container {
display: grid;
direction: ltr;
grid-template-columns: repeat(3, 1fr);
grid-auto-flow: row;
}
In a typical horizontal left-to-right writing mode, row-based placement progresses from left to right.
Right-to-Left Layout
.container {
display: grid;
direction: rtl;
grid-template-columns: repeat(3, 1fr);
grid-auto-flow: row;
}
In a horizontal right-to-left writing mode, row-based placement progresses from right to left.
Vertical Writing Mode
.container {
display: grid;
writing-mode: vertical-rl;
grid-auto-flow: row;
}
With vertical writing modes, the inline and block axes change orientation, affecting the physical direction of grid placement.
Logical Axes
Grid rows follow the inline axis, while grid columns follow the block axis. The physical orientation depends on the writing mode.
Important Consideration
Do not assume that row always means left-to-right placement or that column always means top-to-bottom placement.
Responsive Layouts
The grid-auto-flow property can change placement direction at different viewport widths.
Mobile Row Layout
.card-list {
display: grid;
grid-template-columns: 1fr;
grid-auto-flow: row;
gap: 16px;
}
Desktop Column Layout
@media (min-width: 900px) {
.card-list {
grid-template-columns: none;
grid-template-rows: 1fr;
grid-auto-flow: column;
grid-auto-columns: minmax(180px, 1fr);
}
}
Responsive Grid Gallery
.gallery {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
grid-auto-flow: row;
gap: 12px;
}
@media (min-width: 768px) {
.gallery {
grid-template-columns: repeat(4, minmax(0, 1fr));
grid-auto-flow: row dense;
}
}
Horizontal Scrolling Cards
.cards {
display: grid;
grid-template-rows: 1fr;
grid-auto-flow: column;
grid-auto-columns: minmax(200px, 30%);
gap: 16px;
overflow-x: auto;
}
Important Consideration
When changing placement direction, review the explicit grid tracks and implicit track sizes. A column-based layout may require different sizing and overflow rules than a row-based layout.
Accessibility and Source Order
Grid auto-placement changes the visual arrangement of elements without changing their order in the HTML document.
Source Order
<div class="container">
<article>Article 1</article>
<article>Article 2</article>
<article>Article 3</article>
</div>
Sparse Placement
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-flow: row;
}
Dense Placement
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-flow: row dense;
}
Keyboard Navigation
Keyboard focus generally follows the document's focus navigation order, not the visual arrangement created by dense packing.
Reading Order
Assistive technologies generally use the document structure and accessibility tree rather than the visual placement of grid items.
Appropriate Uses for Dense Packing
Dense packing is best suited to collections where the visual position of individual items does not communicate a required sequence, such as certain image galleries or decorative tile layouts.
Important Consideration
Avoid dense packing for forms, step-by-step instructions, or other content where a mismatch between visual order and source order could confuse users.
Inheritance
The grid-auto-flow property is not inherited.
Parent Grid Container
.parent-grid {
display: grid;
grid-auto-flow: column;
}
Nested Grid Container
.nested-grid {
display: grid;
}
The nested grid uses its own initial value of row, regardless of the parent's automatic placement direction.
Explicit Inheritance
.nested-grid {
display: grid;
grid-auto-flow: inherit;
}
The inherit keyword copies the computed value from the parent element.
Reset to Initial Value
.nested-grid {
display: grid;
grid-auto-flow: initial;
}
Important Consideration
Each grid container controls its own auto-placement algorithm. Nested grids do not automatically share the placement direction or packing behavior of their parent grid.
grid-auto-flow Example
This example compares sparse row placement with dense row placement. The first two items span two columns, leaving a gap that a later one-column item can fill when dense packing is enabled.
HTML Example
<div class="grid-flow-demo">
<div class="grid-item wide">Item 1</div>
<div class="grid-item wide">Item 2</div>
<div class="grid-item">Item 3</div>
<div class="grid-item">Item 4</div>
<div class="grid-item">Item 5</div>
</div>
CSS Example
.grid-flow-demo {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
grid-auto-rows: 70px;
grid-auto-flow: row dense;
gap: 10px;
}
.grid-flow-demo .grid-item {
display: flex;
align-items: center;
justify-content: center;
padding: 10px;
background-color: #2456a6;
color: #ffffff;
font-weight: bold;
border-radius: 4px;
}
.grid-flow-demo .wide {
grid-column: span 2;
background-color: #187a65;
}
Sparse vs. Dense Browser Result
Sparse Placement: grid-auto-flow: row
Dense Placement: grid-auto-flow: row dense
In the sparse grid, Item 2 cannot fit beside Item 1, so it moves to the next row. The remaining space beside Item 1 is skipped, and Item 3 is placed after Item 2.
In the dense grid, Item 3 is allowed to fill the earlier gap beside Item 1. This creates a more compact visual arrangement, but Item 3 appears visually before Item 2 even though it follows Item 2 in the HTML.
This example demonstrates why dense packing is useful for certain visual collections but requires care when content order is important.
Common Mistakes
Common problems with grid-auto-flow involve misunderstanding automatic placement, implicit tracks, or dense packing behavior.
Forgetting display: grid
.container {
grid-auto-flow: column;
}
The property does not establish a grid container. Add display: grid or display: inline-grid.
Using an Invalid Value
.container {
grid-auto-flow: horizontal;
}
The property accepts row, column, and the optional dense keyword. It does not accept horizontal or vertical.
Expecting grid-auto-flow to Set Track Sizes
Use grid-auto-rows or grid-auto-columns to control implicit track sizes.
Expecting dense to Resize Items
Dense packing searches for suitable spaces but does not change item spans or track dimensions.
Assuming dense Always Removes Every Gap
Some gaps remain because no suitable item can fit into the available space.
Using row and column Together
.container {
grid-auto-flow: row column;
}
This combination is invalid. Select one placement direction.
Assuming column Means Equal-Width Columns
The column keyword controls placement direction, not column width.
Ignoring Horizontal Overflow
Column-based placement can generate more columns than fit within the available width.
Expecting Explicitly Positioned Items to Move
The auto-placement algorithm does not replace definite grid positions assigned to individual items.
Ignoring Visual Reordering
Dense packing may place later source items into earlier visual positions, potentially creating accessibility problems for ordered or interactive content.
Important Considerations
Keep the following points in mind when using grid-auto-flow:
- Purpose: Controls automatic grid item placement direction and packing behavior.
- Initial value:
row. - Inheritance: The property is not inherited.
- Applies to: Grid containers.
- Row placement: Fills rows and generates additional rows when necessary.
- Column placement: Fills columns and generates additional columns when necessary.
- Dense packing: Allows later items to fill earlier available spaces.
- Sparse packing: The default behavior without
dense. - Default direction: The
densekeyword alone uses row-based placement. - Track sizing: Controlled separately by explicit and implicit track-sizing properties.
- Implicit rows: Sized using
grid-auto-rows. - Implicit columns: Sized using
grid-auto-columns. - Explicit placement: Definite grid positions are not overridden by automatic placement.
- Spanning items: May create gaps that affect placement.
- Writing modes: Placement follows the grid container's logical axes.
- Shorthand: The property is included in the
gridshorthand. - Responsive design: Placement direction can change with media queries.
- Overflow: Column-based placement may require horizontal scrolling.
- Source order: Visual placement does not change the HTML document order.
- Accessibility: Avoid dense packing when visual order must match reading or keyboard navigation order.
- Browser testing: Check placement, gaps, spanning behavior, and overflow with realistic content.
The grid-auto-flow property is useful when grid items should be positioned automatically rather than assigned individual coordinates. Row and column placement provide predictable directions, while dense packing offers additional flexibility for layouts containing items of different sizes.
Browser Support
The CSS grid-auto-flow property has Widely available support in modern browsers as part of CSS Grid Layout.
Modern versions of Chrome, Edge, Firefox, Safari, and other major browsers support row-based placement, column-based placement, and dense packing.
Standard Example
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-flow: row dense;
}
Legacy Browser Considerations
Internet Explorer implemented an older, prefixed CSS Grid model with different placement behavior. The standardized grid-auto-flow property is not supported by that legacy implementation.
Browser Compatibility
For current compatibility details, visit Can I Use? and search for grid-auto-flow.
Specifications
The grid-auto-flow property is defined in the CSS Grid Layout specification.
CSS Grid Layout Module Level 1: grid-auto-flow
The specification defines the grid auto-placement algorithm, placement direction, sparse and dense packing, and the creation of implicit grid tracks.
CSS Grid Layout Module Level 2
For additional documentation and examples, consult MDN Web Docs.
