I created a file called 'index.html' on Visual Studio Code. I believe the application already knows the code I will be writing is 'html'. Must I still start off my document with <<!DOCTYPE html>
? Please see below
<html>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
</html> ```
Thanks
CodePudding user response:
You must have doctype in an HTML document. It is not for the code editor, but rather the browser. Vs Code will not automatically add doctype for you.
As W3Schools says:
'The declaration is not an HTML tag. It is an "information" to the browser about what document type to expect.'
See article
The quickest way to setup an HTML file in VS code is with the emmet shortcut Shift 1 See emmet cheat sheet
CodePudding user response:
You don't explicitly have to add a DOCTYPE, but you should.
The DOCTYPE is not explicitly required, though the browser will go into quirks mode if the DOCTYPE is omitted. This means that HTML5 tags such as <article>
, <header>
, <nav>
and <section>
will fail to render correctly. These tags are almost exclusively used for ARIA roles.
Additionally, most browsers (and even IDEs) these days are smart enough to infer what DOCTYPE you are using, and will inject it automatically for you. Still, you should not rely on this, and should endeavor to add it yourself.