简体   繁体   English

我是否必须在带有文档类型的 VS 代码中启动 HTML 文档?

[英]Do I have to start a HTML document in VS code with doctype?

I created a file called 'index.html' on Visual Studio Code.我在 Visual Studio Code 上创建了一个名为“index.html”的文件。 I believe the application already knows the code I will be writing is 'html'.我相信应用程序已经知道我将要编写的代码是“html”。 Must I still start off my document with << DOCTYPE html> ?我还必须从<< DOCTYPE html>开始我的文档吗? Please see below请看下面

<html>
<body>

<h1>My First Heading</h1>
<p>My first paragraph.</p>

</body>
</html> ```

Thanks

You should have doctype in an HTML document.应该在 HTML 文档中有 doctype。 It is not for the code editor, but rather the browser.它不是用于代码编辑器,而是用于浏览器。


As W3Schools says:正如 W3Schools 所说:

'The declaration is not an HTML tag. '声明不是 HTML 标签。 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在 VS 代码中设置 HTML 文件的最快方法是使用 emmet 快捷键 Shift+1查看 emmet 备忘单

You don't explicitly have to add a DOCTYPE, but you should .不必明确添加 DOCTYPE,但您应该.

The DOCTYPE is not explicitly required , though the browser will go into quirks mode if the DOCTYPE is omitted. DOCTYPE不是明确要求的,但如果省略 DOCTYPE,浏览器将 go 进入怪癖模式 This means that HTML5 tags such as <article> , <header> , <nav> and <section> will fail to render correctly.这意味着<article><header><nav><section>等 HTML5 标签将无法正确呈现。 These tags are almost exclusively used for ARIA roles .这些标签几乎专门用于ARIA 角色

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.此外,如今大多数浏览器(甚至 IDE)都足够智能,可以推断出您正在使用的 DOCTYPE,并会自动为您注入它。 Still, you should not rely on this, and should endeavor to add it yourself.不过,您不应该依赖它,而应该努力自己添加它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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