Open In App

CSS grid-auto-columns Property

Last Updated : 27 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The grid-auto-columns CSS property specifies the size of implicitly-created grid columns using values like auto, lengths, percentages, minmax(), and more, ensuring flexible and controlled grid column sizing.

Syntax

grid-auto-columns: auto | max-content | min-content | 
length | percentage |
minmax(min, max) | initial | inherit;

Property Values

Property Value

Description

auto

It is the default value. The size is determined implicitly according to the size of the container. 

length

It is used to specify the size as an integer length. Negative values are not allowed. 

percentage

It specifies the size as a percentage value. 

max-content

It specifies the size depending on the largest item in the container.

min-content

It specifies the size depending on the smallest item in the container.

minmax(min, max)

It specifies the size in the range of [min, max]. greater than or equal to min and less than or equal to max. 

initial

It sets the grid-auto-columns property to its default value. 

inherit

It sets the grid-auto-columns property from its parent element. 

Example 1: Using grid-auto-columns: auto

In this example we defines a grid container where items are placed into named areas using grid-template-areas. The grid-auto-columns property sets the width of columns not explicitly defined.

<!DOCTYPE html>
<html>

<head>
    <title>
        CSS grid-auto-column Property
    </title>

    <style>
        .main {
            display: grid;
            grid-template-areas: "a a";
            grid-gap: 20px;
            padding: 30px;
            background-color: green;
            grid-auto-columns: auto;

        }

        .GFG {
            text-align: center;
            font-size: 35px;
            background-color: white;
            padding: 20px 0;
        }
    </style>
</head>

<body>
    <div class="main">
        <div class="GFG">1</div>
        <div class="GFG">2</div>
        <div class="GFG">3</div>
        <div class="GFG">4</div>
        <div class="GFG">5</div>
    </div>
</body>

</html>

Output:

Example 2: Using grid-auto-columns: length

In this example we creates a grid container with fixed-width columns using grid-auto-columns. Each item inside has a centered text with a white background.

<!DOCTYPE html>
<html>

<head>
    <title>
        CSS grid-auto-column Property
    </title>

    <style>
        .main {
            display: grid;
            grid-template-areas: "a a";
            grid-gap: 20px;
            padding: 30px;
            background-color: green;
            grid-auto-columns: 8.5cm;

        }

        .GFG {
            text-align: center;
            font-size: 35px;
            background-color: white;
            padding: 20px 0;
        }
    </style>
</head>

<body>
    <div class="main">
        <div class="GFG">1</div>
        <div class="GFG">2</div>
        <div class="GFG">3</div>
        <div class="GFG">4</div>
        <div class="GFG">5</div>
    </div>
</body>

</html>

Output:

Example 3: Using grid-auto-columns: percentage

In this example we creates a grid container with columns set to 30% width of the container. Each item features centered text with a white background for a clean visual presentation.

<!DOCTYPE html>
<html>

<head>
    <title>
        CSS grid-auto-column container Property
    </title>

    <style>
        .main {
            display: grid;
            grid-template-areas: "a a";
            grid-gap: 20px;
            padding: 30px;
            background-color: green;
            grid-auto-columns: 30%;

        }

        .GFG {
            text-align: center;
            font-size: 35px;
            background-color: white;
            padding: 20px 0;
        }
    </style>
</head>

<body>
    <div class="main">
        <div class="GFG">1</div>
        <div class="GFG">2</div>
        <div class="GFG">3</div>
        <div class="GFG">4</div>
        <div class="GFG">5</div>
    </div>
</body>

</html>

Output: 

Using grid-auto-columns: minmax(min, max)

In this example we defines a grid container where columns dynamically adjust between 100px and 4cm. Each item has centered text with a white background, creating a visually structured layout.

<!DOCTYPE html>
<html>

<head>
    <title>
        CSS grid-auto-column Property
    </title>
    <style>
        .main {
            display: grid;
            grid-template-areas: "a a";
            grid-gap: 20px;
            padding: 30px;
            background-color: green;
            grid-auto-columns: minmax(100px, 4cm);

        }

        .GFG {
            text-align: center;
            font-size: 35px;
            background-color: white;
            padding: 20px 0;
        }
    </style>
</head>

<body>
    <div class="main">
        <div class="GFG">1</div>
        <div class="GFG">2</div>
        <div class="GFG">3</div>
        <div class="GFG">4</div>
        <div class="GFG">5</div>
    </div>
</body>

</html>

Output:

Example 5: Using grid-auto-columns: initial

In this example we creates a grid container with automatic column sizing based on content width. Each item has centered text with a white background, maintaining a structured layout within a green background.

<!DOCTYPE html>
<html>

<head>
    <title>
        CSS grid-auto-column Property
    </title>
    <style>
        .main {
            display: grid;
            grid-template-areas: "a a";
            grid-gap: 20px;
            padding: 30px;
            background-color: green;
            grid-auto-columns: initial;

        }

        .GFG {
            text-align: center;
            font-size: 35px;
            background-color: white;
            padding: 20px 0;
        }
    </style>
</head>

<body>
    <div class="main">
        <div class="GFG">1</div>
        <div class="GFG">2</div>
        <div class="GFG">3</div>
        <div class="GFG">4</div>
        <div class="GFG">5</div>
    </div>
</body>

</html>

Output:

Example 6: Using grid-auto-columns: inherit

In this example we creates a grid container with columns inheriting their width. Each item has centered text with a white background, maintaining a structured layout within a green background.

<!DOCTYPE html>
<html>

<head>
    <title>
        CSS grid-auto-column Property
    </title>
    <style>
        .main {
            display: grid;
            grid-template-areas: "a a";
            grid-gap: 20px;
            padding: 30px;
            background-color: green;
            grid-auto-columns: inherit;

        }

        .GFG {
            text-align: center;
            font-size: 35px;
            background-color: white;
            padding: 20px 0;
        }
    </style>
</head>

<body>
    <div class="main">
        <div class="GFG">1</div>
        <div class="GFG">2</div>
        <div class="GFG">3</div>
        <div class="GFG">4</div>
        <div class="GFG">5</div>
    </div>
</body>

</html>

Output:

Supported Browsers

The browser supported by grid-auto-columns property are listed below:

CSS grid-auto-columns Property – FAQs

What is the grid-auto-columns property used for in CSS?

The grid-auto-columns property sets the size of columns that are created automatically when the grid container has more items than explicitly defined columns.

How do I set a fixed width for auto-generated columns?

To set a consistent width for automatically generated columns, use: grid-auto-columns: 200px; which ensures every new column is 200px wide.

What is the effect of setting grid-auto-columns to auto?

When grid-auto-columns is set to auto, the width of each column is determined based on the content within, allowing flexible column sizes based on what’s inside.

Can I set different sizes for implicit columns?

Yes, you can control the size of implicit columns by specifying values like minmax(100px, auto) in the grid-auto-columns property to create responsive columns.

When should I use grid-auto-columns?

Use grid-auto-columns when your grid layout might include more items than explicitly defined columns, especially in dynamic content grids where additional columns may need specific sizing.



Next Article

Similar Reads

three90RightbarBannerImg