CSS

Responsive Image Gallery/Card Layout with CSS Grid

Create a dynamic and responsive grid layout for image galleries or product cards that automatically adjusts the number of columns based on available screen width.

.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 20px;
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.gallery-item {
  border: 1px solid #ddd;
  padding: 10px;
  text-align: center;
  background-color: white;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  border-radius: 8px;
}

.gallery-item img {
  max-width: 100%;
  height: auto;
  display: block;
  margin-bottom: 10px;
  border-radius: 4px;
}
How it works: This code snippet builds a highly responsive grid for image galleries or card layouts. `display: grid;` activates grid properties. The key is `grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));`, which tells the grid to automatically create as many columns as can fit within the container, with each column being at least `250px` wide and expanding to fill available space (`1fr`). `gap` adds consistent spacing between grid items.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs