Google Advertisement

How to Create a Beautiful HTML Image Gallery with <figure> and <figcaption>

Google Advertisement
πŸ”₯ Read with Full Features on Our Website

Learn how to create a responsive and semantic image gallery using HTML <figure> and <figcaption> tags. Step-by-step tutorial with code examples for beginners.

Published on 04 May 2025
By Vandu

Make an Image Gallery Using <figure> and <figcaption> in HTML

Creating an image gallery in HTML is a great beginner project to learn the basics of web development. In this tutorial, you’ll learn how to use two important HTML5 tags — <figure> and <figcaption> — to create a semantic, organized, and responsive image gallery.

πŸ”₯ Read with Full Features on Our Website

Let’s break it down step by step.


🧠 What are <figure> and <figcaption>?

These tags help improve accessibility and make your HTML code more readable and SEO-friendly.


πŸ”§ Step-by-Step: Build the Image Gallery

βœ… Step 1: Basic HTML Structure

Google Advertisement

Start with a simple HTML5 structure.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>My Image Gallery</title>
  <style>
    /* We'll add some CSS here later */
  </style>
</head>
<body>
  <h1>My Simple Image Gallery</h1>
</body>
</html>

πŸ‘‰ This is a basic template where we’ll build our gallery.


βœ… Step 2: Add Images Using <figure> and <figcaption>

Inside the <body>, use the <figure> tag to add images with captions.

<figure>
  <img src="nature1.jpg" alt="Beautiful mountain view">
  <figcaption>Mountain View</figcaption>
</figure>

<figure>
  <img src="nature2.jpg" alt="Sunset at the beach">
  <figcaption>Sunset at Beach</figcaption>
</figure>

<figure>
  <img src="nature3.jpg" alt="Forest with morning fog">
  <figcaption>Foggy Forest</figcaption>
</figure>

πŸ“ Explanation:


βœ… Step 3: Style with CSS for a Grid Layout

Let’s make it look like a real gallery by adding CSS styles.

<style>
  body {
    font-family: Arial, sans-serif;
    padding: 20px;
    background: #f4f4f4;
  }

  h1 {
    text-align: center;
    color: #333;
  }

  .gallery {
    display: flex;
    flex-wrap: wrap;
    gap: 20px;
    justify-content: center;
  }

  figure {
    width: 300px;
    border: 1px solid #ddd;
    padding: 10px;
    background: #fff;
    box-shadow: 2px 2px 8px rgba(0,0,0,0.1);
    text-align: center;
  }

  figure img {
    max-width: 100%;
    height: auto;
    display: block;
    margin-bottom: 8px;
  }

  figcaption {
    font-size: 1rem;
    color: #555;
  }
</style>

πŸ“ Explanation:


βœ… Step 4: Wrap All Figures Inside a Gallery Container

Google Advertisement

Update your HTML like this:

<div class="gallery">
  <figure>
    <img src="nature1.jpg" alt="Beautiful mountain view">
    <figcaption>Mountain View</figcaption>
  </figure>

  <figure>
    <img src="nature2.jpg" alt="Sunset at the beach">
    <figcaption>Sunset at Beach</figcaption>
  </figure>

  <figure>
    <img src="nature3.jpg" alt="Forest with morning fog">
    <figcaption>Foggy Forest</figcaption>
  </figure>
</div>

🎯 Now all your image cards are inside a .gallery container that uses CSS Flexbox to form a clean layout.


βœ… Final Code: Copy and Run This

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>My Image Gallery</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      padding: 20px;
      background: #f4f4f4;
    }

    h1 {
      text-align: center;
      color: #333;
    }

    .gallery {
      display: flex;
      flex-wrap: wrap;
      gap: 20px;
      justify-content: center;
    }

    figure {
      width: 300px;
      border: 1px solid #ddd;
      padding: 10px;
      background: #fff;
      box-shadow: 2px 2px 8px rgba(0,0,0,0.1);
      text-align: center;
    }

    figure img {
      max-width: 100%;
      height: auto;
      display: block;
      margin-bottom: 8px;
    }

    figcaption {
      font-size: 1rem;
      color: #555;
    }
  </style>
</head>
<body>

  <h1>My Simple Image Gallery</h1>

  <div class="gallery">
    <figure>
      <img src="nature1.jpg" alt="Beautiful mountain view">
      <figcaption>Mountain View</figcaption>
    </figure>

    <figure>
      <img src="nature2.jpg" alt="Sunset at the beach">
      <figcaption>Sunset at Beach</figcaption>
    </figure>

    <figure>
      <img src="nature3.jpg" alt="Forest with morning fog">
      <figcaption>Foggy Forest</figcaption>
    </figure>
  </div>

</body>
</html>

πŸ’‘ Note: Replace nature1.jpg, nature2.jpg, etc. with the actual paths to your images.


πŸŽ‰ Conclusion

Using <figure> and <figcaption> is a great way to build clean, semantic image galleries in HTML. It not only makes your code organized and accessible but also looks professional when styled with CSS.

You’ve learned how to:

❀️ Like πŸ’¬ Comment πŸ”— Share
Google Advertisement
πŸ‘‰ View Full Version on Main Website β†—
Google Advertisement
πŸ‘‰ Read Full Article on Website