The Wayback Machine - https://web.archive.org/web/20240920185830/https://www.geeksforgeeks.org/css-grid-layout-module/
Open In App

CSS Grid Layout Module

Last Updated : 16 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The CSS Grid Layout Module provides a powerful, grid-based layout system that allows you to create complex and responsive web page layouts with rows and columns. It enables precise placement of items within a defined grid structure, eliminating the need for older layout techniques like floats, positioning, and table layouts.

CSS Grid simplifies the process of aligning elements both horizontally and vertically, offering more flexibility and control over web design, making it easier to create modern, clean, and dynamic page structures that adapt seamlessly to various screen sizes.

Currently Active Property

grid-template-columns: auto; 

CSS Grid Layout Module creates a grid-based layout system, with the help of rows and columns, and makes it easier to design any webpage without using floats and positioning.

Syntax

.class {
    display:grid;
}

Note: An HTML element becomes a grid if that element sets display: grid; in style section or inline-grid. Below you will see both examples.

CSS Grid Layout Properties

Below is a list of key CSS Grid properties with descriptions:

Property

Description

column-gap

Defines the amount of space between columns in a multi-column layout, often used with column-count in CSS Grid.

gap

Specifies the spacing (also called gutter) between rows and columns in CSS Grid and Flexbox layouts.

grid

A grid-based layout system in CSS that provides rows and columns, simplifying the design without using floats or positioning.

grid-area

IDefines the size and location of a grid item within a CSS grid layout by specifying the start and end lines.

grid-auto-columns

Sets the size of columns that are automatically generated in a CSS Grid layout when more content is added.

grid-auto-flow

It specifies exactly how auto-placed items get flow into the grid.

grid-auto-rows

It is used to specify the size for the rows of implicitly generated grid containers.

grid-column

It describes the number of properties that allow to design of grid structures and control the placement of grid items using CSS.

grid-column-end

It explains the number of columns an item will span, or on which column-line the item will end.

grid-column-gap

It is used to set the size of the gap between the columns in a grid layout.

grid-column-start

It defines for which column line item will start.

grid-gap

It is used to sets the size of the gap between the rows and columns in a grid layout.

grid-row

It is used to specify the size and location in a grid layout.

grid-row-end

It is used to define the grid item’s end position within a grid row by specifying the inline edge of its grid area.

grid-row-gap

It is used to define the size of the gap between the grid elements.

grid-row-start

It is used to define the grid items’ start position within the grid row by specifying the inline start edge of its grid area.

grid-template

It is a shorthand property for defining grid columns, rows, and areas.

grid-template-areas

It is used to specify the area within the grid layout.

grid-template-columns

It is used to set the number of columns and size of the columns of the grid.

grid-template-rows

It is used to set the number of rows and height of the rows in a grid.

CSS Grid Layout Module Examples

Example 1: Display Grid Property

This example illustrates the use of display: grid; property.

HTML
<!DOCTYPE html>
<html>

<head>
    <style>
        /* Designing all grid */
        .grid-container {
            display: grid;
            grid-template-columns: auto auto auto;
            background-color: gray;
            padding: 5px;
        }

        /* Designing all grid-items */
        .grid-item {
            background-color: rgba(255, 255, 255, 0.8);
            border: 1px solid black;
            padding: 20px;
            font-size: 30px;
            text-align: center;
        }

        /* Designing h1 element */
        h1 {
            color: green;
            text-align: center;
        }
    </style>
</head>

<body>
    <h1>GeeksforGeeks</h1>

    <!-- Creating grid -->
    <div class="grid-container">
        <div class="grid-item">1</div>
        <div class="grid-item">2</div>
        <div class="grid-item">3</div>
        <div class="grid-item">4</div>
        <div class="grid-item">5</div>
        <div class="grid-item">6</div>
        <div class="grid-item">7</div>
        <div class="grid-item">8</div>
        <div class="grid-item">9</div>
    </div>
</body>

</html>

Output:

CSS Display Grid Property Module

Example 2: Display inline-grid Property

This example illustrates the use of display : inline-grid; property.

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        /* Designing all grid */
        .grid-container {
            display: inline-grid;
            grid-template-columns: auto auto auto;
            background-color: gray;
            padding: 5px;
        }

        /* Designing all grid-items */
        .grid-item {
            background-color: rgba(255, 255, 255, 0.8);
            border: 1px solid black;
            padding: 20px;
            font-size: 30px;
        }

        /* Designing h1 element */
        h1 {
            color: green;
            text-align: center;
        }

        /* Centering content */
        .content-center {
            display: flex;
            flex-direction: column;
            align-items: center;
        }
    </style>
</head>

<body>
    <div class="content-center">
        <h1>GeeksforGeeks</h1>

        <!-- Creating grids -->
        <div class="grid-container">
            <div class="grid-item">1</div>
            <div class="grid-item">2</div>
            <div class="grid-item">3</div>
            <div class="grid-item">4</div>
            <div class="grid-item">5</div>
            <div class="grid-item">6</div>
            <div class="grid-item">7</div>
            <div class="grid-item">8</div>
            <div class="grid-item">9</div>
        </div>
    </div>
</body>

</html>

Output:

Display inline-grid example output

We can control the gaps in the grid system using these items

  • grid-column-gap
  • grid-row-gap
  • grid-gap

Example 3: Custom Grid Gap

This example uses both grid-column-gap and grid-row-gap to define the custom gap in grids.

HTML
<!DOCTYPE html>
<html>

<head>
    <style>
        /* Designing all grid */
        .grid-container {
            display: grid;
            grid-template-columns: auto auto auto;
            background-color: gray;
            grid-column-gap: 50px;
            grid-row-gap: 50px;
            padding: 5px;
        }

        /* Designing all grid-items */
        .grid-item {
            background-color: rgba(255, 255, 255, 0.8);
            border: 1px solid black;
            padding: 20px;
            font-size: 30px;
            text-align: center;
        }

        /* Designing h1 element */
        h1 {
            color: green;
            text-align: center;
        }
    </style>
</head>

<body>
    <h1>GeeksforGeeks</h1>

    <!-- Creating grids -->
    <div class="grid-container">
        <div class="grid-item">1</div>
        <div class="grid-item">2</div>
        <div class="grid-item">3</div>
        <div class="grid-item">4</div>
        <div class="grid-item">5</div>
        <div class="grid-item">6</div>
        <div class="grid-item">7</div>
        <div class="grid-item">8</div>
        <div class="grid-item">9</div>
    </div>
</body>

</html>

Output:

Note: Similarly grid-gap also works.

You can control individual grids using the following things on a grid system:

  • grid-column-lines
  • grid-row-lines

Example 4 : Custom Grid Lines

This code use both grid-column-lines and grid-row-lines together to display the customised grid.

HTML
<!DOCTYPE html>
<html>

<head>
    <style>
        /* Designing all grid */
        .grid-container {
            display: grid;
            grid-template-columns: auto auto auto;
            grid-gap: 10px;
            background-color: gray;
            padding: 10px;
        }

        /* Designing all grid-items */
        .grid-container>div {
            background-color: rgba(255, 255, 255, 0.8);
            text-align: center;
            padding: 20px 0;
            font-size: 30px;
        }

        /* Grid Column */
        .item1 {
            grid-column-start: 1;
            grid-column-end: 3;
        }

        /* Grid row */
        .item3 {
            grid-row-start: 2;
            grid-row-end: 5;
        }

        /* Designing h1 element */
        h1 {
            color: green;
            text-align: center;
        }
    </style>
</head>

<body>
    <h1>GeeksforGeeks</h1>

    <!-- Creating grids -->
    <div class="grid-container">
        <div class="item1">1</div>
        <div class="item2">2</div>
        <div class="item3">3</div>
        <div class="item4">4</div>
        <div class="item5">5</div>
        <div class="item6">6</div>
        <div class="item7">7</div>
        <div class="item8">8</div>
    </div>
</body>

</html>

Output:

Supported Browsers

The browser supported by CSS Grid Layout Module are listed below: 

Note: Older versions of these browsers may not fully support all CSS Grid properties. Always ensure compatibility by checking for browser updates or using fallback CSS techniques for older browser versions.

CSS Grid Layout Module – FAQs

What is CSS Grid Layout?

CSS Grid Layout is a system that uses rows and columns to create complex, responsive web layouts without floats or positioning.

How do you create a basic grid?

Set display: grid; on a container and define columns and rows with grid-template-columns and grid-template-rows.

What are grid lines?

Grid lines are the dividers that define the boundaries of rows and columns in a grid, used to place items precisely.

Is CSS Grid good for responsive design?

Yes, it adapts layouts to different screen sizes using media queries, fractional units (fr), and the minmax() function.

When to use CSS Grid vs. Flexbox?

Use CSS Grid for two-dimensional layouts (rows and columns) and Flexbox for one-dimensional layouts (row or column).

What does the fr unit do?

The fr unit allocates space within the grid container, proportionally distributing space among grid items.



Previous Article
Next Article

Similar Reads

Line-Based Placement of Grid Items with CSS Grid Layout
CSS Grid Layout is a flexible tool for designing web pages. One of its key features is line-based placement, which allows you to position items exactly where you want them using grid lines. This guide will explain different ways to use line-based placement in CSS. CSS Grid Layout divides a container into rows and columns, which are defined by grid
5 min read
How to create a Trello Layout with CSS Grid and Flexbox ?
Trello layout is used to organize or manage information in stages. It can be created using CSS Grid and Flexbox. Let's create the layout as shown in the below image. In the image, Trello Layout consists of Trello List which holds the items or information in it. Layout Structure: Example: To understand the Trello layout, let us first create a single
2 min read
What is CSS Grid Layout ?
The CSS Grid Layout is a powerful two-dimensional layout system that allows you to design complex web layouts with rows and columns. It provides a more flexible and efficient way to create grid-based designs compared to traditional methods. Note: An HTML element becomes a grid if that element sets display: grid; in style section or inline-grid. Syn
1 min read
How to Create a Responsive CSS Grid Layout ?
A grid layout is said to be responsive when fixed-sized grid items will shift position according to the viewport size. Responsiveness gives more professional look to the website. It is necessary to out perform your website design. we are going to learn how can we create a responsive CSS grid layout. These are the following approaches: Table of Cont
4 min read
CSS Grid Layout: The Fr Unit
The CSS Grid Layout module is used to create a grid-based layout system. Utilizing rows and columns, it simplifies the design of webpages without relying on floats and positioning. Syntax: .class { display:grid;}Note: An HTML element becomes a grid if that element sets display:grid grid-template-columns: This specifies the size of the columnsgrid-t
4 min read
Holy Grail Layout with CSS Grid
The Holy Grail layout is a widely used web layout that consists of a header, footer, left sidebar, right sidebar, and main content area. This layout is expected to be responsive and adapt to different screen sizes. Despite the varying content lengths, the layout should maintain its structure and ensure the header and footer are always at the top an
2 min read
How to Create a 3-Column Layout Grid with CSS?
Grid in CSS is a powerful layout system that allows you to create complex, responsive designs with ease. By defining rows and columns, you can position elements precisely within a grid container. Using grid properties like grid-template-columns, you can create flexible and adaptive layouts, such as a 3-column design that adjusts to various screen s
3 min read
How to Create a 2-Column Layout Grid with CSS?
Creating a two-column layout is a common design pattern used in web development. This layout helps to organize content efficiently and is used across various types of websites. A 2-column layout divides the webpage into two sections, which can be useful for creating sidebars, main content areas, or other types of content distribution. These are the
4 min read
How to Create a 4-Column Layout Grid with CSS?
We can create a layout with multiple columns using CSS. It is easier to design complex layouts. In this article, we are going to discuss how to create a 4-column layout grid using CSS. We will discuss different approaches, their syntax, and code examples of each method. A 4-column layout grid is a common design structure used in web pages to neatly
4 min read
How to Create Dynamic Grid Layout using CSS?
A dynamic grid layout using CSS can help us to build responsive and flexible web designs. In this article, we are going to learn about different approaches to achieving dynamic grid layouts, with their syntax, and example code for each method. A dynamic grid layout adjusts based on the content and screen size. It is useful for building responsive d
3 min read
CSS Grid Layout auto-placement
Auto placement in CSS Grid layout refers to the automatic placement of grid items in the grid container when explicit placement is not found. The CSS Grid Layout provides a mechanism to control how items are placed on the grid when their positions are not specified. This helps in creating layouts without the need for manually defining every grid it
3 min read
How to make a grid without calling CSS Grid property ?
In this article, we will see how to make a grid without calling CSS Grid property, along with understanding its implementation through the examples. It is essential to understand how CSS grid property works but what if we do not use the CSS grid property to make a grid. This is the generic question asked by the interviewer, which can give you a bet
4 min read
Pure CSS Regular Grid vs Responsive Grid
Pure CSS is a free and open-source CSS framework. It is a collection of tools for creating online programs and responsive web pages. This was developed by Yahoo and is used to make websites that load faster, look better, and are easier to use. Bootstrap is effectively replaced by it. When developing Pure CSS, the responsive design was taken into ac
4 min read
Bootstrap 5 Layout Form Grid
Bootstrap 5 Layout Form Grid is used to create a complex form using grid classes. The form grid layout required multiple columns of different widths and alignments. To create a form grid, we use .row and .col classes. Form Grid Layout used Classes: .row: This class is used to create a row of a form layout..col: This class is used to create columns
2 min read
ReactJS MDBootstrap Grid System Layout
MDBootstrap is a Material Design and bootstrap-based react UI library that is used to make good-looking webpages with its seamless and easy-to-use component. In this article, we will know how to use Grid System Layout in ReactJS MDBootstrap. Grid System Layout is a great tool for creating layouts that are optimized for mobile devices. It's a powerf
3 min read
Angular MDBootstrap Grid System Layout
MDBootstrap is a Material Design and bootstrap-based Angular UI library that is used to make good looking webpages with its seamless and easy-to-use component. In this article, we will know how to use Tooltips Component in Angular MDBootstap. Grid System Layout is a great tool for creating layouts that are optimized for mobile devices. Bootstrap’s
3 min read
How to create two column grid and two column layout using jQuery Mobile ?
jQuery Mobile provides CSS-based columns that are responsive. The grids have a width of 100%, grids do not have a background color, border, and padding. The grids contain elements in them, these elements can be made float side-by-side using the ui-block-a or ui-block-b or ui-block-c or ui-block-d class. Columns in a grid are of equal width. In 2 co
2 min read
Tachyons Layout Basic Grid
Tachyons Layout Basic Grid is a layout with rows and columns, making it easier to design web pages without having to use floats and positioning. We can create a variety of grids by combining widths, float, and padding. Syntax: &lt;element-name class="class-name"&gt;&lt;/element-name&gt; Tachyons Layout Basic Grid Classes: There is no class for Grid
2 min read
Tachyons Layout Debug with a Grid
Tachyons is a toolkit that is used to create a responsive website. In this article, we will learn how to define any layout using the Tachyons toolkit. Every website is divided into various parts like a header, menus, content, and a footer. Tachyons layout is used to define those sub-parts of a website. Layout debug: These classes are used to put a
2 min read
BootStrap 5 Grid system Auto-layout columns
Bootstrap's grid system is responsive. it's come with a flexbox and allows up to 12 columns across the page. you can also group the columns together. Auto-layout columns: To create easy column sizes without an explicit numbered class like .col-sm-6, Use breakpoint-specific column classes. Equal-width: The equal-width grid system is basically used t
2 min read
BootStrap 5 Layout Grid System
BootStrap5 Layout Grid system is responsive. It comes with a flexbox and allows up to 12 columns across the page. You can also group the columns together. The grid system is a powerful mobile-first responsive flexbox grid. How it works: Here’s how the grid system comes together: Its grid supports six responsive breakpoints. You can control containe
3 min read
React Chakra UI Grid Layout
React Chakra UI Grid Layout is an incredibly powerful and flexible component designed to streamline the creation of responsive and aesthetically pleasing grid-based designs within web applications. As an integral part of the Chakra UI library, this grid layout system offers developers a seamless integration with React, enabling them to build visual
2 min read
React MUI Grid Layout
React MUI Grid layout is a responsive layout grid that adapts to screen size and orientation, ensuring consistency across layouts. React MUI Grid LayoutMaterial UI Grid Component gives a responsive layout to all the screens and orientations Syntax: &lt;Grid&gt; ... &lt;/Grid&gt;MUI Grid Layout Variants:Fluid grids: This grid type uses columns that
4 min read
How to Customize the grid layout in React Bootstrap?
In this article, we will be customizing the grid layout in React Bootstrap. Grid layout is used to align the elements with ting the grid format according to the requirements. In React Bootstrap, we can control the number of columns, spacing, responsiveness, and other properties to create a more attractive User Interface. PrerequisitesReactJSReact-B
5 min read
How to use grid element using the grid auto placement rules?
In this article, you will learn to use grid elements using the grid auto-placement rules. You can use auto as a property value on the grid to auto-enable the container to customize its size on the basis of the content of the items in the row or columns. Syntax: grid-template-rows : auto grid-template-columns: auto Property Value: auto : The size of
2 min read
Bootstrap 5 Grid system Grid tiers
Bootstrap Grid system is one of the most versatile layouts which we can use and it has different responsive variants which can be used to add custom responsiveness. Across the six default breakpoints, the grid works smoothly, and all these breakpoints can also be called Grid Tiers which can be changed using the SASS version. For this, we can use th
5 min read
How To Use Grid-Row-Align And Grid-Column-Align?
The grid-row-align and grid-column-align properties don't exist in CSS. Instead, CSS Grid uses properties like "align-items" and "justify-items" for controlling alignment across rows and columns, allowing precise positioning of grid items within their cells. so we will go to see how can we align the grid items. These are the following ways: Table o
3 min read
How to use x-grid-data-generator Module in ReactJS ?
Whenever we want to generate some data and build demos for the grid Component, we can use the x-grid-data-generator module in React JS. It has a large variety of data sets to play around with. DataGrid Component helps in displaying the information in a grid-like format of rows and columns. We can use the following approach in ReactJS to use the x-g
2 min read
How to export promises from one module to another module node.js ?
JavaScript is an asynchronous single-threaded programming language. Asynchronous means multiple processes are handled at the same time. Callback functions vary for the asynchronous nature of the JavaScript language. A callback is a function that is called when a task is completed, thus helps in preventing any kind of blocking and a callback functio
1 min read
What are the differences between HTTP module and Express.js module ?
HTTP and Express both are used in NodeJS for development. In this article, we'll go through HTTP and express modules separately HTTP: It is an in-build module which is pre-installed along with NodeJS. It is used to create server and set up connections. Using this connection, data sending and receiving can be done as long as connections use a hypert
2 min read
Article Tags :