Possible Duplicate:
What's up, Doctype?
HTML: What is the functionality of !DOCTYPE
好吧,当我第一次学习 HTML 时,我基本上是通过反复试验来学习的,据我所知,并没有真正坐下来正确地学习它。结果,我错过了很多,因此,为了我自己的利益,我决定从基础开始。 :) 所以我在这里(重新)学习 HTML。
现在,我的问题 - <!DOCTYPE>
标签比我遇到过的任何其他 HTML 标签更让我困惑,所以我来这里是为了解决我对它的任何疑问,这样我就可以自信地使用它。 :)
首先,有人可以向我解释一下为什么需要使用 <!DOCTYPE>
在您的网页中添加标签?没有它们他们也能工作得很好,不是吗?我读到它是这样的,因此可以根据 W3C 标准对其进行验证,但这就是我所知道的。如果能提供更多细节,我们将不胜感激。 :)
其次,读完之后,我仍然对我的 <!DOCTYPE>
中到底包含了什么感到困惑。以及如何输入。例如,这是我见过的一种 DOCTYPE:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
然而,我也看到了其他变体,从我读到的内容来看,有不同的 DOCTYPES 需要验证 - 这一切都非常令人困惑,如果我要使用一个,我应该在我的网页中使用哪个?
最佳答案
W3C对此有一个很好的答案:http://www.w3.org/QA/Tips/Doctype
Why?
Why specify a doctype? Because it defines which version of (X)HTML your document is actually using, and this is a critical piece of information needed by some tools processing the document.
For example, specifying the doctype of your document allows you to use tools such as the Markup Validator to check the syntax of your (X)HTML. Such tools won't be able to work if they do not know what kind of document you are using.
But the most important thing is that with most families of browsers, a doctype declaration will make a lot of guessing unnecessary, and will thus trigger a "standard" rendering mode.
基本上,如果您忽略它,浏览器会尝试猜测要使用的渲染模式,并且可能会导致一些意外的结果。
关于html - 为什么以及如何使用 <!DOCTYPE>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9284989/