HTML文档类型与内容类型有何区别?

3

请问有人能帮我理解html/xhtml页面的文档类型和内容类型之间的区别吗?

我知道文档类型用于浏览器确定选择哪种模式(标准/怪异)并遵循哪种文档类型方案被使用?

  1. 那么我们在meta标签中指定的内容类型是什么?
  2. 它与文档类型有何关系/差异?
  3. 这个内容类型在页面渲染时有什么重要性?
  4. 指定内容类型是否有助于浏览器性能?
  5. 此外,有哪些内容类型?

2
谷歌会给你更好的答案。 - Emmanuel N
4个回答

5
HTTP的Content-Type头部,而http-equiv="Content-Type"的HTML meta标签仅作为备用,表示文档的一般类型。是text/html文档并且浏览器需要启动其HTML解析器吗?还是application/pdf文档并且浏览器需要加载其PDF插件?或者是完全不同的东西?如果适用,此头部/标签还应指定文档的编码方式。
HTML文档类型规定了HTML文档的确切类型和版本。它是HTML 5文档?HTML 4 Strict?过渡性的?还是遗留的标记混乱?

2
文档类型声明主要用于:
  • 基于DTD(文档类型定义)的验证
  • 触发不同的浏览器呈现模式
HTML5文档类型声明仅用于在浏览器中触发标准模式,因为HTML5没有文档类型定义。
内容类型决定页面是作为HTML标记还是XML序列化(XHTML)标记提供。
  1. Then what is the content type we specify in meta tags?

    For HTML, it is text/html. This is the default for most Web pages.
    For XHTML, it is (usually) application/xhtml+xml.

    There's also a character encoding, typically charset=utf-8.

    This usually doesn't matter, though; in most cases the content type will be sent in the Content-Type HTTP header by the server. When a browser picks up the header it will ignore the meta tag.

    In HTML5 only the charset is specified:

    <meta charset="UTF-8">
    
  2. Does it have any relation/difference to document type?

    No.

  3. How is this content type significant when comes to page rendering?

    The same markup, assuming it is well-formed XML, can produce different DOMs in certain cases when it is served as application/xhtml+xml as opposed to text/html. These differences are minor, but may have different effects on page rendering.

    You can write markup that will produce identical DOMs whether served as HTML or XHTML. This is known as polyglot markup.

  4. Does specifying content type help the browser in terms of performance?

    No.

  5. Also, what all content types are there?

    Concerning HTML/XHTML there are only the two I've mentioned. All the other content types (or MIME types) are irrelevant here.


关于第三点:假设我按照严格模式创建了一个标记(没有任何缺失的标签或语法错误),并将内容类型设置为text/html,那么与将相同的标记保存为内容类型application/xhtml+html相比,这是否会产生不同的DOM结构? - Ivin
@goose:如果你的标记不遵循我链接中提供的多语言标记指南,那么可能会出现问题。 - BoltClock
1
如果您将XHTML作为HTML提供服务,可能会遇到一个主要问题,即如果您有自闭合的脚本或样式标签(例如<script src="script.js" type="text/javascript" />),在HTML中,/>不会结束标签,因此整个文档将被视为脚本。 - crimson_penguin

0

HTML文档类型

声明不是HTML标签,而是向Web浏览器发出的指令,告诉它页面所编写的HTML版本。

来源:w3schools

内容类型

内容属性提供与http-equiv或name属性相关联的值。

来源:w3schools


0
  1. 内容类型是发送给客户端的内容的MIME类型(例如HTML的text/html或XHTML的application/html+xml)。一般来说,内容类型对于浏览器来说就像文件扩展名对于Windows来说一样。
  2. 是的,它确实可以。两者都可以确定相同的格式(例如XHTML)。但是文档类型更具体 - 它可以包含文档使用的格式版本,例如HTML 4.01(内容类型无法告诉您这一点)。
  3. 文档类型对页面渲染有更大的影响。
  4. text/plain用于TXT文件,text/html用于HTML,application/xhtml+xml用于XHTML,text/css用于CSS等等... http://www.freeformatter.com/mime-types-list.html

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接