Skip to content

CSS Image Gallery

This tutorial demonstrates how to create a responsive image gallery using modern CSS techniques like Flexbox. We’ll focus on clean HTML and efficient CSS.

CSS, particularly Flexbox or CSS Grid, offers powerful and flexible ways to create image galleries. Below is an example of a simple responsive image gallery.

The images below are placeholders for a gallery:

We’ll use Flexbox to arrange the gallery items. Each item will consist of an image and a description.

<div class="gallery">
<figure class="gallery-item">
<a href="#" target="_blank">
<img src="klematis_small.jpg" alt="Klematis Flower 1" >
</a>
<figcaption class="gallery-caption">Add a description of the image here. This could be about Klematis Flower 1.</figcaption>
</figure>
<figure class="gallery-item">
<a href="#" target="_blank">
<img src="klematis2_small.jpg" alt="Klematis Flower 2">
</a>
<figcaption class="gallery-caption">Description for Klematis Flower 2. More details can be added.</figcaption>
</figure>
<figure class="gallery-item">
<a href="#" target="_blank">
<img src="klematis3_small.jpg" alt="Klematis Flower 3">
</a>
<figcaption class="gallery-caption">This is Klematis Flower 3. It has distinct colors.</figcaption>
</figure>
<figure class="gallery-item">
<a href="#" target="_blank">
<img src="klematis4_small.jpg" alt="Klematis Flower 4">
</a>
<figcaption class="gallery-caption">Klematis Flower 4, a beautiful specimen for the gallery.</figcaption>
</figure>
</div>
.gallery {
display: flex;
flex-wrap: wrap; /* Allow items to wrap to the next line */
gap: 16px; /* Space between gallery items */
padding: 16px;
justify-content: center; /* Center items if they don't fill the row */
}
.gallery-item {
border: 1px solid #ccc;
border-radius: 4px;
padding: 10px;
margin: 0; /* Resetting default figure margin */
background-color: #f9f9f9;
width: 200px; /* Define a width for each item */
box-shadow: 2px 2px 5px rgba(0,0,0,0.1);
display: flex;
flex-direction: column; /* Stack image and caption vertically */
}
.gallery-item img {
max-width: 100%;
height: auto;
display: block; /* Remove extra space below image */
border-bottom: 1px solid #eee;
margin-bottom: 8px;
}
.gallery-item a:hover img {
opacity: 0.8; /* Slight visual feedback on hover */
}
.gallery-caption {
font-size: 0.9em;
color: #333;
text-align: center;
margin-top: auto; /* Push caption to bottom if image heights vary */
}
  • .gallery: This container uses display: flex to arrange its children (the gallery-item elements) in a row. flex-wrap: wrap allows items to move to the next line if there isn’t enough space. gap provides spacing between items.
  • .gallery-item: Each item is a figure element, which is semantically appropriate for images and captions. width sets a base size, but Flexbox allows them to adjust. display: flex and flex-direction: column inside the item helps in aligning the image and caption.
  • .gallery-item img: Images are made responsive within their container using max-width: 100% and height: auto.
  • .gallery-caption: The figcaption provides context for the image. margin-top: auto; is a neat Flexbox trick to push the caption to the bottom if the items have varying content heights before the caption.
  • Semantic HTML: Using <figure> and <figcaption> is more semantic than generic <div> elements for an image and its description.
  • CSS Grid: For more complex two-dimensional layouts, CSS Grid could be an alternative to Flexbox.
  • Lightbox Effect: JavaScript can be added to display a larger version of the image when clicked.
  • Responsive Breakpoints: Use media queries to adjust the width of gallery-item or the number of columns for different screen sizes.