Responsive CSS Grid Layout
This example demonstrates how to create a responsive grid layout using CSS Grid. The number of columns automatically adjusts based on the available space, without requiring separate media queries for each screen size.
01
Grid Item One
This is the first grid item. CSS Grid automatically positions it within the available columns.
Each card expands or contracts according to the width of the grid container.
02
Grid Item Two
This is the second grid item. The grid maintains consistent spacing between cards.
The layout adapts without requiring fixed column widths.
03
Grid Item Three
This is the third grid item. As the available space decreases, cards automatically move to additional rows.
This makes the layout suitable for responsive content sections.
04
Grid Item Four
This is the fourth grid item. Each card maintains its own content while sharing the available grid space.
CSS Grid handles the placement and alignment of the items.
05
Grid Item Five
This is the fifth grid item. Additional cards can be added without changing the grid structure.
The grid automatically creates new rows when necessary.
06
Grid Item Six
This is the sixth grid item. Responsive grids are useful for product listings, galleries, portfolios, and content cards.
The same layout can accommodate different amounts of content.
Responsive Design: Resize the browser window to see the number of grid columns change automatically. Wider screens display more columns, while narrower screens display fewer columns.
How the Grid Works
The display: grid declaration creates a CSS Grid container.
The grid-template-columns property defines how columns are created and sized.
The following declaration creates a flexible grid:
grid-template-columns: repeat(auto-fit, minmax(min(100%, 240px), 1fr));
The repeat() function generates the column tracks.
The auto-fit keyword allows the grid to fit as many columns as possible into the available space and expand the columns to fill unused space.
The minmax() function defines the minimum and maximum size of each column. Each column can grow to occupy an equal fraction of the available space.
The nested min() function allows columns to shrink below 240 pixels when the container itself is narrower than that value, preventing unnecessary horizontal overflow.
The gap: 20px declaration creates consistent spacing between grid items.
Responsive Design
Unlike layouts that use fixed column counts, this example automatically adjusts the number of columns based on the width of the grid container.
On wider screens, several cards can appear in the same row. As the available width decreases, the grid displays fewer columns until the cards appear in a single column.
A small media query also reduces the page margins, card padding, and grid spacing on narrow screens.
This approach is useful when the layout must adapt to different screen sizes without defining multiple column-count breakpoints.
Customizing the Grid Layout
You can change the minimum preferred column width by modifying the 240px value.
For example, increasing the value to 300px creates wider cards and generally results in fewer columns.
Reducing the value to 180px allows more cards to fit within each row.
You can also change the gap value to increase or decrease the spacing between grid items.
Modify the card backgrounds, borders, padding, and typography to match your website's design.
To add more items, duplicate an existing article element with the grid-card class and replace its content.