Is img a self-closing tag?

Ah, the img tag! A cornerstone of the web, responsible for bringing all those wonderful images to our screens. But when you write it, have you ever paused to wonder: Is img a self-closing tag? The short answer is a resounding yes, it absolutely is.


Unlike tags such as <p> (paragraph) or <div> (division), which wrap around content that belongs *inside* them and therefore require both an opening and a distinct closing tag (e.g., <p>Some text</p>), the <img> tag doesn't contain any content itself. Its job is simply to embed an image from an external source, specified by its `src` attribute, and provide alternative text via the `alt` attribute. Because there's no 'inside' to close, it doesn't need a separate closing tag like `</img>`.


In modern HTML5, you'll most commonly see it written without a trailing slash: <img src="image.jpg" alt="Description">. While stricter HTML standards like XHTML once required the trailing slash for empty elements (e.g., <img src="image.jpg" alt="Description" />), HTML5 made it optional. Browsers are incredibly forgiving and will correctly interpret both forms. So, whether you include the trailing slash or not, your browser understands that the <img> tag stands alone, defining an empty element that points to a resource rather than containing content. It's a key example of how HTML's syntax is designed around the purpose of each element.


Related Keywords: HTML empty elements, HTML5 syntax

No comments

Powered by Blogger.