How can I create a responsive CSS grid layout that adapts to different screen sizes?
Asked on Oct 08, 2025
Answer
Creating a responsive CSS grid layout involves using CSS Grid properties and media queries to adjust the layout based on screen size. Here's a basic example to get you started.
<!-- BEGIN COPY / PASTE -->
<style>
.grid-container {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: 10px;
}
.grid-item {
background-color: #f0f0f0;
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" property uses "repeat(auto-fill, minmax(200px, 1fr))" to create a flexible grid that adjusts the number of columns based on the container's width.
- "minmax(200px, 1fr)" ensures each grid item is at least 200px wide but can grow to fill the available space.
- Use media queries to further customize the grid for specific breakpoints if needed.
- This approach ensures the grid is responsive and adapts to various screen sizes without additional media queries.
Recommended Links: