CSS

🎯 CSS grid-template-columns Property

The grid-template-columns property is one of the most important features of CSS Grid Layout. It defines how many columns a grid container will have and determines the width of each column.

With this property, you can create fixed-width columns, flexible layouts, or columns that automatically adapt to their content.

  • 📌 Why Use grid-template-columns?

  • Create responsive grid layouts.
  • Define fixed or flexible column widths.
  • Build modern page structures without floats.
  • Easily control column sizing and spacing.

🔹 grid-template-columns Values

1️⃣ none

No explicit columns are defined. The browser handles column creation automatically.

CSS

.container {
    grid-template-columns: none;
}
                

2️⃣ auto

Column widths are automatically adjusted according to their content.

CSS

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

3️⃣ max-content

Each column grows according to the maximum width required by its content.

CSS

.container {
    grid-template-columns: max-content max-content;
}
                

4️⃣ min-content

Columns shrink to the smallest possible size without overflowing the content.

CSS

.container {
    grid-template-columns: min-content min-content;
}
                

5️⃣ Length Values

You can define columns using units such as px, %, rem, em, and fr.

CSS

.container {
    grid-template-columns: 200px 300px;
}

.container {
    grid-template-columns: 50% 50%;
}

.container {
    grid-template-columns: 1fr 2fr;
}
                

The fr unit is commonly used in CSS Grid and represents a fraction of the available space.

6️⃣ initial

Resets the property to its default browser value.

CSS

.container {
    grid-template-columns: initial;
}
                

🧩 Common Examples

Equal Width Columns

CSS

.container {
    grid-template-columns: repeat(3, 1fr);
}
                

Fixed Sidebars with Flexible Center

CSS

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

Content-Based Columns

CSS

.container {
    grid-template-columns: auto auto;
}
                
  • ✔️ Quick Summary

  • grid-template-columns defines the number and width of grid columns.
  • fr creates flexible columns that share available space.
  • auto sizes columns according to content.
  • min-content and max-content size columns based on content limits.
  • Fixed units such as px, %, and rem can also be used.