The <figure>
and <figcaption>
elements in HTML are used to provide a way to group an image (or other content) along with a caption. This helps create semantic meaning in the HTML structure, making it clear that the image and caption are related. The <figure>
element acts as a container for the content, and <figcaption>
provides the description or caption of the content.
The <figure>
element is used to group content such as images, diagrams, illustrations, or other visual media. It is especially useful when you want to add a caption to describe the image or media, as it provides structure and meaning to the content.
Example of <figure>
without a caption:
<figure> <img src="https://example.com/image.jpg" alt="A beautiful landscape"> </figure>
In this example, the <figure>
element groups the image together, but there is no caption. The image is simply grouped without any descriptive text.
The <figcaption>
element is used to add a caption to content inside a <figure>
. The caption provides additional information or context for the content and is typically displayed below or above the image.
Example of <figure>
with <figcaption>
:
<figure> <img src="https://example.com/image.jpg" alt="A beautiful landscape"> <figcaption>A breathtaking view of the mountain landscape at sunset.</figcaption> </figure>
In this example, the <figcaption>
element provides a caption describing the image as "A breathtaking view of the mountain landscape at sunset." This caption will typically appear below the image, making it clear what the image represents.
The <figcaption>
element can be placed either before or after the content inside the <figure>
. Both placements are valid, and the choice depends on how you want the content to be displayed.
<figure> <figcaption>A serene lake surrounded by mountains.</figcaption> <img src="https://example.com/lake.jpg" alt="Lake and mountains"> </figure>
Here, the caption appears before the image, describing it as "A serene lake surrounded by mountains." This layout may be useful if you want the caption to introduce the image.
<figure> <img src="https://example.com/lake.jpg" alt="Lake and mountains"> <figcaption>A serene lake surrounded by mountains.</figcaption> </figure>
In this layout, the caption appears after the image. This is the more common placement, with the caption displayed as a description below the image.
The <figure>
element can contain more than just an image. It can also contain a combination of images, tables, videos, or other media. This allows you to create complex content groups with a single caption.
Example of Multiple Images with a Single Caption:
<figure> <img src="https://example.com/photo1.jpg" alt="First photo"> <img src="https://example.com/photo2.jpg" alt="Second photo"> <figcaption>A collection of photos taken during the hiking trip.</figcaption> </figure>
In this example, two images are grouped together within a <figure>
element, and a single <figcaption>
describes both images as "A collection of photos taken during the hiking trip."
The <figure>
and <figcaption>
elements in HTML are useful for grouping media content and providing clear, descriptive captions. By using <figure>
to contain images and <figcaption>
to describe them, you improve the structure and accessibility of your webpage, allowing users to better understand the context of your media content.