Ask any question about CSS here... and get an instant response.
Post this Question & Answer:
How can I create a responsive grid layout using CSS Grid?
Asked on Mar 13, 2026
Answer
Creating a responsive grid layout with CSS Grid involves defining a flexible grid structure that adapts to different screen sizes. You can achieve this by using grid-template-columns with relative units like percentages or the fr unit, and incorporating media queries for responsiveness.
<!-- BEGIN COPY / PASTE -->
<style>
.grid-container {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
gap: 10px;
}
.grid-item {
background-color: #f2f2f2;
padding: 20px;
text-align: center;
}
</style>
<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>
<!-- END COPY / PASTE -->Additional Comment:
- The "grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));" allows the grid to automatically adjust the number of columns based on the container's width.
- The "minmax(150px, 1fr)" ensures each grid item is at least 150px wide but can grow to fill available space.
- Using "gap" provides consistent spacing between grid items.
- Media queries can further refine the layout for specific breakpoints if needed.
Recommended Links:
