Ask any question about CSS here... and get an instant response.
Post this Question & Answer:
How can I create a responsive grid layout with CSS Grid?
Asked on Mar 22, 2026
Answer
Creating a responsive grid layout with CSS Grid involves defining a grid container and specifying how items should be placed within it. CSS Grid provides a flexible way to create complex layouts that adapt to different screen sizes.
<!-- BEGIN COPY / PASTE -->
<style>
.grid-container {
display: grid;
grid-template-columns: repeat(auto-fit, 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>
<!-- END COPY / PASTE -->Additional Comment:
- The "grid-template-columns" property uses "repeat" and "auto-fit" to create a responsive layout that adjusts 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 available space.
- The "gap" property adds spacing between grid items, enhancing visual separation.
- This setup automatically adapts to different screen sizes, making it ideal for responsive design.
Recommended Links:
