CSS grid-auto-columns Property

The CSS grid-auto-columns property defines the size of implicitly created columns in a CSS Grid container.

Implicit columns are generated when grid items need columns beyond those explicitly defined by grid-template-columns. The grid-auto-columns property controls how those additional columns are sized, using fixed lengths, percentages, flexible fractions, content-based values, or track-sizing functions.

grid-auto-columns Syntax

The grid-auto-columns property accepts one or more track-sizing values. When multiple values are supplied, the browser repeats the sequence as additional implicit columns are created.

Basic Syntax

selector {
  grid-auto-columns: auto;
}

Fixed Column Width

.container {
  display: grid;
  grid-auto-columns: 150px;
}

Flexible Column Width

.container {
  display: grid;
  grid-auto-columns: 1fr;
}

Minimum and Maximum Size

.container {
  display: grid;
  grid-auto-columns: minmax(150px, 1fr);
}

Content-Based Sizing

.container {
  display: grid;
  grid-auto-columns: max-content;
}

Multiple Column Sizes

.container {
  display: grid;
  grid-auto-columns: 100px 200px;
}

Percentage Width

.container {
  display: grid;
  grid-auto-columns: 25%;
}

CSS-Wide Keywords

.container {
  grid-auto-columns: initial;
}
.container {
  grid-auto-columns: inherit;
}
.container {
  grid-auto-columns: unset;
}
.container {
  grid-auto-columns: revert;
}
.container {
  grid-auto-columns: revert-layer;
}

The property affects only implicit grid columns. It does not change the dimensions of columns explicitly defined by grid-template-columns.

Property Values

The grid-auto-columns property supports the following track-sizing values and functions.

Value Description
auto Sizes the implicit column according to its contents and available space, using automatic track-sizing rules.
<length> Defines a fixed track size using units such as px, rem, or em.
<percentage> Sizes the column relative to the grid container's content-box width when that size is definite.
<flex> Uses an fr unit to assign a flexible share of available space.
min-content Uses the smallest intrinsic track size that the contents permit.
max-content Uses the maximum intrinsic size of the contents without soft wrapping.
minmax() Defines minimum and maximum track-sizing limits.
fit-content() Limits the track's maximum size while retaining content-based sizing behavior.
inherit Inherits the value from the parent element.
initial Resets the property to auto.
unset Resets the non-inherited property to auto.

Property Characteristics

Characteristic Value
Initial value auto
Inherited No
Applies to Grid containers
Computed value As specified, with relative lengths converted to absolute lengths
Animation type By computed value type
Percentages Relative to the grid container's content-box width
Default track sizing auto

Track sizes must be nonnegative. The property accepts a space-separated list of track sizes, but it does not accept repeat() as part of its value syntax.

How It Works

CSS Grid creates implicit columns when the layout requires columns outside those explicitly defined in the grid template.

Create a Grid Container

.container {
  display: grid;
  grid-template-rows: 100px 100px;
  grid-auto-flow: column;
  grid-auto-columns: 150px;
}

Automatic Column Creation

With grid-auto-flow: column, the browser places items down the available rows before moving to the next column.

Column Sizing

Every implicitly generated column receives the size specified by grid-auto-columns, unless a list of sizes is supplied.

Explicit Column Definitions

If grid-template-columns defines explicit columns, those tracks retain their own sizes. The automatic column-sizing property applies only to columns created outside that explicit structure.

Additional Columns

Implicit columns can also be created when a grid item is explicitly positioned beyond the available grid lines.

Important Consideration

Setting grid-auto-columns does not create columns by itself. The grid layout must require implicit columns before the property has a visible effect.

Implicit vs. Explicit Columns

CSS Grid distinguishes between columns declared by the author and columns generated automatically.

Explicit Columns

.container {
  display: grid;
  grid-template-columns: 200px 1fr;
}

The container has two explicitly defined columns.

Implicit Columns

.container {
  display: grid;
  grid-template-columns: 200px 1fr;
  grid-auto-columns: 150px;
}

Any additional columns created outside the explicit grid use the 150-pixel automatic track size.

Place an Item Beyond the Explicit Grid

.item {
  grid-column: 4;
}

Positioning an item in the fourth column requires the browser to create implicit columns beyond the two explicitly defined columns.

Property Comparison

Feature grid-template-columns grid-auto-columns
Controls Explicit columns Implicit columns
Defines track count Yes No
Initial value none auto
Multiple sizes Supported Supported
repeat() function Supported Not supported

Important Consideration

Explicit and implicit columns can exist in the same grid. Their sizing properties operate independently.

Fixed Column Sizes

Fixed lengths give implicitly generated columns a predictable width.

Pixel Width

.container {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 180px;
}

rem Units

.container {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 12rem;
}

em Units

.container {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 15em;
}

Fixed Columns with Gaps

.container {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 160px;
  gap: 16px;
}

Horizontal Overflow

.container {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 200px;
  gap: 16px;
  overflow-x: auto;
}

When the total width of the columns exceeds the container, horizontal scrolling can provide access to the additional content.

Important Consideration

Fixed column widths do not automatically shrink to fit narrow containers. Account for overflow when using several fixed-size implicit columns.

The auto Value

The auto keyword is the initial value of grid-auto-columns.

Basic Example

.container {
  display: grid;
  grid-auto-columns: auto;
}

Automatic Column Sizing

An automatically sized track uses the grid track-sizing algorithm to determine an appropriate width based on its contents and available space.

Column-Based Auto-Placement

.container {
  display: grid;
  grid-template-rows: repeat(2, 100px);
  grid-auto-flow: column;
  grid-auto-columns: auto;
}

Content Influence

Long text, images, and elements with intrinsic dimensions can affect the size of automatically sized columns.

Stretching Behavior

Unlike many fixed or flexible track sizes, auto tracks can be stretched by the grid container's content distribution alignment when space is available.

Important Consideration

The auto keyword does not guarantee equal column widths. Columns with different content requirements may have different sizes.

Fractional Units

The fr unit assigns a flexible share of available space to implicit grid columns.

Equal Flexible Columns

.container {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
}

Two Fractional Units

.container {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 2fr;
}

When every implicit column uses 2fr, the columns receive equal flexible shares. The numeric fraction matters when tracks have different flex factors.

Alternating Flexible Sizes

.container {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr 2fr;
}

The sizing pattern alternates between tracks with one and two flexible shares.

Minimum Sizing Constraints

.container {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: minmax(0, 1fr);
}

The minmax(0, 1fr) value allows the tracks to shrink below their automatic minimum size.

Important Consideration

Flexible fractions do not guarantee that all content fits. Intrinsic minimum sizes and the available width can affect the final layout.

The minmax() Function

The minmax() function defines a minimum and maximum size for implicitly generated columns.

Minimum Width with Flexible Maximum

.container {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: minmax(150px, 1fr);
}

Fixed Minimum and Maximum

.container {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: minmax(120px, 240px);
}

Content-Based Maximum

.container {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: minmax(100px, max-content);
}

Allow Columns to Shrink

.container {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: minmax(0, 1fr);
}

Sizing Rules

The first argument defines the minimum track sizing function. The second defines the maximum. A flexible fr value is allowed as the maximum but not as the minimum.

Important Consideration

When a definite maximum is smaller than the minimum, the maximum is treated as equal to the minimum. Track sizing must still satisfy the applicable grid sizing rules.

Content-Based Sizing

Content-based values size implicit columns according to the intrinsic dimensions of their contents.

min-content

.container {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: min-content;
}

The min-content value uses the smallest intrinsic width the contents can occupy.

max-content

.container {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: max-content;
}

The max-content value uses the maximum intrinsic width without soft wrapping.

fit-content()

.container {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: fit-content(200px);
}

The fit-content() function provides content-based sizing with a specified maximum constraint, subject to the minimum content requirements.

Automatic Content Sizing

.container {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: auto;
}

Long Unbreakable Content

Long URLs, unbroken strings, and wide images can force content-based tracks to become wider than expected.

Important Consideration

Content-based sizing is useful when column widths should reflect their contents, but it may require overflow handling in narrow layouts.

Multiple Track Sizes

The grid-auto-columns property accepts a list of track sizes. The browser repeats the list as additional implicit columns are generated.

Two-Size Pattern

.container {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 100px 200px;
}

The implicit columns follow a repeating 100-pixel, 200-pixel pattern.

Three-Size Pattern

.container {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 100px 150px 200px;
}

Mixed Track Sizes

.container {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 120px minmax(150px, 1fr);
}

Repeated Pattern

Implicit Column Track Size
Column 1 100px
Column 2 200px
Column 3 100px
Column 4 200px
Column 5 100px

Important Consideration

The list is repeated automatically. Do not use repeat() inside grid-auto-columns; that function is not permitted in this property's syntax.

Automatic Column Placement

The grid-auto-flow property determines whether automatically placed items progress by rows or columns.

Column-Based Placement

.container {
  display: grid;
  grid-template-rows: repeat(2, 100px);
  grid-auto-flow: column;
  grid-auto-columns: 150px;
}

The browser places items down the two rows before generating another column.

Single-Row Layout

.container {
  display: grid;
  grid-template-rows: 100px;
  grid-auto-flow: column;
  grid-auto-columns: 180px;
}

With one explicit row, automatically placed items progress horizontally into additional columns.

Dense Column Placement

.container {
  display: grid;
  grid-template-rows: repeat(3, 100px);
  grid-auto-flow: column dense;
  grid-auto-columns: 150px;
}

The dense keyword allows later items to fill earlier available spaces when their sizes permit.

Row-Based Placement

.container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-auto-flow: row;
  grid-auto-columns: 150px;
}

Row-based placement normally generates additional rows rather than columns. Implicit columns can still be created through explicit item placement.

Important Consideration

Use grid-auto-flow: column when items should advance horizontally into new columns. Use grid-auto-columns to control the width of those columns.

Explicit Item Placement

Implicit columns can be generated when a grid item is positioned outside the explicitly defined columns.

Define Two Explicit Columns

.container {
  display: grid;
  grid-template-columns: 150px 150px;
  grid-auto-columns: 200px;
}

Place an Item in Column Four

.item {
  grid-column: 4 / 5;
}

The browser creates additional implicit columns to accommodate the specified placement.

Span Beyond the Explicit Grid

.item {
  grid-column: 2 / 5;
}

Use grid-area

.item {
  grid-area: 1 / 3 / 2 / 5;
}

The item begins at the third column line and extends to the fifth column line.

Implicit Columns Before the Explicit Grid

.item {
  grid-column: -5 / -4;
}

Negative grid line references can require implicit tracks before the explicit grid when they extend beyond its boundaries.

Important Consideration

Implicit columns can be generated on either side of the explicit grid. The sizing sequence follows the grid's implicit track-sizing rules.

Percentage Sizing

Percentage values size implicit columns relative to the content-box width of the grid container.

Percentage Width

.container {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 25%;
}

Fixed Container Width

.container {
  display: grid;
  width: 800px;
  grid-auto-flow: column;
  grid-auto-columns: 25%;
}

When the content-box width is 800 pixels, each implicit column receives a track size of 200 pixels.

Percentage Columns with Gaps

.container {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 25%;
  gap: 20px;
}

Column gaps are added between the percentage-sized tracks. The total width of the columns and gaps can exceed the container.

Indefinite Container Width

When the grid container's width depends on the sizes of its tracks, percentage track sizing can create a cyclic dependency. In these cases, the specification applies special intrinsic sizing rules.

Important Consideration

Use flexible fractions or minmax() when the layout should distribute available space without relying on fixed percentages.

grid-auto-columns vs. grid-template-columns

Both properties control grid column sizes, but they apply to different parts of the grid.

Explicit Column Sizing

.container {
  display: grid;
  grid-template-columns: 200px 1fr;
}

Implicit Column Sizing

.container {
  display: grid;
  grid-auto-columns: 150px;
}

Combined Example

.container {
  display: grid;
  grid-template-columns: 200px 1fr;
  grid-auto-columns: 150px;
}

The first two columns are explicitly defined. Any additional implicit columns use the 150-pixel size.

Choosing the Correct Property

Use grid-template-columns when the number and size of columns are known in advance. Use grid-auto-columns when additional columns may be generated automatically.

Important Consideration

Setting grid-auto-columns does not override existing explicit column definitions. The two properties work together.

Responsive Layouts

The grid-auto-columns property is useful for responsive horizontal card lists, scrolling galleries, and other layouts that generate columns as content is added.

Horizontal Card Layout

.card-list {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 220px;
  gap: 16px;
  overflow-x: auto;
}

Flexible Card Width

.card-list {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: minmax(180px, 1fr);
  gap: 16px;
}

Responsive Track Sizing

.card-list {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 180px;
  gap: 16px;
  overflow-x: auto;
}
@media (min-width: 768px) {
  .card-list {
    grid-auto-columns: 240px;
  }
}

Viewport-Based Width

.card-list {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: minmax(180px, 30vw);
  gap: 16px;
  overflow-x: auto;
}

Container-Aware Sizing

.card-list {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: minmax(180px, 30%);
  gap: 16px;
  overflow-x: auto;
}

Important Consideration

Horizontally scrolling grids should remain usable with keyboards and touch devices. Avoid hiding important content without providing a clear way to reach it.

Inheritance

The grid-auto-columns property is not inherited.

Parent Grid Container

.parent-grid {
  display: grid;
  grid-auto-columns: 200px;
}

Nested Grid Container

.nested-grid {
  display: grid;
}

The nested grid uses its own initial grid-auto-columns: auto value unless another value is specified.

Explicit Inheritance

.nested-grid {
  display: grid;
  grid-auto-columns: inherit;
}

The inherit keyword copies the computed value from the parent element.

Reset to Initial Value

.nested-grid {
  display: grid;
  grid-auto-columns: initial;
}

Important Consideration

Nested grid containers maintain their own grid sizing rules. Use explicit declarations when a nested layout needs different implicit column sizes.

grid-auto-columns Example

This example creates a horizontal grid with automatically generated columns. Each implicit column has a fixed width of 140 pixels.

HTML Example

<div class="grid-auto-columns-demo">
  <div class="grid-item">Item 1</div>
  <div class="grid-item">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-auto-columns-demo {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 140px;
  gap: 12px;
  overflow-x: auto;
  padding-bottom: 8px;
}
.grid-auto-columns-demo .grid-item {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 90px;
  padding: 12px;
  background-color: #2456a6;
  color: #ffffff;
  font-weight: bold;
  border-radius: 4px;
}
Browser Result
Item 1
Item 2
Item 3
Item 4
Item 5

The grid-auto-flow: column declaration places items horizontally, generating implicit columns as needed. The grid-auto-columns: 140px declaration gives every implicit column the same width.

The gap property adds 12 pixels between columns, while overflow-x: auto allows horizontal scrolling when the grid exceeds the available width.

Common Mistakes

Common problems with grid-auto-columns involve confusing explicit and implicit tracks or expecting the property to create columns automatically.

Expecting the Property to Create Columns

.container {
  display: grid;
  grid-auto-columns: 150px;
}

The declaration controls the size of implicit columns but does not determine how many columns are created.

Using It Instead of grid-template-columns

Use grid-template-columns to define explicit columns. Use grid-auto-columns to size columns generated outside the explicit grid.

Forgetting grid-auto-flow: column

.container {
  display: grid;
  grid-auto-columns: 150px;
}

By default, automatic placement progresses by rows. Use grid-auto-flow: column when the layout should generate additional columns as items are placed.

Using Negative Track Sizes

.container {
  grid-auto-columns: -100px;
}

Negative track sizes are invalid.

Using repeat()

.container {
  grid-auto-columns: repeat(3, 100px);
}

The repeat() function is not supported in grid-auto-columns. Supply a space-separated list of sizes instead.

Assuming auto Produces Equal Widths

Automatic track sizing depends on content and available space. Columns may have different widths.

Ignoring Intrinsic Minimum Sizes

Flexible tracks can remain wider than expected because of their contents. Consider minmax(0, 1fr) when appropriate.

Ignoring Horizontal Overflow

Fixed-width implicit columns can extend beyond the container. Provide sufficient space or a suitable overflow strategy.

Confusing Column Sizing with Row Sizing

The grid-auto-columns property controls implicit columns. Use grid-auto-rows for implicit row sizing.

Assuming Percentage Widths Include Gaps

Percentage track widths do not automatically subtract column gaps. The combined widths and gaps may exceed the container.

Important Considerations

Keep the following points in mind when using grid-auto-columns:

  • Purpose: Controls the size of implicitly generated grid columns.
  • Initial value: auto.
  • Inheritance: The property is not inherited.
  • Applies to: Grid containers.
  • Explicit columns: Controlled separately by grid-template-columns.
  • Column generation: Occurs when placement requires tracks outside the explicit grid.
  • Automatic placement: grid-auto-flow: column commonly generates implicit columns.
  • Fixed sizing: Supports nonnegative length values.
  • Percentage sizing: Relative to the grid container's content-box width.
  • Flexible sizing: Supports fr units.
  • Content sizing: Supports auto, min-content, and max-content.
  • Track limits: Supports minmax() and fit-content().
  • Multiple values: A space-separated list repeats as implicit columns are generated.
  • repeat(): Not supported in this property.
  • Track count: The property does not define the number of columns.
  • Grid gaps: Controlled separately using gap or column-gap.
  • Overflow: Fixed or content-sized columns may exceed the container width.
  • Responsive design: Track sizes can be adjusted with media queries.
  • Accessibility: Horizontally scrolling layouts should remain usable with keyboard and touch input.
  • Browser testing: Check implicit track creation, sizing, and overflow at different widths.

The grid-auto-columns property is particularly useful when the number of columns depends on content or item placement. It allows those additional tracks to follow consistent sizing rules without defining each column individually.

Browser Support

The CSS grid-auto-columns 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 implicit grid column sizing with fixed lengths, flexible fractions, and content-based values.

Standard Example

.container {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 150px;
}

Legacy Browser Considerations

Internet Explorer used an older, prefixed CSS Grid implementation with different automatic placement and track-sizing behavior. Modern grid-auto-columns declarations should not be expected to work in that legacy grid system.

Browser Compatibility

For current compatibility information, visit Can I Use? and search for grid-auto-columns.

Specifications

The grid-auto-columns property is defined in the CSS Grid Layout specification.

CSS Grid Layout Module Level 1: grid-auto-columns

The specification defines implicit grid tracks, track-sizing functions, and the interaction between explicit and automatically generated grid tracks.

CSS Grid Layout Module Level 2

For additional documentation and examples, consult MDN Web Docs.

MDN Web Docs: grid-auto-columns