使用单个div来清除浮动 [英] Using a single div to clear a float

查看:139
本文介绍了使用单个div来清除浮动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直使用< div style =clear:both;>< / div> 来清除浮动,但如果HTML是XML,我不能只做< div style =clear:both; /> 因为在XML中它们是相同的东西?



如果不是,为什么不呢?



1)HTML不是XML:

解决方案



通用术语HTML实际上是指许多标准,其中较老的(4.01之前的版本)称赞规则与XML基本不兼容(例如< br> ; 是一个有效的HTML 4.01标签,它自动关闭,但显然不遵守它应该写为< br /> $ b 2)XML不是HTML: b
$ b

一般术语XML是指该标准定义了有效的XML文档或代码片段必须遵循的语法。但是语法在这样的一般定义中没有语义。另一方面,HTML也为其标签添加了语义。因此,有效的XML文档集合比HTML中的有意义(就语义而言)更大(例如,标签< tag /> >是一个语法上有效的XML标签,但它在HTML中没有意义)。






事实上,您可以自行关闭标签在XML中并不意味着你可以在HTML中使用,而在XHTML中可以做的任何事情都是合法的XML。



你的假设是一个谬误,也许你应该反思一下它并看看你是如何推断逆。

也就是说,在HTML中指定哪些标签是自闭的,如 img 等等。 div 根本不在其中,并且不能自动关闭。



编辑:我试着成为更清楚一点。



在逻辑上你可以写:

  X是XML 
的所有规则的集合H是XHTML的所有规则的集合

如果r是H中的规则,则r是X中的规则:

r | H ===> r | X

如果s是X中的一个规则,则s不一定是H中的规则:

s | X = / => s | H

其实,如果我们有s | x ===> s | H那么我们可以得到:

X === H

但是我们知道X = / = H

希望这可以更清楚地解释您的谬误。


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?

解决方案

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.

这篇关于使用单个div来清除浮动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆