How to create a responsive CSS grid layout?
Oct 16, 2025 am 10:49 AMUse display: grid and repeat(auto-fit, minmax()) to create a responsive layout, achieve flexible adaptation through fr units and gaps, and combine with media queries to finely control the number of columns and spacing on different screens.
To create a responsive CSS Grid layout, you start by defining a container with display: grid and use properties like grid-template-columns , fr units, and minmax() in combination with media queries or the repeat() function to make it adapt to different screen sizes. The key is flexibility—using relative units and letting the browser handle column distribution.
Set up the grid container
Apply display: grid on a parent element to turn it into a grid container. Define columns using grid-template-columns and set spacing with gap .
- Use fr units to distribute available space proportionally.
- Add gap for consistent spacing between grid items.
Example:
.container { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 16px; }
Make columns responsive with repeat() and minmax()
Instead of fixed column counts, use repeat() and minmax() to create flexible layouts that adjust based on container size.
- repeat(auto-fit, ...) automatically fits as many columns as possible.
- minmax(200px, 1fr) means each column is at least 200px but can grow to fill available space.
Example:
.container { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 16px; }
This approach works across devices without writing multiple media queries.
Adjust behavior with media queries (optional)
If more control is needed, use media queries to redefine the grid at specific breakpoints.
- Change column count or layout for mobile, tablet, and desktop.
- Adjust gap sizes or font styles alongside grid changes.
Example:
.container { display: grid; grid-template-columns: 1fr; gap: 12px; } @media (min-width: 768px) { .container { grid-template-columns: repeat(2, 1fr); gap: 16px; } } @media (min-width: 1024px) { .container { grid-template-columns: repeat(3, 1fr); gap: 20px; } }
Basically, use repeat(auto-fit, minmax()) for quick responsiveness, or combine explicit grid definitions with media queries when precise control is needed. It's not complex, just pay attention to how content reflows on smaller screens.
The above is the detailed content of How to create a responsive CSS grid layout?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

ArtGPT
AI image generator for creative art from text prompts.

Stock Market GPT
AI powered investment research for smarter decisions

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

With the popularity of mobile devices and the development of technology, responsive layout has become one of the essential skills for designers. Responsive layout is designed to provide the best user experience for screens of different sizes, allowing web pages to automatically adjust their layout on different devices to ensure the readability and usability of content. Choosing the right units is one of the key steps in responsive layout design. This article will introduce some commonly used units and provide suggestions for selecting units. Pixel (px): Pixel is the smallest unit on the screen. It is an absolute unit and does not automatically change as the screen size changes.

Responsive layout framework competition: who is the best choice? With the popularity and diversification of mobile devices, responsive layout of web pages has become more and more important. In order to cater to the different devices and screen sizes of users, it is essential to adopt a responsive layout framework when designing and developing web pages. However, with so many framework options out there, we can’t help but ask: which one is the best choice? The following will be a comparative evaluation of three popular responsive layout frameworks, namely Bootstrap, Foundation and Tailwind.

How to use HTML to implement responsive layout design. With the popularity of mobile devices and the rapid development of the Internet, responsive layout has become an essential skill for designers. Responsive layout allows the website to automatically adapt to different screen sizes and resolutions on different devices, allowing users to have a better browsing experience. This article will introduce how to use HTML to implement responsive layout design and provide specific code examples. Using @media query @media query is a feature in CSS3 that can be applied based on different media conditions

Application skills of HTML fixed positioning in responsive layout, specific code examples are required. With the popularity of mobile devices and the increase in user demand for responsive layout, developers have encountered more challenges in web design. One of the key issues is how to implement fixed positioning to ensure that elements can be fixed at specific locations on the page under different screen sizes. This article will introduce the application skills of HTML fixed positioning in responsive layout and provide specific code examples. Fixed positioning in HTML is through the position attribute of CSS

CSSGrid and Flexbox can be used in combination, but Grid is more suitable for two-dimensional layouts, while Flexbox is good at one-dimensional layouts. 1.Grid defines grid structure through grid-template-rows and grid-template-columns, which is suitable for complex two-dimensional layouts. 2. Flexbox controls direction and space allocation through flex-direction and flex attributes, suitable for one-dimensional layout and simple responsive design. 3. In terms of performance, Flexbox is suitable for simple layouts, and Grid is suitable for complex layouts, but may affect browser rendering performance. 4. Compatibility, Flexbox supports more extensively, Grid in modern browsers

Responsive Layout Framework Analysis: An Essential Guide from Beginners to Experts With the popularity and diversification of mobile devices, responsive layout has become an essential skill for modern web design. The responsive layout framework has become the preferred tool for developers due to its simplicity, flexibility and maintainability. However, for beginners, learning and understanding responsive layout frameworks can feel a little confusing. From beginner to expert, this article provides you with a detailed guide to mastering the responsive layout framework, along with concrete code examples. What is responsive cloth

With the rapid development of mobile Internet, more and more people are beginning to use mobile phones and tablets to browse the web, which has brought huge challenges to traditional web design. Traditional web design is often based on desktops, and the screen sizes and resolutions of mobile phones and tablets are different from desktop computers. If you continue to use traditional fixed-width web design, it will lead to difficulty in displaying on mobile devices and poor user experience. Not good. Responsive layout is a web design method that can be displayed adaptively on different devices. It brings a better browsing experience to users.

CSS's minmax() function is used to define the minimum and maximum size range of grid tracks, thereby improving layout flexibility. Its core function is to let the developer specify a size interval, such as minmax (200px, 1fr) means that the column width is at least 200px and can be stretched to 1fr at most. Common uses include responsive card layout, automatic column width adjustment of data tables, and balanced blank areas. Commonly used combinations include minmax (200px, 1fr), minmax (min-content,max-content), minmax (150px, 300px) and minmax (auto, 1fr). Notes include avoiding setting too high minimum values ??and testing different screens
