Ask any question about CSS here... and get an instant response.
Post this Question & Answer:
How can I create a CSS grid layout that adapts to different screen sizes?
Asked on Mar 24, 2026
Answer
Creating a CSS grid layout that adapts to different screen sizes involves using CSS Grid properties along with media queries. This allows the layout to change based on the viewport size, ensuring a responsive design.
<!-- BEGIN COPY / PASTE -->
<style>
.grid-container {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: 10px;
}
@media (max-width: 600px) {
.grid-container {
grid-template-columns: 1fr;
}
}
</style>
<div class="grid-container">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
<div>Item 4</div>
</div>
<!-- END COPY / PASTE -->Additional Comment:
- The "grid-template-columns" property uses "repeat(auto-fill, minmax(200px, 1fr))" to create flexible columns that fit the container.
- The "gap" property adds spacing between grid items.
- A media query adjusts the grid to a single column layout for screens narrower than 600px.
- This approach ensures that the grid adapts smoothly to different screen sizes, maintaining usability and aesthetics.
Recommended Links:
