什么是“< html xmlns =”http://www.w3.org/1999/xhtml">做? [英] What does "<html xmlns="http://www.w3.org/1999/xhtml">" do?
问题描述
我不敢相信我的网站发生了什么。添加此行时:
I can't believe what is happening in my website. When I add this line:
<html xmlns="http://www.w3.org/1999/xhtml">
<!DOCTYPE html>
<html>
<head>
一切正常。当我不,CSS混乱了,一切都变得不同,布局变成丑陋。
Everything works fine. And when I don't, CSS "messes" up, everything becomes different and layout becomes "ugly".
此行如何解决所有问题?
How can this line solve all the problems?!
推荐答案
通常,<!DOCTYPE>
声明用于区分版本HTMLish lanaguages(在这种情况下,HTML或XHTML)。
Usually a <!DOCTYPE>
declaration is used to distinguish between versions of HTMLish lanaguages (in this case, HTML or XHTML).
不同的标记语言会有不同的行为。我最喜欢的例子是 height:100%
。在浏览器中查看以下内容:
Different markup languages will behave differently. My favorite example is height:100%
. Look at the following in a browser:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<style type="text/css">
table { height:100%;background:yellow; }
</style>
</head>
<body>
<table>
<tbody>
<tr><td>How tall is this?</td></tr>
</tbody>
</table>
</body>
</html>
...并将其与以下内容进行比较:(注意, c><!DOCTYPE> 声明)
... and compare it to the following: (note the conspicuous lack of a <!DOCTYPE>
declaration)
<html>
<head>
<style type="text/css">
table { height:100%;background:yellow; }
</style>
</head>
<body>
<table>
<tbody>
<tr><td>How tall is this?</td></tr>
</tbody>
</table>
</body>
</html>
你会注意到表的高度是完全不同的,文档是标记的类型!
You'll notice that the height of the table is drastically different, and the only difference between the 2 documents is the type of markup!
技术上, xmlns
属性由XHTML文档的根元素使用:(根据维基百科)
That doesn't answer your question though. Technically, the xmlns
attribute is used by the root element of an XHTML document: (according to Wikipedia)
XHTML文档的根元素必须是html,并且必须包含xmlns属性将它与XHTML命名空间关联。
The root element of an XHTML document must be html, and must contain an xmlns attribute to associate it with the XHTML namespace.
你会看到,重要的是要理解XHTML不是HTML,而是 XML - 一个非常不同的生物。 (确定,一种不同的生物) xmlns
属性只是文档需要的有效XML之一。为什么?因为有人在标准上工作,所以这样说;)(你可以阅读更多关于维基百科的XML命名空间,但我忽略
You see, it's important to understand that XHTML isn't HTML but XML - a very different creature. (ok, a kind of different creature) The xmlns
attribute is just one of those things the document needs to be valid XML. Why? Because someone working on the standard said so ;) (you can read more about XML namespaces on Wikipedia but I'm omitting that info 'cause it's not actually relevant to your question!)
如果结构化文档, a href =http://stackoverflow.com/questions/5838343/what-does-html-xmlns-http-www-w3-org-1999-xhtml-do/#comment-6704060>您的评论 )
If structuring your document like so... (as you suggest in your comment)
<html xmlns="http://www.w3.org/1999/xhtml">
<!DOCTYPE html>
<html>
<head>
[...]
...正在修复您的文档,相信你不了解CSS和HTML(没有进攻!),而且事实是没有 < html xmlns =http:// www。 w3.org/1999/xhtml\">
它的行为正常,并且 < html xmlns =http://www.w3.org / 1999 / xhtml>
这不是 - 你只是这是因为你习惯于写无效的HTML,因此在 quirks模式。
... is fixing your document, it leads me to believe that you don't know that much about CSS and HTML (no offense!) and that the truth is that without <html xmlns="http://www.w3.org/1999/xhtml">
it's behaving normally and with <html xmlns="http://www.w3.org/1999/xhtml">
it's not - and you just think it is, because you're used to writing invalid HTML and thus working in quirks mode.
我提供的上述示例是同一问题的示例;大多数人认为 height:100%
应导致< table>
的高度是整个窗口, DOCTYPE
实际上是打破了他们的CSS ...但事实并非如此;相反,他们只是不明白,他们需要添加一个 html,body {height:100%; }
CSS规则来实现他们想要的效果。
The above example I provided is an example of that same problem; most people think height:100%
should result in the height of the <table>
being the whole window, and that the DOCTYPE
is actually breaking their CSS... but that's not really the case; rather, they just don't understand that they need to add a html, body { height:100%; }
CSS rule to achieve their desired effect.
这篇关于什么是“< html xmlns =”http://www.w3.org/1999/xhtml">做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!