Things to note when writing self-closing XHTML tags

Things to note when writing self-closing XHTML tags

The img tag in XHTML should be written like this: <img alt="" src="" />. This writing method is also called self-closing, which is completely legal in XML. If you are familiar with XML-related development, you may be accustomed to this way of writing, thinking that any element in XML that does not have child nodes can be written in this way, and then any tag with no content in XHTML can also be written in this way. In theory, XHTML allows any tag to be written in a self-closing manner, but browser compatibility has brought a new problem, that is, IE cannot correctly recognize the self-closing writing method of certain tags. String 8
Please try to enter the following XHTML code and browse it in IE: <p>hello <script type="text/javascript" /> world</p>. You will find that you can only see the hello at the beginning but not the world at the end. This is really inexplicable. Many of you may have encountered this problem before and spent hours on it without finding a reasonable explanation. String 4
The explanation comes from another similar code: <p>hello <textarea /> world</p>. Can you get a reasonable explanation by looking at its display effect in IE? We can see that the hello in the front is displayed normally, while the world in the back is displayed in the textarea. This proves that IE does not correctly recognize that the textarea tag has been closed by itself, but treats it as not closed and recognizes the following content as the content inside the textarea.
Now we understand why the previous code cannot see the world behind it, because it is recognized as part of the script. This shows that when we use XHTML, we cannot use self-closing writing style as arbitrarily as XML. Only a few tags that do not need to be closed can be written in a self-closing way. For other tags, it is best to use paired closing writing style even if they do not have any content.
Finally, I need to remind everyone that in fact, stupid parsers are not only found in IE. Problems caused by imprecise parsers may occur in many places. Therefore, when writing XHTML, we still have to accommodate some habits inherited from old HTML. We cannot just write it casually as if we think it meets the standards like real XML. Don't believe it? Let’s try another one: <p>hello <br></br> world</p>, and notice how it looks in IE and Opera.
Some relevant specifications of XHTML:
1. All tags must have a corresponding end tag
2. The names of all tag elements and attributes must be lowercase
3. All XML tags must be properly nested
4. All attributes must be enclosed in quotation marks ""
5. Encode all < and & special symbols
6. Assign a value to all attributes
7. Don’t use “--” in comments

<<:  Perfect solution to the problem of not being able to access the port of the docker container under Windows 10

>>:  Some common mistakes with MySQL null

Recommend

Detailed explanation of the loading rules of the require method in node.js

Loading rules of require method Prioritize loadin...

Explanation of Dockerfile instructions and basic structure

Using Dockerfile allows users to create custom im...

MySQL infobright installation steps

Table of contents 1. Use the "rpm -ivh insta...

Summary of webpack's mobile adaptation solution

Table of contents rem vw Adapt to third-party UI ...

Randomly generate an eight-digit discount code and save it to the MySQL database

Currently, many businesses are conducting promoti...

About the selection of time date type and string type in MySQL

Table of contents 1. Usage of DATETIME and TIMEST...

Detailed explanation of vite2.0+vue3 mobile project

1. Technical points involved vite version vue3 ts...

Implementation steps for docker deployment of springboot and vue projects

Table of contents A. Docker deployment of springb...

How to quickly create tens of millions of test data in MySQL

Remark: The amount of data in this article is 1 m...

WHMCS V7.4.2 Graphical Installation Tutorial

1. Introduction WHMCS provides an all-in-one solu...

Nginx Linux installation and deployment detailed tutorial

1. Introduction to Nginx Nginx is a web server th...

Detailed explanation of how to connect Java to Mysql version 8.0.18

Regarding the connection method between Java and ...

CSS3 gradient background compatibility issues

When we make a gradient background color, we will...