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

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

问题描述

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

 < img src =smiley.gifalt =笑脸height =42width =42> 

我也注意到< img >标签在 XHTML ...

 < img src =smiley.gifalt =笑脸 height =42width =42/> 

W3School: 图片标签

解决方案

基于SGML,允许标签在特定条件下被省略。由于< img> 元素不能有任何子节点,所以它被定义为EMPTY,并且结束标签是禁止的(因为它没有任何用途)。

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



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

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

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

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

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

W3Schools: Image Tag

解决方案

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

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 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 is backwards compatible with versions of HTML that were SGML based.

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

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