Explain the use of figure tag in HTML5

In HTML, the <figure> tag is used to include self-contained information such as illustrations, diagrams, photographs, or code listings in a document. It is linked to the main flow, but it may be used at any place of a document, and the figure flows with the content, thus removing it should not impact the flow of the document. HTML5 includes a new tag for this purpose. Because the <figure> element is a sectioning root, the outline of its content is removed from the page’s main outline.

Use of Figure tag: To embed an image in an HTML page, use the <img> element. Images are linked to web pages rather than being placed into them. The <img> element generates a placeholder for the specified image. The <figcaption> element is used to give a picture a caption. It is an optional tag that can come before or after the tag’s content. Although the element itself may include several additional components such as or, only one element may be nested within a tag. The element is used with the element, and it might be the element’s first or last child. A <figure> is often a picture, illustration, diagram, code snippet, or another item that is referenced in the main flow of a document but may be relocated to another portion of the text or an appendix without disrupting the main flow. The outline of the content of the <figure> element is omitted from the main outline of the page since it is a sectioning root. A caption can be connected with the <figure> element by putting an <figcaption> as the first or final child inside it. The figure’s caption is given as the first <figcaption> element found in the figure.

Syntax:

<figure>
    <img src="....">
    <figcaption>.......</figcaption>
</figure>

Parameters:

  1. img src: This tag is used in the document to include an image source.
  2. figcaption: This element is used to specify the image’s caption.

 

Example 1: In this example, to mark up a w3wiki image in a document, we will use the <figure> element and the <figcaption> element to specify a caption for the image.

HTML




<!DOCTYPE html>
<html>
  <head>
    <title>w3wiki</title>
  </head>
  <body>
    <figure>
      <img src=
"https://media.w3wiki.net/wp-content/uploads/20210823163000/1.png" 
           alt="gfg" width="320" height="250">
       <figcaption>
         w3wiki | A computer science portal for Beginner
       </figcaption>
    </figure>
 </body>
</html>


Output:

Example 2: In this example, we will use a figure tag with multiple images nested within a single <figure> element with a single caption.

HTML




<!DOCTYPE html>
<html>
   <head>
      <title>Page Title</title>
   </head>
   <body>
      <figure>
         <img src=
 "https://media.w3wiki.net/wp-content/cdn-uploads/20210727192049/CP_ad_icon.png" 
              width="110">
         <img src=
 "https://media.w3wiki.net/wp-content/cdn-uploads/20210203145720/DSA-SP_1-min.png" 
              width="110">
         <img src=
 "https://media.w3wiki.net/wp-content/cdn-uploads/20210609101834/GC-LIve-Icon-1.png"
             width="110">
         <figcaption>
           w3wiki | A computer science portal for Beginner
         </figcaption>
      </figure>
   </body>
</html>


Output: