CSS grid-auto-flow

View Output Full Screen Preview

The CSS grid-auto-flow property specifies how auto-placed items get flowed into the grid.

If your browser supports CSS grids, the above example should look like this:

Screenshot of a grid using the 'grid-auto-flow' property.

The grid-auto-flow property only deals with grid items that are placed into tracks that are created implicitly. Grid items that aren't explicitly placed are automatically placed into unoccupied space, and an implicit grid is created just for that purpose. The grid-auto-flow property specifies exactly how these items are placed.

Let's look at the relevant code from the above example:

We define two rows, but we don't define any columns. Therefore the grid container will need to add columns implicitly in order to fit any grid items into the grid.

Normally when you do this, the grid container will add more rows to accomodate the grid items — not columns. The grid items will simply pile up on top of each other. This is because the intial value of the grid-auto-flow property is row. But in our example, we use grid-auto-flow: column to specify that grid items will be auto-placed into columns — not rows.

The dense Keyword

The dense keyword can come in handy if you have different sized grid items within an implicit grid, some of which span multiple tracks.

Normally, an implicit grid will simply list out the items in source order. If all grid items are the same size, and fit within a single track, this will typically result in an evenly spaced grid with the grid items packed up tight against each other (depending on gutters, alignment, etc).

But if some of the grid items span multiple tracks and others don't, you can end up with a grid where items flow over to the next line well before the end of the grid container, causing the grid to look a bit "jagged" on one side.

Without the dense keyword:

Here, grid items 2 and 3 both span two columns. This causes item 3 to wrap over to the next row, leaving a space on the end of the first row.

View Output Full Screen Preview

Here's how that should look in the browser:

Screenshot of a grid using the 'sparse' algorithm.

With the dense keyword:

Using the dense keyword causes item 4 to be moved up to the end of the first line so that it fills the space left behind by item 3.

View Output Full Screen Preview

Here's how that should look in the browser:

Screenshot of a grid using the 'dense' algorithm.

The dense keyword is an optional keyword that tells the browser to use a "dense" packing algorithm. What this means is that the browser will attempt to fill in holes earlier in the grid if smaller items come up later.

It's important to note that this can result in grid items appearing out of source order. You can see from the above example that grid item "4" comes before "3" once the dense keyword is used. This technique is usually fine when used on things where the visual order doesn't matter (like image galleries, etc), but it should be avoided if the visual order is important (such as on forms, etc).

Syntax

Here's the official syntax of the grid-auto-flow property:

These values are explained below.

Possible Values

row

Specifies that new rows are added to fit any auto-placed items.

column

Specifies that new columns are added to fit any auto-placed items.

dense

Specifies that a "dense" packing algorithm is used, which attempts to fill in holes earlier in the grid if smaller items come up later.

If dense isn't specified, then a "sparse" placement algorithm is used instead, where grid items are placed in source order (never backtracking to fill any holes).

In addition, all CSS properties also accept the following CSS-wide keyword values as the sole component of their property value:

initial
Represents the value specified as the property's initial value.
inherit
Represents the computed value of the property on the element's parent.
unset
This value acts as either inherit or initial, depending on whether the property is inherited or not. In other words, it sets all properties to their parent value if they are inheritable or to their initial value if not inheritable.

Basic Property Information

Initial Value
row
Applies To
Grid containers
Inherited?
No
Media
Visual
Animation type
Discrete (see example)

CSS Specifications

Browser Support

Browser support for CSS properties can vary, particularly for newer or experimental features. Check current browser compatibility before using a property in production, especially when supporting older browsers.

The following table provided by Caniuse.com shows the level of browser support for this feature.

Initial Value

CSS properties have an initial value that applies when the property has not been explicitly set. The initial value is different from the value inherited from a parent element and may vary between properties.

Inheritance

Some CSS properties inherit their computed value from their parent element by default, while others do not. The inherit, initial, unset, and revert keywords can also be used to control how a property gets its value.

Vendor Prefixes

Most modern CSS properties use the standard, unprefixed syntax. Vendor-prefixed properties such as -webkit- are mainly relevant to older browser versions or specific features and should only be used when required by your browser support targets.