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.
Let’s break it down step by step.
π§ What are <figure>
and <figcaption>
?
-
<figure>
: This HTML tag is used to group media content, such as an image, along with its caption. -
<figcaption>
: This tag is used inside the<figure>
tag to provide a caption or description for the image.
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
Start with a simple HTML5 structure.
π 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.
π Explanation:
-
<img>
tag displays the image. -
alt
attribute helps screen readers and improves SEO. -
Google Advertisement
<figcaption>
provides a text label for the image.
β Step 3: Style with CSS for a Grid Layout
Let’s make it look like a real gallery by adding CSS styles.
π Explanation:
-
.gallery
class uses Flexbox to arrange images side by side and wrap them on smaller screens. -
Each
<figure>
has padding, border, and shadow to make it look like a card. -
Images are responsive with
max-width: 100%
.
β Step 4: Wrap All Figures Inside a Gallery Container
Update your HTML like this:
π― 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
π‘ 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:
-
Use HTML5 semantic tags for images
-
Add captions to your images
-
Style a responsive gallery using Flexbox