html - 为什么以及如何使用 <!DOCTYPE>?

标签 html doctype

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 - 为什么以及如何使用 &lt;!DOCTYPE>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9284989/

相关文章:

html - 修复文档类型声明后,我的页面崩溃了

javascript - HTML onmousedown/onclick 显示隐藏的 HTML

jquery - 简单的 jquery 代码。我不知道我做错了什么

html - 文档类型和浏览器速度

html - 有什么理由可以证明 Apple 的 HTML5 演示使用 HTML4 文档类型?

html - 我是否需要使用 XHTML 文档类型在页面上声明 XML?

php - 网站无法在 IE8 中运行,但可以在 Google Chrome 中运行

html - 模态弹出窗口在表格的下拉菜单中不起作用

html 标签 :after content needs to have different colors in the text

javascript - 对象不支持属性或方法 'createContextualFragment'