HTML is not XML
Many people think that html is a subset of xml. And, accordingly, they write code in the same style. But this is not so, there are differences between these markups. There are some xml rules that are not applicable in html.
I will cover the three main mistakes of those who are trying to write html in xml style.
The first and most common mistake. Many times I saw someone trying to close the html tag with
/> For example,
. I will cover the three main mistakes of those who are trying to write html in xml style.
1. Self-closing tags
The first and most common mistake. Many times I saw someone trying to close the html tag with
/> For example,
And by the way, even serious companies sometimes do not write correctly.
Example
Yandex.Metrica writes its img tag like this:
/>
2. Closing tags
Well, tags need to be explicitly closed. So you should always write
I will give examples:
- Be sure to close div, span, script, table and footer;
- Tags such as option, li, tr, body can be closed or not. From the point of view of code quality, of course, it is better to always close tags, but the standard allows you not to do this;
- But some tags, such as input, br, img and hr, are forbidden to be closed. If you writeor
- it will be invalid html. These tags must be left unclosed.
Of course, these examples are not complete. For each individual tag, see the documentation.
3. Writing Boolean Attributes
How to write boolean attributes in html (such as checked and disabled)? Those who write xml-style html happen to write them like this:
This is not necessary. There is no true value in html. The standard says that if an attribute is declared in the markup, then its value is already true.
You can choose one of three recording options:
I prefer to use the short third option, like: .
PS These rules apply to html, not xhtml. However, even if your page is framed as xhtml, the browser will
parse it as html if the server submits it with mime-type 'text / html' . In order for a page to become valid xhtml, its mime-type must be 'application / xhtml + xml' . Only then will the browser parse this page according to xml rules.