开发者

What does <!doctype html> do?

What does this tag do?

<!doctype html>

I 开发者_JAVA技巧can see that with this tag on top, my HTML behaves in another way than without.

How do I know if I need this tag?


It's an integral part of HTML as defined in the specification:

8.1.1 The DOCTYPE

A DOCTYPE is a required preamble.

DOCTYPEs are required for legacy reasons. When omitted, browsers tend to use a different rendering mode that is incompatible with some specifications. Including the DOCTYPE in a document ensures that the browser makes a best-effort attempt at following the relevant specifications.


Edit to add:

What does this seatbelt do?

What does <!doctype html> do?

What I can see is that, with this seatbelt on, my car behaves the same way as without. How do I know if I need this seatbelt?

You wont know if you'll need it until something goes wrong and you don't have it.


DOCTYPE Declaration is the abbreviation for Document Type Declaration (DTD).

The DOCTYPE Declaration (DTD or Document Type Declaration) does a couple of things:

  1. When performing HTML validation testing on a web page it tells the HTML (HyperText Markup Language) validator which version of (X)HTML standard the web page coding is supposed to comply with. When you validate your web page the HTML validator checks the coding against the applicable standard then reports which portions of the coding do not pass HTML validation (are not compliant).

  2. It tells the browser how to render the page in standards compliant mode.

If the web page coding does not include a DOCTYPE Declaration (DTD or Document Type Declaration) or it is done incorrectly:

  1. You will not be able to use a HTML (HyperText Markup Language) Validator to check the page coding. HTML validation requires the DOCTYPE declaration.

  2. The browser rendering the webpage will process the coding in Quirks Mode.

  3. The stylesheet may not be implemented as planned.

Which DOCTYPE should you use?

If you are a beginner to HTML (HyperText Markup Language) then I would suggest you use the HTML 4.01 Transitional declaration. It is much more forgiving for the beginner when performing HTML validation. You would also use this declaration if you know that your audience will not have a browser that supports CSS (Cascading Style Sheets).

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

This DOCTYPE declaration still allows elements and presentation attributes that should be in the stylesheet.


If you want to learn to code in preparation for the future but still not ready for XHTML then you would use the Strict declaration.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

With this declaration all presentation attributes are moved to the stylesheet.

A complete list of DOCTYPE Declarations (DTDs) is available at List of valid DTDs you can use in your document.


The doctype declaration should be the very first thing in an HTML document, before the tag.

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

The doctype declaration refers to a Document Type Definition (DTD). The DTD specifies the rules for the markup language, so that the browsers render the content correctly.

Quote from here: http://www.w3schools.com/tags/tag_doctype.asp

:)


A Doctype declaration triggers standards mode in your browser and should always be used. Quirks mode should always be avoided.


Browser war and two modes

To understand the significance of DOCTYPE, let us dig into some history starting from the days of browser wars. During those days web pages were written in two different versions to support both Internet Explorer and Netscape Navigator.

But then W3C took over defining Web standards. But sadly, browsers with new standards were not able to render web pages built for legacy browsers. So, to support the website built for legacy browsers quirks mode was introduced. In which the browser assumes you’ve written old-fashioned, invalid markup and code per the depressing industry norms of the late 1990s.

To differentiate those website from the new ones, DOCTYPE was added, which gave the browser a signal that this web page should be rendered in standards mode.

HTML5 and HTML 4.01

Prior to HTML5, the HTML was SGML-based which required a document type definition (DTD) to establish rules and grammar for markup. Thus, doctype had other information about the document type definition (DTD).

However, with introduction of HTML5 the DOCTYPE is the only thing needed to enable the standards mode.


To make it simple: <!DOCTYPE HTML> states that the code is HTML.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜