为什么 <img>标签未在 HTML 中关闭? [英] Why is the <img> tag not closed in HTML?

查看:32
本文介绍了为什么 <img>标签未在 HTML 中关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

我还注意到 XHTML...

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

W3Schools: 图片标签

推荐答案

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

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

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

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).

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

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.

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

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

这篇关于为什么 &lt;img&gt;标签未在 HTML 中关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆