- HTML Tag
- Definition and Usage
- Browser Support
- Global Attributes
- Event Attributes
- More Examples
- Example
- Related Pages
- Default CSS Settings
- COLOR PICKER
- Report Error
- Thank You For Helping Us!
- How to set caption for an image using HTML?
- Syntax
- Introduction to HTML Image Captions
- Example
- Caption for an image using HTML
- Creating a Responsive Caption for an Image in HTML
- Example
- Responsive Caption for an image using HTML
- Conclusion
- HTML Add Captions to Images
- Use of the and Tags
- Add Captions on the Top of an Image
- Add Captions to Multiple Images Using the and Tags
- Add Captions to Multiple Images Using the Tag
- Conclusion
- Related Article — HTML Image
HTML Tag
Use a element to mark up a photo in a document, and a element to define a caption for the photo:
More «Try it Yourself» examples below.
Definition and Usage
The tag defines a caption for a element.
The element can be placed as the first or last child of the element.
Browser Support
The numbers in the table specify the first browser version that fully supports the element.
Element | |||||
---|---|---|---|---|---|
8.0 | 9.0 | 4.0 | 5.1 | 11.0 |
Global Attributes
Event Attributes
More Examples
Example
figcaption background-color: black;
color: white;
font-style: italic;
padding: 2px;
text-align: center;
>
Related Pages
Default CSS Settings
Most browsers will display the element with the following default values:
COLOR PICKER
Report Error
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
Thank You For Helping Us!
Your message has been sent to W3Schools.
Top Tutorials
Top References
Top Examples
Get Certified
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.
How to set caption for an image using HTML?
HTML, or Hypertext Markup Language, is used to create web pages and define their structure and content. HTML has varieties of elements to create a web page. The image element is one of the essential elements of web content. Images are a powerful tool for presenting ideas and feelings on a web page and enhancing the visual appeal of a web page. Additionally, Image captions make this even more powerful because images alone are not enough to convey a message or provide context. Captions are short descriptions that provide additional information to the viewer along with the images.
Syntax
Below is a syntax for image caption −
Introduction to HTML Image Captions
Image captions are an important part of web design images because it provides additional information to the viewer. In HTML, it is a very simple and effective way to add captions to images on a web page.
HTML allows setting a caption for an image using the and elements. The element groups the image and its caption together, while the element is used to define the caption text.
Here is an example of how to add a caption to an image using HTML −
In the above example, an image is added to the figure element, along with a caption using the figcaption element.
Example
Below is the full code example to set the caption for an image using HTML.
Caption for an image using HTML
Creating a Responsive Caption for an Image in HTML
It is important that the added image should be responsive and adapt to different screen sizes for a better user experience. CSS allows developers to create a responsive image caption. We can create a responsive image caption by styling the and elements. For example to create a responsive caption for an image, we can use the below CSS code −
In the above CSS code, we have set center alignment for the whole body, and then set the display property to block and the margin and padding properties to 0.
After that we have styled the figcaption element by setting the font family, size, and weight, as well as the color and alignment of the text. Finally, we have added a margin-top property to create some space between the image and the caption.
Example
Below is the full code example to set the responsive caption for an image.
body < text-align: center; >figure < display: block; margin: 0; padding: 0; >figcaptionResponsive Caption for an image using HTML
Conclusion
In the above article, we have discussed adding captions to images using HTML. Adding captions to images is a simple and effective way of providing additional information to the viewer. We can add the caption with an image by using the figure and fig caption elements.
HTML Add Captions to Images
- Use of the and Tags
- Add Captions on the Top of an Image
- Add Captions to Multiple Images Using the and Tags
- Add Captions to Multiple Images Using the Tag
- Conclusion
This article discusses the different approaches to writing a caption under or above an image or images on a web page using HTML and CSS properties.
Use of the and Tags
HTML has a specific tag used to insert a caption to an image. The represents a caption for a element, which can be placed as the first or last child of the element in HTML.
and are two new elements introduced as tags in HTML5. Here, the tag is best used to display images and graphics, whereas tells the viewers what they are looking at.
The tag also supports the HTML’s Global Attributes and Event Attributes.
In the following example, we are using an element to mark up an image on a web page or a document and an element to define a caption for the image:
html> body> figure> img src="/img/DelftStack/logo.png" alt="logo"> figcaption>DelftStack Logofigcaption> figure> body> html>
Here we can adjust the image’s resolution (height and width) using the style attribute of the tag in HTML.
Here, the resolution of the image is set to its original size.
Because and are new HTML5 tags, old version browsers cannot understand the process of these two tags. So these tags get rendered on the web page as inline tags, which means those tags won’t get an automatic line break for figure captions where they will just be set side-by-side with the images.
So, as a solution, we can use CSS properties to style and as in the following example:
html> head> style> figure border: 5px #4257f5 solid; padding: 4px; margin: auto; > figcaption background-color:grey; color: white; font-style: italic; padding: 2px; text-align: center; > style> head> body> figure> img src="/img/DelftStack/logo.png" alt="logo" style="width:100%"> figcaption>DelftStack Logofigcaption> figure> body> html>
Here, we can edit the and tags as per the necessity by using different properties of CSS and values.
Add Captions on the Top of an Image
Without any CSS guidelines to the contrary, the caption will appear at the figure’s top or bottom depending on whether the element is the first or last element inside the figure.
In this example, we will set the caption of the image at the top of it as follows:
html> head> style> figure border: 5px #4257f5 solid; padding: 4px; margin: auto; > figcaption background-color:grey; color: white; font-style: italic; padding: 2px; text-align: center; > style> head> body> figure> figcaption>Fig.2 - DelftStack Logofigcaption> img src="/img/DelftStack/logo.png" alt="logo" style="width:100%"> figure> body> html>
Add Captions to Multiple Images Using the and Tags
We can add captions to multiple images with the aid of :
html> body> figure> img src="/img/DelftStack/logo.png" alt="logo"> figcaption>DelftStack Logofigcaption> figure> figure> img src="/img/DelftStack/logo.png" alt="logo"> figcaption>Fig.2 - DelftStack Logofigcaption> figure> body> html>
We can use CSS properties to format the captions of the images by adding different styles or colors to the text.
Add Captions to Multiple Images Using the Tag
With the aid of the tag and CSS properties, we can add captions to multiple images at the same time. In the below example, we will add captions to two images, and optionally, we can add a link that directs to another web page or website once clicked on the image.
html> head> style> #pictures text-align:center; margin:50px auto; > #pictures a margin:0px 50px; display:inline-block; text-decoration:none; color:black; > style> head> body> div id="pictures"> a href=""> img src="/img/DelftStack/logo.png" width="480px" height="90px"> div class="caption">Fig 1 - DelftStack Logodiv> a> a href=""> img src="/img/DelftStack/logo.png" width="480px" height="90px"> div class="caption">Fig 2 - DelftStack Logodiv> a> div> body> html>
When using this method, we can add the caption of the images at the top with further implementations.
Conclusion
Different approaches can be used to caption an image or images using HTML together with CSS properties, as mentioned above. Still, some methods, like adding an image or images and their captions in a table format, are not currently appropriate.
New tags such as and in HTML5 have made the task of captioning an image easier.
Nimesha is a Full-stack Software Engineer for more than five years, he loves technology, as technology has the power to solve our many problems within just a minute. He have been contributing to various projects over the last 5+ years and working with almost all the so-called 03 tiers(DB, M-Tier, and Client). Recently, he has started working with DevOps technologies such as Azure administration, Kubernetes, Terraform automation, and Bash scripting as well.