What are all the valid self-closing elements in XH

2018-12-31 07:02发布

What are all the valid self-closing elements (e.g. <br/>) in XHTML (as implemented by the major browsers)?

I know that XHTML technically allows any element to be self-closed, but I'm looking for a list of those elements supported by all major browsers. See http://dusan.fora.si/blog/self-closing-tags for examples of some problems caused by self-closing elements such as <div />.

13条回答
梦该遗忘
2楼-- · 2018-12-31 07:05

From the W3 Schools reference site:

<area />
<base />
<basefont />
<br />
<hr />
<input />
<img />
<link />
<meta />
查看更多
深知你不懂我心
3楼-- · 2018-12-31 07:09

What about <meta> and <link>? Why aren't they on that list?

Quick rule of thumb, do not self-close any element which is intended to have content, because it will definitely cause browser problems sooner or later.

The ones which are naturally self-closing, like <br> and <img>, should be obvious. The ones which aren't ... just don't self-close them!

查看更多
皆成旧梦
4楼-- · 2018-12-31 07:09

<hr /> is another

查看更多
唯独是你
5楼-- · 2018-12-31 07:10

Better question would be: what tags can be self-closed even in HTML mode without affecting code? Answer: only those that have empty content (are void). According to HTML specs the following elements are void:

area, base, br, col, embed, hr, img, input, keygen, link, menuitem, meta, param, source, track, wbr

Older version of specification also listed command. Besides, according to various sources the following obsolete or non-standard tags are void:

basefont, bgsound, frame, isindex

查看更多
明月照影归
6楼-- · 2018-12-31 07:10

I'm not going to try to overelaborate on this, especially since the majority of pages that I write are either generated or the tag does have content. The only two that have ever given me trouble when making them self-closing are:

<title/>

For this, I have simply resorted to always giving it a separate closing tag, since once it's up there in the <head></head> it doesn't really make your code any messier to work with anyway.

<script/>

This is the big one that I very recently ran into problems with. For years I had always used self-closing <script/> tags when the script is coming from an external source. But I very recently started recieving JavaScript error messages about a null form. After several days of research, I found that the problem was (supposedly) that the browser was never getting to the <form> tag because it didn't realize this was the end of the <script/> tag. So when I made it into separate <script></script> tags, everything worked. Why different in different pages I made on the same browser, I don't know, but it was a big relief to find the solution!

查看更多
琉璃瓶的回忆
7楼-- · 2018-12-31 07:13

Hope this helps someone:

<base />
<basefont />
<frame />
<link />
<meta />

<area />
<br />
<col />
<hr />
<img />
<input />
<param />
查看更多
登录 后发表回答