Ask any question about CSS here... and get an instant response.
Post this Question & Answer:
How can I create a responsive CSS grid layout that adapts to different screen sizes?
Asked on Mar 17, 2026
Answer
Creating a responsive CSS grid layout involves using CSS Grid properties combined with media queries to adjust the layout based on screen size. This approach ensures that your design is flexible and adapts to various devices.
<!-- BEGIN COPY / PASTE -->
<style>
.grid-container {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: 10px;
}
.grid-item {
background-color: #f2f2f2;
padding: 20px;
text-align: center;
}
@media (max-width: 600px) {
.grid-container {
grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
}
}
</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>
<!-- END COPY / PASTE -->Additional Comment:
- The "grid-template-columns" property uses "auto-fill" and "minmax" to create flexible columns that adapt to the container's width.
- "minmax(200px, 1fr)" ensures each column is at least 200px wide but can grow to fill the space equally.
- Media queries adjust the grid layout for smaller screens, changing the minimum column width to 150px in this example.
- Use "gap" to control the spacing between grid items.
Recommended Links:
