'What happens if I don't put a <!DOCTYPE html> in my code? Will it make any major changes?
I'm working on several projects with HTML, and sometimes I forget to put <!DOCTYPE html>
. Will it make any big or noticeable changes?
Solution 1:[1]
The <!DOCTYPE>
declaration must be the very first thing in your 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 HTML the page is written in.
A lot of IDEs allow users to leave this out and default to a certain HTML style (possibly even inserting it automagically), but leaving it out does pose a potential threat in browser compatibility and the use of older versions of HTML.
For example: new features & tags in HTML5 such as <article>
,< footer >
, <header>
,<nav>
, <section>
may not be supported if the <!DOCTYPE>
is not declared.
Additionally, the browser may decide to automatically go into Quirks or Strict Mode.
Solution 2:[2]
In HTML documents, browsers use a DOCTYPE in the beginning of the document to decide whether to handle it in quirks mode or standards mode.
<!DOCTYPE html> // Tells the browser that we are using HTML5.
If document type is not mentioned, browser will go to Quirks mode. Quirks mode depends upon the web browser version, If is older version then this will not support HTML5 tags (Example: header tag, footer tag, section tag,...)
To see the different between the Quirks mode and Standard mode visit : https://developer.mozilla.org/en-US/docs/Web/HTML/Quirks_Mode_and_Standards_Mode
If you want to try this one use the below code with and without
<!DOCTYPE html>
in your older browser like IE 8 or earlier
`<video controls>
<source src="../videos/big_buck_bunny.mp4" type="video/mp4">
<p>Your browser does not support H.264/MP4.</p>
</video>`
//Note : In the above code src="give your local mp4 video link in your computer"
Solution 3:[3]
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.
for more details: http://www.w3.org/TR/html5/syntax.html
Solution 4:[4]
in some cases, the DOCTYPE is not the very first line in a HTML document, for example:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | |
Solution 3 | Jason Aller |
Solution 4 | minto |