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 May 17, 2026
Answer
Creating a responsive grid layout with CSS Grid involves defining grid areas and using media queries to adjust the layout for different screen sizes. Here's a basic example to get you started.
<!-- BEGIN COPY / PASTE -->
<style>
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 10px;
}
.grid-item {
background-color: #f0f0f0;
padding: 20px;
text-align: center;
}
@media (max-width: 768px) {
.grid-container {
grid-template-columns: repeat(2, 1fr);
}
}
@media (max-width: 480px) {
.grid-container {
grid-template-columns: 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 class="grid-item">5</div>
<div class="grid-item">6</div>
</div>
<!-- END COPY / PASTE -->Additional Comment:
- The "grid-template-columns" property defines the number of columns and their width. "1fr" means one fraction of the available space.
- Media queries adjust the grid layout based on the screen width, ensuring responsiveness.
- The "gap" property adds space between grid items.
- Start with a desktop-first approach, then use media queries to adapt for smaller screens.
Recommended Links:
