Difference between <figure> & <img> tags in HTML
In this article, we will learn about the <figure> & <img> tag along with its implementation. We will also learn the differences between them. Let’s begin the discussion with the figure tag in HTML.
HTML <figure> Tag: The <figure> tag in HTML5 is used to add self-contained content like illustrations, diagrams, photos, or codes listing in a document. It is related to the main flow, but it can be used in any position of a document and the figure goes with the flow of the document and if remove it then it should not affect the flow of the document. In simple words, the figure tag is used to semantically organize the content of images, videos, audios or even charts or tables, block of codes in the HTML document.
Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML course.
<figure> <img src="url"> <figcaption>content</figcaption> </figure>
Attributes: It contains mostly two tags which are given below:
- img src: This tag is used to add an image source in the document.
- figcaption: This tag is used to set the caption to the image. It is optional to use.
Example: This example illustrates the use of the <figure> tag along with src, width, height & alt attributes.
Output: In this case the <figure> tag, it has made all the changes, as the image with caption is properly aligned with some space away from the margin. The image and caption are part of each other. The border is added to both <figure> tag and <figcaption> tag, both have occupied the border. <figure> tag is inline element.
HTML <img> Tag: HTML <img> tag is used to add image or to set the background in the webpage/website. Nowadays website does not directly add images to a web page, as the images are linked to web pages by using the <img> tag which holds space for the image.
<img src="url" alt="some_text">
Attribute: It contains the following attribute values:
- src: It is used to specifies the URL path of the source image.
- alt: It is used to specifies an alternate text for the image if the image for some reason cannot be displayed.
Example: In this example, we are using the <img> tag along with src, width, height, and alt attributes.
Output: In the case of the <img> tag, it is not properly aligned and is very close to the margin, and the caption is away from the image. The image and caption are not part of each other. The border is not the part of both image and caption. <img> tag is an inline element but when we specify width and height it becomes a block element.
Difference between <figure> & <img> tags:
1. The figure tag is used to semantically organize the content of images, videos, audios or even charts or tables, block of codes in the HTML document. The image tag is used to add an image to an HTML page. <img> tag can only insert image. 2. <figure> tag is a container tag. <img> tag is a void tag. 3. This tag provides a container for content that is equivalent to a figure or diagram in a book. The HTML <img> tag is used for embedding images into an HTML document. 4. This tag is inline element. It is an inline element but when we specify width and height it becomes a block element. 5. You can use the figure element in conjunction with the figcaption element to provide a caption for the contents of your figure element. In image tag there’s no special tag for caption rather we can use <p> tag or <span> tag to add pseudo captions. 6. It makes it easy for the machine to understand the code. Easy to get on search engines. It is difficult for machines to understand. 7. The <figure> element itself may contain multiple other child elements be it a block of code, images, audios, video etc. The <img> tag can not have multiple elements inside it only images can be added in <img> tag. 8. The figure tag contains default alignment and styling. The image tag does not contain any default alignment and styling.