简体   繁体   中英

Difference between HTML Document Type and Content type?

Could someone help me understand the difference between document type and content type of an html/xhtml page?

I know that document type is used by the browsers to determine which mode(standards/quirks) to choose and also to follow which document type schema to be used?

  1. Then what is the content type we specify in meta tags?
  2. Does it have any relation/difference to document type?
  3. How is this content type significant when comes to page rendering?
  4. Does specifying content type help the browser in terms of performance?
  5. Also, what all content types are there?

The HTTP Content-Type header, for which the http-equiv="Content-Type" HTML meta tag is a fallback only, signifies what general type of document the document is. Is it a text/html document and should the browser fire up its HTML parser? Or is it an application/pdf document and the browser needs to load its PDF plugin? Or is it something completely different? This header/tag should also specify the encoding the document is in, if applicable.

The HTML Document Type specifies the exact type and version of the HTML document. Is it an HTML 5 document? HTML 4 Strict? Transitional? Or just legacy tag soup?

The document type declaration is there mainly for

  • DTD (document type definition)-based validation
  • triggering different browser rendering modes

The HTML5 doctype declaration is solely there for triggering standards mode in browsers, since HTML5 has no document type definition.

The content type is what determines whether a page is served as HTML markup or XML-serialized (XHTML) markup.

  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.

HTML Document Type

The declaration is not an HTML tag; it is an instruction to the web browser about what version of HTML the page is written in.

Source: w3schools

While

Content Type

The content attribute gives the value associated with the http-equiv or name attribute.

Source: w3schools

  1. Content Type is a MIME type of content that is sent to a client (for example text/html for HTML or application/html+xml for XHTML). Generally speaking, Content Type is for a browser what a file extension is, let's say, for Windows.
  2. Yes, it does. Both can determine the same (XHTML, for example). But Document Type is more specific – it can contain version of the format used by the document, for example HTML 4.01 (Content Type wouldn't tell you that).
  3. Document Type is what influences page rendering more.
  4. text/plain for TXT files, text/html for HTML, application/xhtml+xml for XHTML, text/css for CSS, and so on... http://www.freeformatter.com/mime-types-list.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM