Are XHTML self closing elements still valid in HTM

2019-02-18 16:06发布

I was wondering if I can write self closing elements like in XHTML in HTML5, for example, <input type="email"> can be <input type="email" />, and will it still validate? And is this the correct way to code HTML5 web pages?

标签: html5 xhtml
3条回答
仙女界的扛把子
2楼-- · 2019-02-18 16:36

HTML5 can either be coded as XHTML, or as HTML 4. It's flexible that way.

As to which is the correct way, that's a preference. I suspect that many web designers into standards are used to XHTML and will probably continue to code that way.

You can go straight to: http://html5.validator.nu/ to validate your code, or if you have the right doctype, the official W3C site will use it for you.

查看更多
对你真心纯属浪费
3楼-- · 2019-02-18 16:41

Self-closing tags may lead to some parsing errors. Look at this:

<!DOCTYPE html>
<html>
<head><title>Title</title></head>
<body>
  <div>
    <p>
      <div/>
    </p>
  </div>
</body>
</html>

While it is perfectly valid HTML4, it is invalid in HTML5. W3C validation complains about <div/>:

Self-closing syntax (/>) used on a non-void HTML element. Ignoring the slash and treating as a start tag.

If innermost self-closed div is treated as start tag, it breaks whole structure, so be careful.

查看更多
别忘想泡老子
4楼-- · 2019-02-18 16:42

Either will work, just try to be consistent.

Same goes for quoting attributes - I've read tutorials that discourage quoting one word attribute variables. I would quote them all, at least for consistency (unless you have a popular web app where every byte is precious).

查看更多
登录 后发表回答