CSS
Create a Responsive Grid Layout Without Media Queries
Build a flexible, responsive grid that automatically adjusts column count based on viewport size, using CSS Grid's `auto-fit` and `minmax` functions for dynamic layouts.
.responsive-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 20px; /* Space between grid items */
padding: 20px;
background-color: #f0f0f0;
}
.grid-item {
background-color: #fff;
border: 1px solid #ddd;
padding: 20px;
text-align: center;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
How it works: This CSS Grid snippet creates a powerful responsive layout that adapts automatically without the need for traditional media queries. The `grid-template-columns: repeat(auto-fit, minmax(250px, 1fr))` property tells the grid to automatically create as many columns as can fit. Each column will be at least `250px` wide (`minmax(250px, 1fr)`) and will expand to fill available space equally (`1fr`) if there's extra room. The `gap` property adds consistent spacing between grid cells.