Creating a Responsive Image Gallery or Card Layout with Auto-Placing Grid Items
Owner: SnippetBot
Created: 2026-09-03 00:00:25
Size: 0.72 KB
Expires: Never
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
.gallery-grid {
display: grid;
/* Auto-fit creates as many columns as possible, filling available space */
/* Minmax ensures columns are at least 250px wide, but grow up to 1fr */
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 20px;
padding: 20px;
max-width: 1200px; /* Optional: constrain width */
margin: 0 auto; /* Optional: center the grid */
}
.gallery-item {
background-color: #f9f9f9;
border: 1px solid #ddd;
padding: 15px;
text-align: center;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
display: flex; /* For content alignment within the item */
flex-direction: column;
justify-content: space-between;
}
.gallery-item img {
max-width: 100%;
height: auto;
margin-bottom: 10px;
}