The Wayback Machine - https://web.archive.org/web/20240917020116/https://www.geeksforgeeks.org/how-to-create-a-responsive-css-grid-layout/
Open In App

How to Create a Responsive CSS Grid Layout ?

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

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:

Using “auto-fill” property

This approach utilizes CSS Grid to create a responsive grid layout. The grid-template-columns property, with repeat(auto-fill, minmax(200px, 1fr)), dynamically adjusts the number of columns based on available space, ensuring each column maintains a minimum width of 200 pixels while stretching to fill any remaining space equally. grid-gap: 10px; sets the gap between grid items. Styling is applied to the grid items using .grid > div { … }, specifying properties like font size, padding, text color, and background color. This approach provides a flexible grid structure that adapts to various screen sizes, making it suitable for responsive web design. The HTML structure includes a container with the class grid and multiple div elements representing grid items. Overall, this approach offers an efficient and straightforward method for creating responsive grid layouts in web development.

Example: This example shows the responsive CSS grid layout.

HTML
<!DOCTYPE html>
<html>

<head>
    <title> Responsive grid with auto-fill</title>
    <style>
        .grid {
            display: grid;
            grid-template-columns: repeat(auto-fill,
              minmax(200px, 1fr));
            grid-gap: 10px
        }

        .grid>div {
            font-size: 20px;
            padding: 1rem;
            color: yellow;
            text-align: center;
            background: red;
        }
    </style>
</head>

<body>
    <div class="grid">
        <div> Div 1</div>
        <div>Div 2</div>
        <div>Div 3</div>
        <div>Div 4</div>
        <div>Div 5</div>
        <div>Div 6</div>
        <div>Div 7</div>
        <div>Div 8</div>
    </div>


</body>

</html>

Output:

cat2

Output gif


Using both “auto-fill” and “auto-fit”

This approach provides flexibility in creating responsive grid layouts, allowing developers to choose between filling the space with fixed-width columns (auto-fill) or adjusting column widths to fit the container (auto-fit).The .auto-fill and .auto-fit classes are used to control the behavior of the grid columns. .auto-fill creates as many columns as possible with a minimum width of 200 pixels, while .auto-fit ensures that the columns fit snugly within the container, expanding or contracting as needed.

Example: This example shows the responsive CSS grid layout.

HTML
<!DOCTYPE html>
<html>

<head>
    <title> Responsive grid 
      with auto-fill</title>
    <style>
        .grid {
            display: grid;
            margin: 8px;
            grid-gap: 10px
        }

        .grid>div {
            font-size: 20px;
            padding: 1rem;
            color: yellow;
            text-align: center;
            background: red;
        }

        .auto-fit {
            grid-template-columns: repeat(auto-fit,
                    minmax(200px, 1fr));
        }

        .auto-fill {
            grid-template-columns: repeat(auto-fill,
                    minmax(200px, 1fr));
        }
    </style>
</head>

<body>

    <div class="grid auto-fill">
        <div>Div 1</div>
        <div>Div 2</div>

    </div>

    <div class="grid auto-fit">
        <div>Div 1</div>
        <div>Div 2</div>

    </div>




</body>

</html>

Output:

cat1

Output gif


Using media query

This approach uses media query to create a responsive webpage layout. The .wrapper class defines the overall grid container with specified grid areas for header, sidebar, content, and footer. Media queries are used to adjust the layout based on screen width: for small screens, the layout is single-column with the header, sidebar, content, and footer stacked vertically; for medium screens, it transitions to a two-column layout with the sidebar and content beside each other and the header and footer spanning both columns. Each grid area is styled accordingly using .box class, with specific colors and padding. The HTML structure contains div elements with corresponding classes to populate the grid areas. This approach offers flexibility and responsiveness to different screen sizes while maintaining a structured layout.

Example: This example shows the responsive CSS grid layout by using media query.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>Title of the document</title>
    <style>
        body {
            margin: 40px;
        }

        .sidebar {
            grid-area: sidebar;
        }

        .content {
            grid-area: content;
        }

        .header {
            grid-area: header;
        }

        .footer {
            grid-area: footer;
        }

        .wrapper {
            background-color: #eeeeee;
            color: #444;
            padding: 10px;
            margin: auto;
            display: grid;
            grid-gap: 1em;
            grid-template-areas:
                "header" "sidebar" "content" "footer";
        }

        @media only screen and (min-width: 500px) {
            .wrapper {
                grid-template-columns: 20% auto;
                grid-template-areas:
                    "header   header" "sidebar  content" "footer   footer";
            }
        }

        @media only screen and (min-width: 600px) {
            .wrapper {
                grid-gap: 20px;
                grid-template-columns: 120px auto;
                grid-template-areas:
                    "header  header" "sidebar content" "footer  footer";
                max-width: 600px;
            }
        }

        .box {
            background-color: #444;
            color: #ffffff;
            border-radius: 5px;
            padding: 10px;
            font-size: 20px;
        }

        .header,
        .footer {
            background-color: blue;
        }

        .sidebar {
            background-color: #ccc;
            color: #444;
        }

        .content {
            background: rgb(16, 160, 179);
        }
    </style>
</head>

<body>
    <div class="wrapper">
        <div class="box header">Header</div>
        <div class="box sidebar">Sidebar</div>
        <div class="box content">
            Content

        </div>
        <div class="box footer">Footer</div>
    </div>
</body>

</html>

Output:

cat3

Output gif



Similar Reads

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
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
Create a Responsive Blog Layout in Tailwind CSS
A blog layout is a fundamental component of many websites, enabling content creators to showcase their articles in an organized and visually appealing manner. We will explore how to create a responsive Blog Layout in Tailwind CSS for styling purposes. Prerequisites:Tailwind CSSJavaScriptApproachFirst, Create a layout in HTML and Integrate Tailwind
9 min read
How to Create a Responsive Image Grid using CSS?
A responsive image grid is a layout technique used to display images in a grid-like structure that adjusts dynamically based on the screen size to ensure that the grid looks good and functions well across various devices and screen resolutions. Below are the approaches to creat a responsive image grid using CSS: Table of Content Using CSS GridUsing
3 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
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
How to Create a Responsive Layout with React Bootstrap ?
Creating a responsive layout with React Bootstrap means designing a web page that adapts and looks good on various screen sizes and devices. React Bootstrap provides responsive components and grid system classes that help in organizing and scaling content effectively, ensuring a seamless user experience across desktops, tablets, and mobile devices.
3 min read
How to hide elements in responsive layout using CSS ?
CSS can be used to hide or display elements of the page. This can be used in a responsive website to show certain elements that are only possible to interact with a larger screen size for a good user experience. Media queries are the technique introduced in CSS3 to help to design responsive websites. A media query has two parts, one is the optional
3 min read
Pure CSS Responsive Horizontal-to-Vertical Menu Layout
To create a responsive horizontal-to-vertical menu layout using only CSS, you can use media queries to apply different styles to the menu based on the width of the viewport. Example 1: This will create a horizontal menu with evenly distributed menu items on larger screens, and a vertical menu with full-width menu items on smaller screens. You can a
3 min read
Pure CSS Responsive Horizontal-to-Scrollable Menu Layout
A Horizontal-to-Scrollable menu is a type of responsive menu bar in Pure.CSS, which changes the horizontal menu bar to a horizontally scrollable menu bar. It is used to make the menu bar responsive when the width of the website increases or decreases from device to device, For example, Tablets, Smartphones, etc. The Pure.CSS Horizontal-to-Scrollabl
4 min read
Pure CSS Responsive Side Menu Layout
Pure CSS is a free and open-source CSS framework. It is a set of tools for building responsive websites and web applications. This was created by Yahoo and is used to create websites that are more responsive, attractive, and speedy. It serves as a strong substitute for Bootstrap. Pure CSS was created with consideration for responsive design. We thu
5 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
Foundation CSS XY Grid Responsive Adjustments
Foundation CSS is an open-source &amp; responsive front-end framework built by ZURB foundation in September 2011, that makes it easy to layout stunning responsive websites, apps, and emails that appear amazing &amp; can be accessible to any device. In this article, we will discuss the XY Block Grid in Foundation CSS. The XY Grid Responsive Adjustme
2 min read
Foundation CSS Flex Grid Responsive Adjustments
Foundation CSS is an open-source &amp; responsive front-end framework built by ZURB foundation in September 2011, that makes it easy to design beautiful responsive websites, apps, and emails that look amazing &amp; can be accessible to any device. It is used by many companies such as Facebook, eBay, Mozilla, Adobe, and even Disney. The framework is
2 min read
Creating Responsive Grid Vanilla using HTML,CSS and Bootstrap
Many modern websites use a grid system to display chunks of data on their home-page as well as other pages. To much extent grid resembles a table, however, it is much more visually appealing and most often displays sibling elements. A grid must be responsive, i.e. it should adjust to the screen size of the user. Bootstrap is a very popular open-sou
5 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
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
CSS Grid Layout Module
The CSS Grid Layout Module is a revolutionary tool in the realm of web development. It introduces a grid-based layout system, facilitated by rows and columns, that simplifies the design process of any webpage. This system eliminates the need for floats and positioning, making it a game-changer for developers. iframe { width: 100%; height: 330px;} @
7 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
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
Best way to make a d3.js visualization layout responsive
D3 stands for Data-Driven Documents. It is an open-source JavaScript library that is used to create interactive data visualizations in the browser using HTML, SVG, and CSS. The huge amount of data is being generated in today's world, which is very difficult to store and analyze. Visual representations of data are the most effective means of conveyi
3 min read
Responsive Column Layout using React and Tailwind
Column Layout is commonly employed on web pages to present content side by side on larger screens, allowing the efficient display of substantial information. On smaller screens where side-by-side viewing is impractical, a row format is adopted using the responsiveness concept. Preview of final output: Let us have a look at how the final layout will
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
Semantic-UI Responsive Grid Reverse Order
Semantic UI is an open-source framework that uses CSS and jQuery to build great user interfaces. It is the same as a bootstrap for use and has great different elements to use to make your website looks more amazing. It uses a class to add CSS to the elements. Semantic-UI Grid layout is used to harmonize the negative space layout. The grid element i
3 min read
Semantic-UI Responsive Grid Stackable
Semantic UI is an open-source framework that uses CSS and jQuery to build great user interfaces. It is the same as a bootstrap for use and has great different elements to use to make your website looks more amazing. It uses a class to add CSS to the elements. Semantic-UI Grid layout is used to harmonize the negative space layout. The grid element i
4 min read
Semantic-UI Responsive Grid Doubling
Semantic UI is an open-source framework that uses CSS and jQuery to build great user interfaces. It is the same as a bootstrap for use and has great different elements to use to make your website looks more amazing. It uses a class to add CSS to the elements. Semantic-UI Grid layout is used to harmonize the negative space layout. The grid element i
3 min read
Semantic-UI Responsive Grid Manual Tweaks
Semantic UI is an open-source framework that uses CSS and jQuery to build great user interfaces. It is the same as a bootstrap for use and has great different elements to use to make your website looks more amazing. It uses a class to add CSS to the elements. Semantic-UI Grid layout is used to harmonize the negative space layout. The grid element i
4 min read