简体   繁体   English

我需要总是指定开口吗<html> HTML5 中的标签?

[英]Do I need to always specify the opening <html> tag in HTML5?

First Form:-第一种形式:-

<!DOCTYPE html>
 <head>
     <title>Untitled</title>
     <meta charset="UTF-8"/>
     <link rel="stylesheet" href="" type="text/css"/>
 </head>
     <body>

     </body>
 </html>

Second Form:-第二种形式:-

<!DOCTYPE html>
 <html>
 <head>
     <title>Untitled</title>
     <meta charset="UTF-8"/>
     <link rel="stylesheet" href="" type="text/css"/>
 </head>
     <body>

     </body>
 </html>

As I know including <!DOCTYPE html> (if using HTML5) before <html> tag is mandatory, otherwise I may face behaviour issues in my website.据我所知,在<html>标记之前包含<!DOCTYPE html> (如果使用 HTML5)是强制性的,否则我可能会在我的网站中遇到行为问题。 (I was told here ) (我在这里被告知)

In the first one, there is no start of </html> tag, but I have included it in the second one.在第一个中,没有</html>标记的开始,但我已将其包含在第二个中。

I have noticed some of the websites use one of these complexion.我注意到一些网站使用其中一种肤色。 It maybe with <html> or non <html> !它可能带有<html>或非<html> Even a HTML editor Android app (WebMaster) uses the first one (with no <html> )!即使是 HTML 编辑器 Android 应用程序(WebMaster)也使用第一个(没有<html> )!

I can't understand, is the <!DOCTYPE html> exact same as <html> , just attached with !DOCTYPE , or both are different!我无法理解,是<!DOCTYPE html><html>完全相同,只是附加了!DOCTYPE ,或者两者都不同!

So, which one is correct?那么,哪一个是正确的呢? And why that one is correct?为什么那个是正确的?

The Doctype is mandatory and completely different to the html start tag. Doctype 是强制性的,与html开始标签完全不同。

(The Doctype has html in it because XML and SGML doctypes must specify the tag name of the root element, and the HTML 5 Doctype is designed to be backwards compatible with browsers that expected one). (Doctype 中包含html ,因为 XML 和 SGML doctypes 必须指定根元素的标记名称,而 HTML 5 Doctype 旨在向后兼容预期的浏览器)。


The start and end tags for the html , head , and body elements are optional. htmlheadbody元素的开始和结束标记是可选的。

For example, the HTML specification has this to say about the html element :例如,HTML 规范html元素是这样说

Tag omission in text/html: text/html 中的标记遗漏:

  • An html element's start tag can be omitted if the first thing inside the html element is not a comment.如果 html 元素中的第一件事不是注释,则可以省略 html 元素的开始标记。
  • An html element's end tag can be omitted if the html element is not immediately followed by a comment.如果 html 元素后面没有紧跟注释,则可以省略 html 元素的结束标记。

You'll find similar rules for head and body (as well as some other elements).您会发现headbody (以及其他一些元素)有类似的规则。

This is true for every version of HTML (but not for XHTML which doesn't allow any tag to be omitted).这适用于每个版本的 HTML(但不适用于不允许省略任何标记的 XHTML)。

You only need to include the start tag if you want to include an attribute on it.如果要在其上包含属性,则只需要包含开始标记。 In general, it is a good thing to state the language your document is written in so best practise is to have an explicit html start tag with a lang attribute.一般来说,最好说明您的文档所用的语言,因此最佳实践是使用带有lang属性的显式html开始标记。

Technically, in HTML5 only !DOCTYPE and title tags are mandatory.从技术上讲,在 HTML5 中,只有!DOCTYPEtitle标签是强制性的。 All other tags are optional and only for the developer convenience and readability.所有其他标签都是可选的,只是为了开发人员的方便和可读性。

For more, you can read Google's Styleguide for HTML & CSS: https://google.github.io/styleguide/htmlcssguide.html#Optional_Tags有关更多信息,您可以阅读 Google 的 HTML 和 CSS 样式指南: https ://google.github.io/styleguide/htmlcssguide.html#Optional_Tags

To elaborate on Quentin's and Maciek's answers, <!DOCTYPE html> declares an HTML file or stream as having the html document type and top-level element to SGML parsers.为了详细说明 Quentin 和 Maciek 的答案, <!DOCTYPE html>将 HTML 文件或流声明为具有 SGML 解析器的html文档类型和顶级元素。 SGML (Standard Generalized Markup Language) is the generalization and origin of HTML, XML, and other markup languages. SGML(Standard Generalized Markup Language)是 HTML、XML 和其他标记语言的概括和起源。 HTML5 and later don't anymore reference the SGML standard (ISO 8879) to describe validity and parsing rules for HTML. HTML5 及更高版本不再参考 SGML 标准 (ISO 8879) 来描述 HTML 的有效性和解析规则。 Nevertheless, HTML documents are in fact SGML documents and can be fully parsed using SGML, including W3C HTML5, W3C HTML5.1, WHATWG HTML, and all other versions of HTML.尽管如此,HTML 文档实际上是 SGML 文档,可以使用 SGML 完全解析,包括 W3C HTML5、W3C HTML5.1、WHATWG HTML 和所有其他版本的 HTML。

The SGML standard also defines the rules for tag omission, or rather describes the rules for inferring tags that aren't actually there in an HTML document, and treat the document as if they were. SGML 标准还定义了标记省略的规则,或者更确切地说,描述了推断 HTML 文档中实际上不存在的标记的规则,并将文档视为它们存在。 SGML being merely a language framework for describing concrete markup languages such as HTML, the rules for tag omission must be told to SGML as SGML has no build-in, hardcoded knowledge about HTML. SGML 仅仅是描述具体标记语言(如 HTML)的语言框架,必须将标记省略规则告知 SGML,因为 SGML 没有内置的、硬编码的 HTML 知识。 This is a way to tell SGML that the html start- and end-element can be omitted in a document (note the additional SGML declaration syntax is not supported by web browsers directly!):这是一种方法来告诉SGML的html开始和结束元素可以在文档中被忽略(注意额外的SGML声明语法不是由网页浏览器直接支持!):

<!DOCTYPE html [
    <!ELEMENT html O O (head,body?)>
    <!-- other declarations for
         the head and body element
         following here ... -->
]>

This declaration for the html element has the OO (double letter O for O mission), with space between) tag omission indicators telling SGML that the start- and the end-element tags can be omitted, respectively. html元素的这个声明有OO (双字母 O 表示O任务),中间有空格)标记省略指示符告诉 SGML 可以分别省略开始和结束元素标记。 If a - hyphen had been used in place of O as either the start- or end-tag omission indicator, or both, the respective tag is made required instead.如果使用-连字符代替O作为开始或结束标签省略指示符,或两者兼而有之,则需要相应的标签。

There are additional HTML elements which admit tag inference stated in the various HTML5 specifications.还有其他 HTML 元素允许在各种 HTML5 规范中说明的标签推断。 I've created a transcription of these and other grammar rules into SGML for modern HTML at my project page for it ( 2 ).我已经在我的项目页面 ( 2 ) 中为现代 HTML 创建了这些和其他语法规则的转录。

For example, in the above declaration for the html element, the (head,body?) part declares that the child content of html must be a head element, followed by an optional body element.例如,在上面对html元素的声明中, (head,body?)部分声明html的子内容必须是一个head元素,后跟一个可选的body元素。 Since these, too, are declared optional (though not in the above snippet for space reasons), in combination with further declarations for title , a complete and valid HTML document can look like this由于这些也被声明为可选的(尽管出于空间原因不在上面的代码段中),结合title进一步声明,完整且有效的 HTML 文档可能如下所示

<!DOCTYPE html>
<title>Hello</title>
<p>Body Text

How this is parsed using SGML is described in my linked project page ( 3 ), as well as in the slides/talk I gave about it ( 1 ).我的链接项目页面 ( 3 ) 以及我给出的幻灯片/演讲 ( 1 ) 中描述了如何使用 SGML 对其进行解析。

You will only need the first line TAG and a TITLE tag.您只需要第一行 TAG 和 TITLE 标签。 Head tag and everything else on the page is optional, technically speaking从技术上讲,头部标签和页面上的其他所有内容都是可选的

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; <!DOCTYPE>声明不是 HTML 标签; it is an instruction to the web browser about what version of HTML the page is written in.它是对 Web 浏览器的关于页面所用 HTML 版本的指令。

eg例如

HTML 5 <!DOCTYPE html> tell the browser that this is a html document, so render it out. HTML 5 <!DOCTYPE html>告诉浏览器这是一个 html 文档,所以渲染它。

But in HTML 4, it was done like so: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">但是在 HTML 4 中,它是这样完成的: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

Usual ... are rendered.通常......被渲染。 This is strictly not required but a good practice because we don't always user plain out HTML.这绝对不是必需的,但这是一个很好的做法,因为我们并不总是用户使用简单的 HTML。 With knowledge of other web programming languages, you will see multiple languages being used in one file.了解其他 Web 编程语言后,您将看到在一个文件中使用了多种语言。 So it's useful for that.所以它很有用。

There are many other reasons but I won't explain them here.还有很多其他原因,但我不会在这里解释它们。

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

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