CSS
CSS Grid: Responsive Columns with `minmax` and `auto-fit`
Create a flexible and responsive grid layout where columns automatically adjust and fit within the container, ensuring items don't become too narrow.
.grid-container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 20px; /* Space between grid items */
padding: 20px;
background-color: #e8e8e8;
}
.grid-item {
background-color: #f9f9f9;
padding: 20px;
border: 1px solid #ddd;
text-align: center;
}
How it works: The `display: grid` property initializes the grid container. `grid-template-columns: repeat(auto-fit, minmax(250px, 1fr))` is key for responsiveness. `auto-fit` fills the row with as many columns as possible. `minmax(250px, 1fr)` ensures each column is at least 250px wide but can grow to fill available space equally (`1fr`). The `gap` property adds consistent spacing between grid items, improving readability and layout.