CSS grid-template-columns Property
The CSS grid-template-columns property defines the number and sizes of columns in the explicit grid of a CSS Grid container.
It supports fixed lengths, percentages, flexible fr units, content-based sizing, and functions such as repeat() and minmax(). You can also name column grid lines and create responsive layouts that adapt to available space.
grid-template-columns Syntax
The grid-template-columns property accepts a track list, the none keyword, or subgrid. A track list can combine different sizes and optional grid line names.
Basic Syntax
selector {
grid-template-columns: none;
}
Fixed Column Widths
.container {
grid-template-columns: 150px 250px 150px;
}
Flexible Columns
.container {
grid-template-columns: 1fr 2fr 1fr;
}
Percentage-Based Columns
.container {
grid-template-columns: 30% 70%;
}
Repeated Columns
.container {
grid-template-columns: repeat(3, 1fr);
}
Minimum and Maximum Widths
.container {
grid-template-columns: minmax(150px, 1fr) 2fr;
}
Automatically Repeated Columns
.container {
grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
}
Named Grid Lines
.container {
grid-template-columns:
[sidebar-start] 200px
[sidebar-end content-start] 1fr
[content-end];
}
Subgrid
.nested-grid {
grid-template-columns: subgrid;
}
CSS-Wide Keywords
.container {
grid-template-columns: initial;
}
.container {
grid-template-columns: inherit;
}
.container {
grid-template-columns: unset;
}
.container {
grid-template-columns: revert;
}
.container {
grid-template-columns: revert-layer;
}
Column definitions are separated by whitespace. Commas are used inside functions where required, but not between individual column track sizes.
Property Values
The property supports the following keywords, sizing values, and functions.
| Value | Description |
|---|---|
none |
Defines no explicit column tracks through this property. |
<length> |
Sets a column width using a length such as px, rem, or em. |
<percentage> |
Sizes a column relative to the grid container's inline size when that size is definite. |
<flex> |
Uses the fr unit to allocate a share of available flexible space. |
auto |
Sizes the column according to its contents and available space. |
min-content |
Uses the smallest intrinsic width the column's contents can occupy. |
max-content |
Uses the preferred intrinsic width without optional wrapping. |
minmax(min, max) |
Defines a minimum and maximum track sizing limit. |
fit-content(limit) |
Limits the track's maximum size while respecting intrinsic sizing constraints. |
repeat() |
Repeats a track pattern a specified number of times or automatically. |
auto-fill |
Creates as many repeated tracks as fit while retaining empty tracks. |
auto-fit |
Creates as many repeated tracks as fit, then collapses empty repeated tracks. |
[name] |
Assigns one or more names to a column grid line. |
subgrid |
Uses the parent grid's corresponding column tracks for a nested grid item. |
initial |
Resets the property to none. |
inherit |
Inherits the computed value from the parent. |
unset |
Resets this non-inherited property to none. |
revert |
Reverts the property according to cascade origin rules. |
revert-layer |
Reverts the property according to cascade layer rules. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | none |
| Inherited | No |
| Applies to | Grid containers |
| Computed value | As specified, with relative lengths converted to absolute lengths |
| Animation type | Track-list interpolation when compatible; otherwise discrete |
| Percentages | Refer to the grid container's inline size |
| Related shorthands | grid-template, grid |
Column widths are determined by the grid track sizing algorithm, which considers available space, intrinsic content sizes, and minimum and maximum constraints.
How It Works
The grid-template-columns property creates explicit column tracks in a grid container. Each value defines one track, unless a function such as repeat() expands into multiple tracks.
Create a Grid Container
.container {
display: grid;
grid-template-columns: 150px 1fr 150px;
gap: 12px;
}
This creates three columns. The first and third are 150 pixels wide, while the middle column uses the remaining flexible space.
Define Two Equal Columns
.container {
display: grid;
grid-template-columns: 1fr 1fr;
}
Define Three Equal Columns
.container {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
}
The minmax(0, 1fr) pattern allows the columns to shrink below their contents' automatic minimum widths when necessary.
Position a Grid Item
.item {
grid-column: 2 / 4;
}
This item spans the second and third columns of the three-column grid.
Important Consideration
Column track definitions control the structure of the grid. Individual item placement is handled by grid-column, grid-area, or the automatic placement algorithm.
Fixed Column Sizes
Fixed lengths provide precise control over column widths. They are useful when a layout requires predictable dimensions.
Pixel-Based Columns
.container {
grid-template-columns: 200px 300px;
}
Relative Length Units
.container {
grid-template-columns: 12rem 20rem;
}
Mixed Fixed and Flexible Columns
.container {
grid-template-columns: 220px minmax(0, 1fr);
}
This pattern is useful for a fixed-width sidebar beside a flexible content area.
Percentage Columns
.container {
grid-template-columns: 25% 75%;
}
Percentages are resolved against the grid container's inline size when definite. Because gaps take additional space, percentage columns totaling 100% can overflow when a gap is present.
Important Consideration
Fixed-width columns do not automatically shrink to fit narrow screens. Consider minmax(), flexible units, or media queries when responsiveness is required.
Fractional Units
The fr unit represents a fraction of the available flexible space in the grid container.
Two Equal Columns
.container {
grid-template-columns: 1fr 1fr;
}
Unequal Column Proportions
.container {
grid-template-columns: 1fr 2fr 1fr;
}
The middle column receives two shares of flexible space, while each outer column receives one share, subject to intrinsic sizing constraints.
Fixed Sidebar with Flexible Content
.container {
grid-template-columns: 200px 1fr;
}
Prevent Content from Forcing Overflow
.container {
grid-template-columns: minmax(0, 1fr) minmax(0, 2fr);
}
Using minmax(0, ...) removes the automatic minimum track sizing constraint. Content inside the columns may still need appropriate wrapping or overflow handling.
Important Consideration
Fractional units distribute flexible space after the grid sizing algorithm accounts for fixed tracks, gaps, and applicable sizing constraints.
The repeat() Function
The repeat() function reduces repetitive code by defining a track pattern multiple times.
Repeat Three Equal Columns
.container {
grid-template-columns: repeat(3, 1fr);
}
Equivalent Expanded Declaration
.container {
grid-template-columns: 1fr 1fr 1fr;
}
Repeat Fixed-Width Columns
.container {
grid-template-columns: repeat(4, 150px);
}
Repeat a Track Pattern
.container {
grid-template-columns: repeat(2, 100px 1fr);
}
This creates four columns following the pattern 100px 1fr 100px 1fr.
Combine Repeated and Individual Tracks
.container {
grid-template-columns: 180px repeat(3, minmax(0, 1fr));
}
Important Consideration
The first argument of repeat() can be a positive integer or an automatic repetition keyword. Automatic repetition has additional restrictions on track sizing.
The minmax() Function
The minmax() function establishes minimum and maximum sizing limits for a grid track.
Minimum Width with Flexible Growth
.container {
grid-template-columns: minmax(180px, 1fr) 2fr;
}
Fixed Minimum and Maximum
.container {
grid-template-columns: minmax(150px, 300px) 1fr;
}
Allow Columns to Shrink
.container {
grid-template-columns: repeat(3, minmax(0, 1fr));
}
Content-Based Maximum
.container {
grid-template-columns: minmax(150px, max-content) 1fr;
}
Important Consideration
The minimum value cannot be a flexible fr size. A flexible maximum such as 1fr is allowed. If the maximum is smaller than the minimum, the maximum is effectively ignored.
auto-fill and auto-fit
The auto-fill and auto-fit keywords work with repeat() to create responsive columns based on available space.
Using auto-fill
.container {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
gap: 12px;
}
The grid creates as many repeated tracks as fit within the available space. Empty tracks remain part of the layout.
Using auto-fit
.container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
gap: 12px;
}
The grid initially determines how many tracks fit, then collapses empty repeated tracks. Occupied tracks can expand into the freed space.
Comparison
| Keyword | Behavior |
|---|---|
auto-fill |
Preserves empty repeated tracks. |
auto-fit |
Collapses empty repeated tracks after placement. |
Important Consideration
Automatic repetition requires definite or appropriately constrained track sizes. A declaration such as repeat(auto-fit, 1fr) is invalid because the track size does not provide a definite basis for calculating the repetition count.
Content-Based Sizing
Content-based track sizing uses the intrinsic dimensions of grid items to help determine column widths.
Automatic Sizing
.container {
grid-template-columns: auto 1fr;
}
The first column sizes according to its contents and available space, while the second uses a flexible track.
Minimum Content Width
.container {
grid-template-columns: min-content 1fr;
}
The min-content keyword uses the smallest intrinsic width the content can occupy.
Maximum Content Width
.container {
grid-template-columns: max-content 1fr;
}
The max-content keyword sizes the track according to its preferred intrinsic width without optional wrapping.
Limit Content-Based Growth
.container {
grid-template-columns: fit-content(250px) minmax(0, 1fr);
}
The fit-content() function caps the track's maximum sizing contribution while respecting intrinsic minimum requirements.
Important Consideration
Long unbreakable text, images, and other intrinsic-size content can affect track dimensions. Use appropriate wrapping, min-width, or overflow rules when needed.
Named Grid Lines
Named grid lines provide descriptive references for column boundaries, making item placement easier to understand.
Define Named Column Lines
.container {
display: grid;
grid-template-columns:
[sidebar-start] 200px
[sidebar-end main-start] minmax(0, 1fr)
[main-end];
}
Position an Item Using Names
.main {
grid-column: main-start / main-end;
}
Multiple Names for One Line
.container {
grid-template-columns:
[left] 200px
[middle sidebar-end content-start] 1fr
[right];
}
One grid line can have multiple names separated by whitespace inside square brackets.
Repeated Named Lines
.container {
grid-template-columns: repeat(3, [column-start] 1fr);
}
.item {
grid-column-start: column-start 2;
}
The integer selects a particular occurrence of the named grid line.
Important Consideration
Named grid lines are case-sensitive. A name referenced by a grid item should match the intended line definition.
Subgrid
The subgrid keyword allows a nested grid container to use the column tracks of its parent grid rather than defining an independent column track list.
Define the Parent Grid
.parent-grid {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 12px;
}
Create a Column Subgrid
.nested-grid {
display: grid;
grid-column: 1 / -1;
grid-template-columns: subgrid;
}
The nested grid spans all three parent columns and uses their track structure for its own columns.
Subgrid with Additional Line Names
.nested-grid {
grid-template-columns: subgrid [first] [second] [third] [last];
}
Additional names can be assigned to the inherited grid lines without changing the parent track sizes.
Important Consideration
Subgrid requires a nested grid item that participates in a parent grid. Browser compatibility for subgrid should be checked separately from basic grid-template-columns support.
Implicit Columns
CSS Grid can create implicit columns when items are placed beyond the explicitly defined column tracks.
Define Two Explicit Columns
.container {
display: grid;
grid-template-columns: repeat(2, 150px);
}
Place an Item in an Additional Column
.item {
grid-column: 4;
}
The item is placed in the fourth column, requiring two implicit columns beyond the explicit grid.
Size Implicit Columns
.container {
display: grid;
grid-template-columns: repeat(2, 150px);
grid-auto-columns: 100px;
}
Automatically Generate Columns
.container {
display: grid;
grid-auto-flow: column;
grid-auto-columns: minmax(120px, auto);
}
Important Consideration
The grid-template-columns property defines explicit columns. The grid-auto-columns property controls the sizing of implicit columns.
Responsive Layouts
Responsive grid layouts can use flexible columns, automatic repetition, or media queries to adapt to different viewport widths.
Responsive Card Layout
.card-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 220px), 1fr));
gap: 16px;
}
The min() function allows the minimum track width to shrink to the container width when less than 220 pixels are available.
Single-Column Mobile Layout
.page-layout {
display: grid;
grid-template-columns: minmax(0, 1fr);
gap: 16px;
}
Two-Column Desktop Layout
@media (min-width: 768px) {
.page-layout {
grid-template-columns: 220px minmax(0, 1fr);
}
}
Three-Column Layout on Wider Screens
@media (min-width: 1200px) {
.page-layout {
grid-template-columns: 220px minmax(0, 1fr) 220px;
}
}
Important Consideration
Consider the width of the grid container, not only the viewport. A component can be narrow even on a wide screen when placed inside a sidebar or constrained parent.
Shorthand Relationship
The grid-template-columns property is a longhand component of the grid-template and grid shorthands.
Separate Longhand Properties
.container {
grid-template-rows: 80px 1fr;
grid-template-columns: 200px 1fr;
}
Equivalent grid-template Shorthand
.container {
grid-template: 80px 1fr / 200px 1fr;
}
Override Only the Columns
.container {
grid-template: 80px 1fr / 200px 1fr;
grid-template-columns: 250px minmax(0, 1fr);
}
The final declaration changes the column track definitions while preserving the existing row track definitions.
Shorthand Reset Behavior
.container {
grid-template-columns: repeat(3, 1fr);
grid-template: 100px / 200px 1fr;
}
The later shorthand replaces the earlier three-column definition with two columns.
Important Consideration
The grid-template shorthand also sets grid-template-areas. Use the column longhand when you want to change only the column tracks.
Inheritance
The grid-template-columns property is not inherited.
Parent Grid Container
.parent {
display: grid;
grid-template-columns: 200px 1fr;
}
Nested Grid Container
.nested {
display: grid;
grid-template-columns: 1fr 1fr;
}
The nested grid defines its own column tracks rather than automatically using the parent's column sizes.
Explicit Inheritance
.nested {
grid-template-columns: inherit;
}
The inherit keyword copies the computed value from the parent. It does not provide the track-sharing behavior of subgrid.
Reset to the Initial Value
.container {
grid-template-columns: initial;
}
Reset with unset
.container {
grid-template-columns: unset;
}
Because the property is not inherited, unset resets it to none.
grid-template-columns Example
This example creates a three-column layout using 1fr 2fr 1fr. The middle column receives twice the flexible space of either outer column, while the grid items demonstrate how different column proportions affect their widths.
HTML Example
<div class="grid-template-columns-demo">
<div class="grid-item">Column 1</div>
<div class="grid-item featured">Column 2</div>
<div class="grid-item">Column 3</div>
</div>
CSS Example
.grid-template-columns-demo {
display: grid;
grid-template-columns: minmax(0, 1fr) minmax(0, 2fr) minmax(0, 1fr);
gap: 12px;
}
.grid-template-columns-demo .grid-item {
display: flex;
align-items: center;
justify-content: center;
min-height: 90px;
padding: 10px;
background-color: #596579;
color: #ffffff;
text-align: center;
font-weight: bold;
border-radius: 4px;
min-width: 0;
}
.grid-template-columns-demo .featured {
background-color: #2456a6;
}
The middle column receives two flexible shares, while the outer columns each receive one. The 12-pixel gaps are accounted for before the remaining space is distributed.
Change to Equal Columns
.grid-template-columns-demo {
grid-template-columns: repeat(3, minmax(0, 1fr));
}
This change gives all three columns equal widths.
Change to Two Columns
.grid-template-columns-demo {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
The third item moves to a new row through automatic placement because only two explicit columns remain.
Common Mistakes
Forgetting to Enable CSS Grid
.container {
grid-template-columns: 1fr 1fr;
}
This declaration does not create a grid formatting context by itself. Add display: grid or display: inline-grid.
Using Commas Between Tracks
.container {
grid-template-columns: 100px, 200px;
}
Track sizes are separated by whitespace, not commas.
Using an Invalid Fractional Value
.container {
grid-template-columns: 1 fr 2 fr;
}
Fractional units must be attached to their numbers, such as 1fr and 2fr.
Using Invalid Automatic Repetition
.container {
grid-template-columns: repeat(auto-fit, 1fr);
}
Automatic repetition needs a track size that allows the browser to determine how many tracks fit. Use a definite minimum, such as minmax(180px, 1fr).
Assuming Fractional Tracks Always Shrink
A track sized with 1fr has an automatic minimum that can be influenced by its contents. Use minmax(0, 1fr) when shrinking below intrinsic content width is appropriate.
Forgetting About Gaps
Gaps occupy space between tracks. Percentage-based columns totaling 100% can overflow when additional gaps are included.
Confusing Explicit and Implicit Columns
The grid-template-columns property defines explicit tracks. Use grid-auto-columns to size implicitly generated columns.
Using Fixed Columns on Narrow Screens
Multiple fixed-width columns may overflow smaller containers. Consider flexible sizing, automatic repetition, or media queries.
Important Considerations
Keep the following points in mind when using grid-template-columns:
- The property defines column tracks in the explicit grid.
- Each track size establishes a column unless a repetition function expands the track list.
- Fixed lengths provide predictable column widths.
- The
frunit distributes available flexible space. - The
repeat()function simplifies repeated track definitions. - The
minmax()function establishes minimum and maximum track sizing limits. - The
auto-fillandauto-fitkeywords support responsive repetition. - Named grid lines can simplify item placement.
- The
subgridkeyword allows nested grid items to share parent column tracks. - The
grid-auto-columnsproperty controls implicit column sizing. - Grid gaps consume space between columns.
- Intrinsic content sizes can influence flexible track dimensions.
- The
grid-templateandgridshorthands can override column definitions. - Column placement does not change document order or keyboard navigation order.
Browser Support
The CSS grid-template-columns property is widely available in modern browsers, including Chrome, Edge, Firefox, Safari, and Opera.
For current compatibility information, visit Can I Use? and search for grid-template-columns.
Advanced features such as subgrid have separate compatibility requirements. Check support for those features when using them in production layouts.
Specifications
The grid-template-columns property is defined in the CSS Grid Layout specification.
CSS Grid Layout Module Level 1: grid-template-columns
The specification defines explicit grid tracks, track sizing, repetition, and named grid lines.
CSS Grid Layout Module Level 2
For additional documentation and examples, consult MDN Web Docs.
