Search code examples
htmlweb

Why do some `img` tags have no end tag while others do?


I read in tutorials that img tags don't have ending tag. But, while inspecting html of various pages I noticed that some img tags have ending tag </img> or <img ... /> and some don't have ending tag and appear just <img ... >. In what cases they don't have an ending tag?


Solution

    • XHTML requires that all elements be explicitly ended (e.g. </img>) but provides alternative syntax with a / at the end of the start tag (instead of an end tag) for elements which have no content.
    • HTML forbids end tags on <img> elements but (for backwards compatibility with XHTML) permits a meaningless / to appear at the end of a start tag on elements where the end tag is forbidden.
    • People also make mistakes