为什么 <img> 标签在 HTML 中没有关闭?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23890716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 01:53:43  来源:igfitidea点击:

Why is the <img> tag not closed in HTML?

htmlimagexhtml

提问by ??eall?

For curiosities sake, why is the <img> tag not closed in HTML?

出于好奇,为什么 < img> 标签在HTML 中没有关闭?

<img src="smiley.gif" alt="Smiley face" height="42" width="42">

I also noticed that <img> tags are explicitly closed in XHTML...

我还注意到 < img> 标签在XHTML 中被明确关闭......

<img src="smiley.gif" alt="Smiley face" height="42" width="42"/>

W3Schools:Image Tag

W3Schools:图像标签

采纳答案by Quentin

Historically, HTML has been based on SGML which allows tags to be omitted under certain conditions.

从历史上看,HTML 一直基于 SGML,它允许在某些条件下省略标签。

Since the <img>element cannot have any child nodes, it is defined as EMPTY and the end tag is forbidden (as it would serve no purpose).

由于<img>元素不能有任何子节点,它被定义为 EMPTY 并且禁止使用结束标记(因为它没有任何作用)。

XHTML is HTML expressed in XML, and XML does not support optional or forbidden tags (although it allows a self-closing tag to substitute for a start+end tag pair), so it has to be explicitly closed there.

XHTML 是用 XML 表示的 HTML,而 XML 不支持可选或禁止标签(尽管它允许自关闭标签代替开始+结束标签对),因此必须在那里显式关闭。

HTML 5 is backwards compatible with versions of HTML that were SGML based.

HTML 5 向后兼容基于 SGML 的 HTML 版本。

回答by kmoe

The <img>tag represents what is known as a void element(see HTML5 spec), so called because it can't have any contents (unlike, say <a>or <div>). Therefore there is no syntactic reason why it should need to be closed in HTML.

<img>标签代表什么被称为一个空元素(见HTML5规范),所谓的,因为它不能有任何内容(不像,说<a><div>)。因此,没有语法上的理由需要在 HTML 中关闭它。

XHTML, however, is based on XML, where every tag needs to be closed.

然而,XHTML 基于 XML,其中每个标签都需要关闭。

回答by Josh Davenport

It is what is called a void element which just means the element must not have any content (but can have attributes.) The HTML5 spec has this to say about void elements:

这就是所谓的 void 元素,意思是该元素不能有任何内容(但可以有属性)。 HTML5 规范对 void 元素有这样的说法:

if the element is one of the void elements, or if the element is a foreign element, then there maybe a single "/" (U+002F) character. This character has no effect on void elements

如果元素是空元素之一,或者元素是外来元素,则可能有一个“/”(U+002F)字符。这个字符对空元素没有影响

So there's no real reason to have the single "/" (U+002F) character, but it won't break anything if it's included.

所以没有真正的理由使用单个“/”(U+002F)字符,但如果包含它,它不会破坏任何东西。

回答by Azeez

<img>tag is basically a Void element.

<img>tag 基本上是一个 Void 元素。

For your understanding:

您的理解:

The image does not have any content. Image tag will just give the path from where the resource will be loaded through src attribute. So, it does not require any end element.

图片没有任何内容。Image 标签将通过 src 属性给出资源加载的路径。因此,它不需要任何结束元素。

Whereas <img src="smiley.gif" alt="Smiley face" height="42" width="42"/>, this code is for XHTML version.

<img src="smiley.gif" alt="Smiley face" height="42" width="42"/>,此代码适用于 XHTML 版本。