Defining Grid Columns with grid-template-columns
You'll learn how to create explicit column tracks in your grid using specific sizing values.
What This Property Does
The grid-template-columns property lets you define exactly how many columns your grid should have and how wide each one should be. Think of it like setting up vertical lanes on a highway—you decide how many lanes exist and how wide each one is.
When you apply this property to a grid container, you're creating explicit column tracks. Each value you provide creates one column, and that value determines the column's width.
Basic Syntax
.grid-container {
display: grid;
grid-template-columns: 200px 300px 200px;
/* Creates 3 columns: 200px, 300px, 200px */
}
This creates a three-column grid with specific pixel widths.
Common Sizing Values
You can use various units:
.grid {
display: grid;
/* Fixed pixels */
grid-template-columns: 100px 200px;
/* Fractional units (fr) */
grid-template-columns: 1fr 2fr 1fr;
/* Percentages */
grid-template-columns: 25% 50% 25%;
/* Mixed units */
grid-template-columns: 200px 1fr 1fr;
}
The fr unit (which you learned earlier) is especially powerful because it distributes available space proportionally.
Multiple Columns Example
.photo-grid {
display: grid;
grid-template-columns: 1fr 1fr 1fr 1fr;
/* Four equal-width columns */
}
Key Takeaway: grid-template-columns explicitly defines the number and width of column tracks in your grid—each space-separated value creates one column with that specific size.