Ask any question about CSS here... and get an instant response.
Post this Question & Answer:
How can I use CSS grid to create a responsive gallery layout? Pending Review
Asked on May 20, 2026
Answer
CSS Grid is a powerful layout system that can be used to create a responsive gallery layout by defining grid areas and adjusting them based on screen size. Here's a simple example to get you started.
<!-- BEGIN COPY / PASTE -->
<style>
.gallery {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
gap: 10px;
}
.gallery-item {
background-color: #ddd;
padding: 20px;
text-align: center;
}
</style>
<div class="gallery">
<div class="gallery-item">1</div>
<div class="gallery-item">2</div>
<div class="gallery-item">3</div>
<div class="gallery-item">4</div>
<div class="gallery-item">5</div>
<div class="gallery-item">6</div>
</div>
<!-- END COPY / PASTE -->Additional Comment:
- The "grid-template-columns" property uses "repeat(auto-fill, minmax(150px, 1fr))" to create a responsive layout where each item has a minimum width of 150px and grows to fill the available space.
- The "gap" property adds spacing between grid items, enhancing visual separation.
- Adjust the "minmax" values to control the minimum size and flexibility of your grid items.
- This setup automatically adjusts the number of columns based on the container's width, making it responsive.
Recommended Links:
