why this self-closing TITLE tag breaks my web page

纵然是瞬间 提交于 2020-01-15 07:16:27

问题


I have a very simple webpage.

<html>
    <head>
        <title/>
    </head>
    <body>
    <h1>hello</h1>
    </body>
</html>

breaks my webpage ,both in Chrome and Firefox

the issue is with self-closing Title tag ,removing TITLE tag or adding a title fix the issue

<title>Test Page</title>

Whats the issues with self-closing TITLE tags , couldn't find any reference to say its invalid


回答1:


If you have a void element:

<img />
<br />

Then they have no content, because there's nowhere to put it. Images can be thought of as an empty <div> with a background image.

Compared to these elements:

<h1>Hello</h1>
<section>World</section>

Which actually contain stuff (in this case, text).

The reason that the <title/> breaks your page is because you need a title in a webpage - if you don't have one, it'll just display the URL of the page, for example:

google.com/index.html

You need to have a valid title, and <title> is not a void element. This is why it breaks. To see this, go to a HTML validation website (e.g. https://validator.w3.org) and see what it tells you.

In short - <title> is not a void element - it requires an opening and closing tag.

EDIT: Research showed me this website, which says:

Self-closing: No

So in short they're not self-closing elements. You can find a list of self-closing elements here.



来源:https://stackoverflow.com/questions/53147142/why-this-self-closing-title-tag-breaks-my-web-page

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!