问题
I have always done this <div style="clear:both;"></div>
to clear a float, but if HTML is XML, can't I just do <div style="clear:both;" />
since in XML they are the same thing?
If not, why not?
回答1:
No you can't for two reasons:
1) HTML is not XML:
The general term "HTML" actually refers to many standards, among which the older ones (pre-4.01) commend rules fundamentally incompatible with XML (e.g. <br>
is a valid HTML 4.01 tag which is self-closing but clearly does not honor the XML rule by which it should be written as <br />
).
2) XML is not HTML:
The general term "XML" refers to the standard which defines what syntax a valid XML document or snippet must follow. But syntax has in such a general definition no semantics. HTML, on the other side, also adds semantics to its tags. Because of this, the set of valid XML documents is larger than those that make sense (in respect to the semantics) in HTML (e.g. the tag <tag />
is a syntactically valid XML tag but it has no significance in HTML).
The fact that you can self-close a tag in XML does not mean you can in HTML, while whatever you can do in XHTML is legit XML.
Your assumption is a fallacy, maybe you should reflect a sec about it and see how you're trying to infer the inverse.
That said, in HTML it is specified which tags are self-closing, like img
and thelike. div
simply isn't among them and can't be self-closing.
EDIT: I try to be a bit more clear.
In logic you can write:
X is the set of all rules of XML
H is the set of all rules of XHTML
if r is a rule in H, then r is a rule in X:
r|H ===> r|X
if s is a rule in X, then s is not necessarily a rule in H:
s|X =/=> s|H
in fact, if we had s|x ===> s|H then we would have:
X === H
but we know that X =/= H
Hope this is a bit clearer to explain your fallacy.
来源:https://stackoverflow.com/questions/21613828/using-a-single-div-to-clear-a-float