"<html xmlns="http://www.w3.org/1999/xhtml">" 有什么作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5838343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does "<html xmlns="http://www.w3.org/1999/xhtml">" do?
提问by good_evening
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>
Everything works fine. And when I don't, CSS "messes" up, everything becomes different and layout becomes "ugly".
一切正常。而当我不这样做时,CSS“混乱”,一切都变得不同,布局变得“丑陋”。
How can this line solve all the problems?!
这条线怎么能解决所有问题?!
回答by Richard JP Le Guen
You're mixing up HTMLwith XHTML.
Usually a <!DOCTYPE>
declaration is used to distinguish between versions of HTMLish languages (in this case, HTML or XHTML).
通常使用<!DOCTYPE>
声明来区分 HTMLish 语言的版本(在本例中为 HTML 或 XHTML)。
Different markup languages will behave differently. My favorite example is height:100%
. Look at the following in a browser:
不同的标记语言会有不同的表现。我最喜欢的例子是height:100%
。在浏览器中查看以下内容:
XHTML
XHTML
<!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>
... and compare it to the following: (note the conspicuous lack of a <!DOCTYPE>
declaration)
...并将其与以下内容进行比较:(注意明显缺少<!DOCTYPE>
声明)
HTML (quirks mode)
HTML(怪癖模式)
<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!
您会注意到表格的高度完全不同,两个文档之间的唯一区别是标记的类型!
That's nice... now, what does <html xmlns="http://www.w3.org/1999/xhtml">
do?
这很好……现在,有什么作用<html xmlns="http://www.w3.org/1999/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)
但这并不能回答你的问题。从技术上讲,该xmlns
属性由 XHTML 文档的根元素使用:(根据Wikipedia)
The root element of an XHTML document must be
html
, and must contain anxmlns
attribute to associate it with the XHTML namespace.
XHTML 文档的根元素必须是
html
,并且必须包含一个xmlns
属性以将其与 XHTML 名称空间相关联。
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 Wikipediabut I'm omitting that info 'cause it's not actually relevant to your question!)
你看,理解 XHTML 不是 HTML 而是XML- 一种非常不同的生物是很重要的。(好吧,一种不同的生物)xmlns
属性只是文档需要是有效 XML 的东西之一。为什么?因为从事标准工作的人是这样说的 ;)(您可以在 Wikipedia 上阅读有关XML 名称空间的更多信息,但我省略了该信息,因为它实际上与您的问题无关!)
But then why is <html xmlns="http://www.w3.org/1999/xhtml">
fixing the CSS?
但是为什么要<html xmlns="http://www.w3.org/1999/xhtml">
修复 CSS?
If structuring your document like so... (as you suggest in your comment)
如果像这样构建您的文档......(正如您在评论中所建议的那样)
<html xmlns="http://www.w3.org/1999/xhtml">
<!DOCTYPE html>
<html>
<head>
[...]
... 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 thinkit is, because you're used to writing invalid HTML and thus working in quirks mode.
...正在修复您的文档,这让我相信您对 CSS 和 HTML 了解不多(无意冒犯!),事实是,如果没有<html xmlns="http://www.w3.org/1999/xhtml">
它,它会正常运行,而如果使用<html xmlns="http://www.w3.org/1999/xhtml">
它,则它不会正常运行- 而您只是认为它是,因为您习惯于编写无效的 HTML 并因此在quirks mode 下工作。
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.
我提供的上述示例是同一问题的示例;大多数人认为height:100%
应该导致<table>
整个窗口的高度,并且DOCTYPE
实际上破坏了他们的CSS......但事实并非如此;相反,他们只是不明白他们需要添加一个html, body { height:100%; }
CSS 规则来达到他们想要的效果。
回答by tere?ko
Its an XML namespace. It is required when you use XHTML 1.0 or 1.1 doctypes or application/xhtml+xml mimetypes.
它是一个 XML 命名空间。当您使用 XHTML 1.0 或 1.1 doctypes 或 application/xhtml+xml mimetypes 时需要它。
You should be using HTML5 doctype, then you don't need it for text/html. Better start from template like this :
您应该使用 HTML5 doctype,那么 text/html 就不需要它了。最好从这样的模板开始:
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>domcument title</title>
<link rel="stylesheet" href="/stylesheet.css" type="text/css" />
</head>
<body>
<!-- your html content -->
<script src="/script.js"></script>
</body>
</html>
When you have put your Doctype straight - do and validate you htmland your css.
That usually will sove you layout issues.
回答by Boris Zbarsky
It sounds like your site has CSS or JS that depends on running in quirks mode. Which is why you need garbage above your doctype to render "correctly". I suggest removing said garbage and then fixing your CSS+JS to actually work in standards mode; you'll save yourself a lot of pain in the long run.
听起来您的网站有 CSS 或 JS,这取决于在 quirks 模式下运行。这就是为什么您需要文档类型上方的垃圾才能“正确”呈现。我建议删除所说的垃圾,然后修复你的 CSS+JS 以在标准模式下实际工作;从长远来看,你会为自己省去很多痛苦。
回答by nemesisfixx
The namespace name http://www.w3.org/1999/xhtml
is intended for use in various specifications such as:
Recommendations:
XHTML? 1.0: The Extensible HyperText Markup Language
XHTML Modularization
XHTML 1.1
XHTML Basic
XHTML Print
XHTML+RDFa
Check here for more detail
在这里查看更多详细信息