CSS grid-template-columns property

The CSS grid-template-columns property is used to define the number and size of columns in a CSS Grid layout. This property allows you to specify how much space each column should take up and how the grid items should be distributed across the columns.

Syntax

selector {
    grid-template-columns: value1 value2 ... valueN;
}

Possible Values

Value Description
auto Column size adjusts based on content
length Fixed size using px, em, rem, etc.
% Percentage of container width
fr Fraction of available space
repeat() Repeats a pattern of column sizes

Example 1: Auto-Sized Columns

The following example creates a 2-column grid where columns automatically adjust to content size −

<!DOCTYPE html>
<html>
<head>
<style>
    .container {
        display: grid;
        background-color: #3498db;
        grid-template-columns: auto auto;
        padding: 20px;
        gap: 20px;
    }
    .container > div {
        background-color: #f39c12;
        border: 2px solid #2c3e50;
        padding: 20px;
        font-size: 18px;
        text-align: center;
        color: white;
    }
</style>
</head>
<body>
    <div class="container">
        <div>Item 1</div>
        <div>Item 2</div>
        <div>Item 3</div>
        <div>Item 4</div>
    </div>
</body>
</html>
A 2x2 grid layout with orange grid items on a blue background. Items are arranged in two columns with equal spacing.

Example 2: Fixed and Fractional Units

This example demonstrates mixing different unit types for more control over column sizing −

<!DOCTYPE html>
<html>
<head>
<style>
    .container {
        display: grid;
        background-color: #2c3e50;
        grid-template-columns: 100px 1fr 2fr;
        padding: 20px;
        gap: 15px;
        height: 200px;
    }
    .container > div {
        background-color: #e74c3c;
        border: 1px solid #fff;
        padding: 15px;
        text-align: center;
        color: white;
        display: flex;
        align-items: center;
        justify-content: center;
    }
</style>
</head>
<body>
    <div class="container">
        <div>Fixed 100px</div>
        <div>1 fraction</div>
        <div>2 fractions</div>
    </div>
</body>
</html>
A 3-column grid where the first column is 100px wide, the second takes 1/3 of remaining space, and the third takes 2/3 of remaining space.

Conclusion

The grid-template-columns property provides flexible control over grid column layouts. You can use auto-sizing, fixed dimensions, percentages, or fractional units to create responsive and well-structured grid layouts.

Updated on: 2026-03-15T12:59:13+05:30

105 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements