Is writing self closing tags for elements not trad

2019-01-03 13:11发布

I have noticed jQuery (or is it Firefox) will turn some of my <span class="presentational"></span> into <span class="presentational" />

Now my question is, is this okay to write my markup like this? Will any browsers choke on it?

Personally, I think it looks cleaner to do <span class="presentational" /> if it's going to be empty.

标签: xml html5 xhtml
8条回答
神经病院院长
2楼-- · 2019-01-03 14:07

Yes. It is. It'll cause problems in certain cases for old browsers.

<script type='text/javascript' src='script.js' />

In this case, the old browser might not understand that <script> tag has ended.

查看更多
Lonely孤独者°
3楼-- · 2019-01-03 14:08

Generally it's not a problem to use shorthand for empty elements, but there are some exceptions where it can cause problems.

<script> is an important one that needs to be closed with </script> to avoid issues.

Another is <meta> which works much better with spiders written as <meta></meta> instead of <meta />

Not exactly the question, but related, in terms of formatting, versions of IE have problems with just empty elements such as <div></div> or <div />. In this case, <div>&nbsp;</div> is required to maintain the formatting.

查看更多
登录 后发表回答