If according to this logic, the first half of , then won’t be parsed as >? If you think like me, you also think this syntax is stupid.
Unfortunately, the creators of the HTML4 specification didn't think so and wrote it into the specification. However, it is obvious that the browser manufacturers at the time did not care about this syntax and did not support it to a great extent. (At this point, maybe browser manufacturers have done a good thing.)
The makers of the XML (also applicable to XHTML) specification realized that this syntax was not very good, so they simply did not include the feature of tailless tags.
At the same time, it provides a relatively easy-to-understand syntax for content-free tags. The name of this syntax is "elementless tags", and it looks like this: . This syntax looked so natural that most developers at the time thought it was the correct way to write it.
Fortunately, HTML is constantly improving, and W3C members are learning from the mistakes they made in the past. That's why HTML5 has made such great progress compared to previous versions.
When introducing the new syntax of HTML5, W3C said:
The syntax of HTML5 is fully compatible with HTML4 and XHTML1, but is not compatible with those obscure HTML4 features in SGML. For example, no tail tag (
HTML5 Well done!
(I think they should keep the "short tag" feature, like Nice>, which I think is cool. But at least HTML is no longer so cluttered.)
Legality
Okay, let’s go back to the question of legality at the beginning of the article. The current HTML5 specification’s explanation of non-content tags is this:
Such tags should consist of the following components, in the order consistent with the following table:
A "<" character.
Tag name.
This item is optional, one or more attributes, each attribute must be preceded by one or more spaces.
This is optional, one or more spaces.
This item is optional, a "/" character, this item can only appear in content-less elements.
A ">" character.
The "/" character in the penultimate part is optional and has no actual meaning. So there is actually no real difference between and .
Correctness
Developers who like XML and XHTML may say, "Yes, although / is optional, is 'more correct'."
I must tell you that you are wrong. In fact, some argue that / in contentless tags is actually a tolerated grammatical error. This tolerance is based on compatibility considerations, which causes all browsers and parsers to treat and equally.
Regarding this point, Google's code style guide also clearly stipulates not to close the content-free tag.
shortcoming
Of course, there are also disadvantages to not closing contentless tags, but I don't think this can cover up its advantages: making your code clean and concise.
The first disadvantage is that the developer must know which tags have no content tags. Suppose you don't know whether is a contentless tag, then when you can't find its closing tag, you will wonder whether you should close this tag. However, there are only a few content-free tags in total, and you can usually tell at a glance whether a certain tag is a content-free tag.
The second disadvantage is that the editor may not handle unclosed contentless tags well. Editor developers must understand contentless tags and provide appropriate syntax highlighting and code completion. When you write an in the editor, the editor must know that it will never be followed by .
But these functions are very simple to implement, and the editors I know support this aspect quite well, so this is not really a disadvantage.
My thoughts on content-free tags
I think the concept of contentless tags can actually be eliminated from HTML. We can add content to these tags to replace some of its attributes.
Take the tag as an example. It has a mandatory alt attribute. This attribute exists to prevent users who cannot see images (perhaps due to physical defects or because the device they are using does not support images) Know what the image is about (if the image is just for aesthetic reasons, you really shouldn't add the alt attribute).
My question is: why not use the content of instead of the alt attribute? I think it’s more intuitive to write it this way:
Image of doge.
The tag even has an attribute called content! Why not just write the value of content directly into the content of the tag? should be written as Value content, just like
So there are only a few content-free tags that should really be retained, but the W3C must consider backward compatibility, so it is still very difficult to change the status quo.
Final Thoughts: <script> tag</h2>
<p>
This label really bothers me because its meaning is very simple, but its writing is very wordy. <script src="my-script.js"></script> This way of writing seems wrong, because the content of