HTML <figcaption> tag
The <figcaption>
tag in HTML is used to provide a caption or description for a <figure>
element. The <figure>
element is a container for content such as images, diagrams, or illustrations, and the <figcaption>
tag provides a textual description or explanation for that content.
Syntax:
<figure>
<img src="image.jpg" alt="Description of the image">
<figcaption>This is a caption describing the image.</figcaption>
</figure>
Key Characteristics:
Caption for Figure: The
<figcaption>
tag is used to give context or additional information about the content inside the<figure>
tag. It can describe the content, provide context, or give credit.Placement: The
<figcaption>
tag must be placed inside the<figure>
tag, either before or after the content (e.g., an image or a diagram). However, it is commonly placed after the content.Semantics: The
<figcaption>
tag provides semantic meaning to the<figure>
element. It helps convey that the caption is associated with the content within the<figure>
tag.Styling: The
<figcaption>
tag can be styled using CSS. By default, browsers may apply basic styling, but additional CSS can be used to adjust the appearance, such as font size, color, alignment, and spacing.
Example Usage:
Basic Example:
<figure>
<img src="sunset.jpg" alt="A beautiful sunset over the mountains">
<figcaption>A beautiful sunset over the mountains.</figcaption>
</figure>
In this example:
- The
<img>
tag displays an image of a sunset. - The
<figcaption>
provides a textual description of the image.
Example with CSS Styling:
<style>
figure {
text-align: center;
margin: 20px;
}
figcaption {
font-style: italic;
color: #555;
margin-top: 10px;
}
</style>
<figure>
<img src="landscape.jpg" alt="Scenic landscape with a river and forest">
<figcaption>Scenic landscape with a river flowing through a lush forest.</figcaption>
</figure>
In this example:
- The
<figure>
is centered, and the<figcaption>
is styled with italic text and a specific color.
Accessibility and SEO:
- Accessibility: The
<figcaption>
tag improves accessibility by providing descriptive text for visual content. Screen readers will announce the caption along with the associated<figure>
, helping users with visual impairments understand the context of the content. - SEO: Using
<figcaption>
to provide descriptive text for images or other visual content can improve SEO by giving search engines additional context about the content. This can help with indexing and understanding the relevance of the content on the page.