Open In App

How to Set Three Columns in One Particular Row in CSS Grid?

Last Updated : 08 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Setting up a grid layout with specific columns in CSS can sometimes be a bit challenging, especially if we are new to CSS Grid. It is a powerful tool that helps us to create complex easily. In this article, we are going to learn about different approaches to setting three columns in one particular row in the CSS grid, with their syntax and code examples.

CSS Grid is a layout system designed to make it easier to create complex web layouts. It allows us to divide our page into rows and columns. It gives us control over the size, position, and alignment of elements within the grid. One common task is setting up a row with a specific number of columns—such as three columns in one particular row—while other rows might have a different number of columns or structure.

Below are different approaches to Setting Three Columns in One Particular Row:

Using grid-template-columns for a Specific Row

The grid-template-columns property is used to define the number and size of columns in a grid. By setting it for a particular row, we can control how many columns appear and how they are sized.

Example: In this example, the grid-template-columns: repeat(3, 1fr); line creates a grid with three equal columns. The 1fr unit means "one fraction of the available space," so each column will take up an equal amount of space.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width,
                   initial-scale=1.0">
    <title>Three Columns in One Row</title>
    <style>
        .grid-container {
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            gap: 10px;
        }

        .grid-item {
            background-color: lightblue;
            padding: 20px;
            text-align: center;
        }
    </style>
</head>

<body>
    <div class="grid-container">
        <div class="grid-item">Column 1</div>
        <div class="grid-item">Column 2</div>
        <div class="grid-item">Column 3</div>
    </div>
</body>

</html>

Output:

grid-template-columns
Using grid-template-columns for a Specific Row

Using Grid Item Placement

If you want to set up three columns in just one specific row while leaving other rows with a different number of columns, you can use the grid-column property to place grid items.

Example: In this example, the first item spans all three columns, creating a full-width row. The next three items each occupy one column in the second row, setting up three columns in that particular row.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Specific Row with Three Columns</title>
    <style>
        .grid-container {
            display: grid;
            grid-template-columns: 1fr 1fr 1fr;
            gap: 10px;
        }

        .grid-item:nth-child(1) {
            grid-column: span 3;
        }

        .grid-item {
            background-color: lightgreen;
            padding: 20px;
            text-align: center;
        }
    </style>
</head>

<body>
    <div class="grid-container">
        <div class="grid-item">Full Width Row</div>
        <div class="grid-item">Column 1</div>
        <div class="grid-item">Column 2</div>
        <div class="grid-item">Column 3</div>
    </div>
</body>

</html>

Output:

grid-placement
Using Grid Item Placement

Using a Combination of grid-template-areas and grid-column

You can also define specific grid areas and use them to control the layout of your grid items, which can be useful when you want more control over where items are placed.

Example: In this example, we use grid-template-areas to define the layout structure. The second row has three columns (col1, col2, col3), and other areas (header and footer) are full-width.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width,
                   initial-scale=1.0">
    <title>Grid Areas for Specific Rows</title>
    <style>
        .grid-container {
            display: grid;
            grid-template-areas:
                "header header header"
                "col1 col2 col3"
                "footer footer footer";
            gap: 10px;
        }

        .header {
            grid-area: header;
        }

        .col1 {
            grid-area: col1;
        }

        .col2 {
            grid-area: col2;
        }

        .col3 {
            grid-area: col3;
        }

        .footer {
            grid-area: footer;
        }

        .grid-item {
            background-color: lightcoral;
            padding: 20px;
            text-align: center;
        }
    </style>
</head>

<body>
    <div class="grid-container">
        <div class="grid-item header">Header</div>
        <div class="grid-item col1">Column 1</div>
        <div class="grid-item col2">Column 2</div>
        <div class="grid-item col3">Column 3</div>
        <div class="grid-item footer">Footer</div>
    </div>
</body>

</html>

Output:

combo-area-column
Using a Combination of grid-template-areas and grid-column

Conclusion

It is easy to set three columns in one particular row using CSS Grid. By using different properties such as grid-template-columns, grid-column, and grid-template-areas, we can create flexible and responsive layouts according to our needs.


Next Article

Similar Reads