如果我不在代码中放置 <!DOCTYPE html> 会发生什么?会不会有大的改变?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23230798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 01:36:26  来源:igfitidea点击:

What happens if I dont put a <!DOCTYPE html> in my code? Will it make any major changes?

htmldoctype

提问by Maxemoss

So im working on several projects with html, and sometimes i forget to put <!DOCTYPE html>. Will it make any big or noticable changes?

所以我用 html 处理了几个项目,有时我忘记把<!DOCTYPE html>. 它会做出任何重大或显着的变化吗?

回答by steviejay

The <!DOCTYPE>declaration must be the very first thing in your HTML document, before the tag.

<!DOCTYPE>声明必须是你的HTML文档中的第一件事,标签之前。

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.

<!DOCTYPE>声明不是一个HTML标记; 它是对 Web 浏览器的关于页面所用 HTML 版本的指令。

A lot of IDEs allow users to leave this out and default to a certain HTML style, but leaving it out does pose a potential threat in browser compatibility and the use of older versions of HTML.

许多 IDE 允许用户忽略它并默认为某种 HTML 样式,但忽略它确实会对浏览器兼容性和旧版 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.

例如:如果未声明,则可能不支持HTML5 中的新功能和标签,例如<article>< footer ><header>、。<nav><section><!DOCTYPE>

Additionally, the browser may decide to automatically go into Quirks or Strict Mode.

此外,浏览器可能会决定自动进入 Quirks 或 Strict 模式。

回答by M.chaudhry

DOCTYPEs are required for legacy reasons.

出于遗留原因,需要 DOCTYPE。

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.

在文档中包含 DOCTYPE 可确保浏览器尽最大努力遵循相关规范。

for more details: http://www.w3.org/TR/html5/syntax.html

更多详情:http: //www.w3.org/TR/html5/syntax.html

回答by Muthu Kumar

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.

在 HTML 文档中,浏览器使用文档开头的 DOCTYPE 来决定是否以 quirks 模式或标准模式处理它。

<!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,...)

如果未提及文档类型,浏览器将进入 Quirks 模式。Quirks 模式取决于网络浏览器版本,如果是旧版本,则不支持 HTML5 标签(例如:页眉标签、页脚标签、节标签等)

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

要查看 Quirks 模式和标准模式之间的区别,请访问: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

在 IE 8 或更早版本的旧浏览器中

   `<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"

//注意:在上面的代码中src="在你的电脑中给出你本地的mp4视频链接"