How can I create a responsive grid layout that adapts to different screen sizes without using media queries?
Asked on Oct 24, 2025
Answer
Creating a responsive grid layout without media queries can be effectively achieved using CSS Grid's auto-fit and auto-fill properties. These properties allow the grid to automatically adjust the number of columns based on the available space.
<!-- BEGIN COPY / PASTE -->
<div class="grid-container">
<div class="grid-item">1</div>
<div class="grid-item">2</div>
<div class="grid-item">3</div>
<div class="grid-item">4</div>
<div class="grid-item">5</div>
<div class="grid-item">6</div>
</div>
<style>
.grid-container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
gap: 10px;
}
.grid-item {
background-color: #ccc;
padding: 20px;
text-align: center;
}
</style>
<!-- END COPY / PASTE -->Additional Comment:
- The "auto-fit" keyword in "grid-template-columns" allows the grid to adjust the number of columns based on the container's width.
- "minmax(150px, 1fr)" ensures each grid item is at least 150px wide but can grow to fill the available space.
- This approach creates a flexible grid that adapts to different screen sizes without explicit media queries.
- Adjust the "minmax" values to control the minimum size and flexibility of the grid items.
Recommended Links: