Ova

What is the grid-template-columns Property Used For?

Published in CSS Grid Columns 6 mins read

The grid-template-columns property in CSS Grid Layout is used to define the columns of a grid, specifying both their number and their individual widths. This foundational CSS property is crucial for structuring the horizontal arrangement of content within a grid container, allowing developers to precisely control how items are distributed across the layout.

Understanding grid-template-columns

At its core, grid-template-columns specifies the number (and the widths) of columns in a grid layout. When you apply this property to a grid container element, you provide a space-separated list of values. Each value in this list directly corresponds to a single column, defining its size. For instance, if you provide three distinct values, your grid will establish three columns, with each value dictating the width of one of those columns.

This property is a primary tool for establishing the column grid tracks. It works alongside properties like grid-template-rows (for defining rows) and grid-template-areas (for naming grid regions) to construct robust and adaptive web layouts.

How to Define Column Sizes

The versatility of grid-template-columns stems from the various types of values you can use to specify column sizes. The values are provided as a space-separated list, where each value sets the size for its respective column.

Common Value Types

Here's a breakdown of the most common ways to define column widths:

  • Fixed Units: You can set columns to precise, unchangeable dimensions using units like pixels (px), ems (em), rems (rem), or points (pt).
    • Example: grid-template-columns: 100px 200px 50px; creates three columns with fixed widths.
  • Flexible Units (fr): The fractional unit (fr) is designed for responsive layouts, allowing columns to take up a proportion of the available space. For instance, in 1fr 2fr 1fr, the second column will be twice as wide as the first and third.
    • Example: grid-template-columns: 1fr 2fr 1fr;
  • Percentage (%): Columns can be sized as a percentage of the grid container's overall width.
    • Example: grid-template-columns: 25% 50% 25%;
  • auto Keyword: Columns defined with auto will automatically size themselves. They can expand to accommodate their content or evenly distribute remaining space if other columns have explicit sizes.
    • Example: grid-template-columns: 100px auto 1fr;

Advanced Column Definitions with Functions

CSS Grid offers powerful functions that enable more dynamic and responsive column layouts:

repeat() Function

The repeat() function streamlines the process of defining multiple columns that share the same size or follow a repeating pattern.

  • Syntax: repeat(count, track-size) or repeat(auto-fill|auto-fit, track-size)

  • Usage Examples:

    • repeat(3, 1fr) is a concise way to write 1fr 1fr 1fr.
    • repeat(2, 100px 1fr) creates a pattern like 100px 1fr 100px 1fr.

    When combined with auto-fill or auto-fit, the repeat() function becomes exceptionally powerful for creating fluid, dynamic grids where columns adjust based on the container's width.

    • auto-fill: Instructs the grid to create as many columns as possible to fill the available space. If there's extra room, it might create empty tracks.
      • Example: grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    • auto-fit: Similar to auto-fill, but it collapses any empty tracks, effectively fitting only the number of columns that contain actual content.
      • Example: grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));

minmax() Function

The minmax() function allows you to define a size range for a grid track. This ensures that a column is never smaller than a specified minimum value and never larger than a specified maximum value, which is crucial for building robust responsive designs.

  • Syntax: minmax(min, max)
  • Usage Example: grid-template-columns: minmax(100px, 1fr) 2fr; Here, the first column will maintain a minimum width of 100px but can expand up to 1 fractional unit of the available space.

Practical Examples of grid-template-columns

Let's explore how grid-template-columns is implemented in practical web design scenarios.

Example 1: Basic Three-Column Layout

.grid-container {
  display: grid;
  /* Defines three columns: 150px fixed, 1fr flexible, 2fr flexible */
  grid-template-columns: 150px 1fr 2fr;
  gap: 10px; /* Space between grid items */
}

.grid-item {
  background-color: lightblue;
  padding: 20px;
  border: 1px solid #ccc;
  text-align: center;
}

In this setup, the grid will have:

  • A first column with an exact width of 150px.
  • A second column that takes up 1 fractional unit of the remaining space.
  • A third column that takes up 2 fractional units of the remaining space, making it twice as wide as the second column.

Example 2: Responsive Item Gallery

To create a responsive gallery where items arrange themselves into as many columns as fit, with each column having a minimum width of 250px:

.gallery {
  display: grid;
  /* Automatically adjust columns: min 250px, max 1fr of available space */
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 15px;
  padding: 20px;
}

.gallery-item {
  border: 1px solid #eee;
  box-shadow: 2px 2px 5px rgba(0,0,0,0.1);
  text-align: center;
  padding: 15px;
}

This CSS ensures that the gallery always maintains columns that are at least 250px wide, but they can stretch proportionally (1fr) to fill any available space. As the browser window resizes, auto-fit will dynamically adjust the number of columns, providing a flexible layout.

Why grid-template-columns is Essential for Modern Layouts

  • Precise Layout Control: It offers granular control over the horizontal structure of your web pages, enabling pixel-perfect or fluid designs.
  • Exceptional Responsiveness: By intelligently using fr units, repeat(), and minmax(), you can craft layouts that adapt seamlessly and efficiently to various screen sizes and device orientations.
  • Clarity and Maintainability: Defining your grid's column structure directly within the CSS makes the layout's intentions clear and significantly easier to manage and update.

Overview of Column Sizing Values

Value Type Description Example
Fixed Units Defines an exact, unchanging width (e.g., pixels, ems, rems). 100px
fr Unit Fractional unit; takes up a proportional amount of the available space. 1fr
Percentage Sizes the column relative to the width of the grid container. 25%
auto Sizes automatically based on content or distributes remaining space evenly. auto
minmax() Sets a minimum and maximum size for a grid column. minmax(150px, 1fr)
repeat() Generates multiple columns following a specific size pattern. repeat(4, 1fr) or repeat(auto-fit, 200px)

grid-template-columns stands as a cornerstone of CSS Grid, empowering developers to create highly organized, flexible, and responsive web page layouts with unparalleled ease and control.