html手册
Note: you can download a PDF / ePub / Mobi version of this book so you can read it offline.
注意:您可以下载本书的PDF / ePub / Mobi版本,以便离线阅读。
Welcome! I wrote this book to help you quickly learn HTML and get familiar with the advanced HTML topics.
欢迎! 我写这本书是为了帮助您快速学习HTML并熟悉高级HTML主题。
HTML, a shorthand for Hyper Text Markup Language, is one of the most fundamental building blocks of the Web.
HTML是超文本标记语言的缩写,是Web的最基本构建块之一。
HTML was officially born in 1993 and since then it evolved into its current state, moving from simple text documents to powering rich Web Applications.
HTML于1993年正式诞生,从那以后它演变成现在的状态,从简单的文本文档发展为强大的Web应用程序。
This handbook is aimed at a vast audience.
本手册面向广大读者。
First, the beginner. I explain HTML from zero in a succinct but comprehensive way, so you can use this book to learn HTML from the basics.
首先是初学者。 我以简洁而全面的方式从零开始解释HTML,因此您可以使用本书从基础上学习HTML。
Then, the professional. HTML is often considered like a secondary thing to learn. It might be given for granted.
然后,专业。 HTML通常被认为是次要的学习。 它可能是理所当然的。
Yet lots of things are obscure to many people. Me included. I wrote this handbook to help my understanding of the topic, because when I need to explain something, I better make sure I first know the thing inside out.
然而,许多事情对许多人来说还是晦涩难懂的。 我包括在内。 我写这本手册是为了帮助我理解该主题,因为当我需要解释某些内容时,最好确保我先从内而外地了解该内容。
Even if you don't write HTML in your day to day work, knowing how HTML works can help save you some headaches when you need to understand it from time to time, for example while tweaking a web page.
即使您在日常工作中不编写HTML,了解HTML的工作方式也可以帮助您在需要不时了解它(例如在调整网页时)时避免头痛。
You can reach me on Twitter @flaviocopes.
您可以通过Twitter @flaviocopes与我联系 。
My website is flaviocopes.com.
我的网站是flaviocopes.com 。
Preface
前言
HTML Basics
HTML基础
The document heading
文件标题
The document body
文件主体
Tags that interact with text
与文字互动的标签
Links
链接
Container tags and page structure HTML
容器标签和页面结构HTML
Forms
形式
Tables
桌子
Multimedia tags: audio and video
多媒体标签:音频和视频
iframes
iframe
Images
图片
Accessibility
辅助功能
HTML is the foundation of the marvel called the Web.
HTML是称为Web的奇迹的基础。
There is an incredible power underneath this rather simple and limited set of rules, which lets us -- developers, makers, designers, writers, and tinkerers -- craft documents, apps, and experiences for people all around the globe.
在这套相当简单且有限的规则集之下,有着不可思议的力量,它使我们(开发人员,制作人员,设计师,作家和修补匠)能够为全球各地的人们制作文档,应用程序和体验。
My first HTML book came out in 1997 and was called "HTML Unleashed". A big, lots-of-pages, long tome.
我的第一本HTML书籍于1997年出版,被称为“ HTML Unleashed”。 一个很大的页面,很长。
20+ years have passed, and HTML is still the foundation of the Web, with minimal changes from back then.
20多年过去了,HTML仍然是Web的基础,从那时起变化很小。
Sure, we got more semantic tags, presentational HTML is no longer a thing, and CSS has taken care of the design of things.
当然,我们获得了更多的语义标签,表示性HTML不再是一回事,而CSS负责了事物的设计。
HTML's success is based on one thing: simplicity.
HTML的成功基于一件事: 简单性 。
It resisted being hijacked into an XML dialect via XHTML, when eventually people realized that thing was way, way too complex.
当最终人们意识到事情太复杂了,它拒绝通过XHTML被劫持为XML方言。
It did so because of another feature it provides us: forgiveness. There are some rules, right, but after you learn those, you have a lot of freedom.
之所以这样做,是因为它为我们提供了另一个功能: 宽恕 。 有一些规则,对,但是学习这些规则之后,您将拥有很多自由。
Browsers learned to be resilient and to always try to do their best when parsing and presenting HTML to the users.
浏览器学会了适应性强,并且在解析并向用户呈现HTML时总是尽力而为。
And the whole Web platform did one thing right: it never broke backward compatibility. Pretty incredibly, we can go back to HTML documents written in 1991, and they look pretty much as they looked back then.
整个Web平台做对了一件事情:它永远不会破坏向后兼容性。 令人难以置信的是,我们可以回溯到1991年编写HTML文档,它们看上去和那时一样。
We even know what the first web page was. It's this: http://info.cern.ch/hypertext/WWW/TheProject.html
我们甚至知道第一个网页是什么。 就是这样的: http : //info.cern.ch/hypertext/WWW/TheProject.html
And you can see the source of the page, thanks to another big feature of the Web and HTML: we can inspect the HTML of any web page.
由于Web和HTML的另一个重要功能,您可以看到页面的来源: 我们可以检查任何网页HTML 。
Don't take this for granted. I don't know any other platform that gives us this ability.
不要认为这是理所当然的。 我不知道有任何其他平台可以提供这种功能。
The exceptional Developer Tools built into any browser let us inspect and take inspiration from HTML written by anyone in the world.
内置在任何浏览器中的出色开发人员工具使我们能够检查世界上任何人编写HTML并从中汲取灵感。
If you are new to HTML this book aims to help you get started. If you are a seasoned Web Developer this book will improve your knowledge.
如果您不熟悉HTML,那么本书旨在帮助您入门。 如果您是一位经验丰富的Web开发人员,这本书将提高您的知识。
I learned so much while writing it, even though I've been working with the Web for 20+ years, and I'm sure you'll find something new, too.
即使我在Web上工作了20多年,我在编写它时也学到了很多东西,而且我相信您也会发现一些新东西。
Or you'll re-learn something old you forgot.
否则,您将重新学习忘记的旧知识。
In any case, the goal of the book is to be useful to you, and I hope it succeeds.
无论如何,这本书的目标是对您有用,我希望它能成功。
HTML is a standard defined by the WHATWG, an acronym for Web Hypertext Application Technology Working Group, an organization formed by people working on the most popular web browser. This means it's basically controlled by Google, Mozilla, Apple and Microsoft.
HTML是WHATWG定义的标准, WHATWG是Web超文本应用程序技术工作组的缩写,该组织由使用最流行的Web浏览器的人们组成。 这意味着它基本上由Google,Mozilla,Apple和Microsoft控制。
In the past the W3C (World Wide Web Consortium) was the organization in charge of creating the HTML standard.
过去, W3C (万维网联盟)是负责创建HTML标准的组织。
The control informally moved from W3C to WHATWG when it became clear that the W3C push towards XHTML was not a good idea.
当很明显W3C向XHTML推进不是一个好主意时,该控件非正式地从W3C转移到WHATWG。
If you've never heard of XHTML, here's a short story. In the early 2000s, we all believed the future of the Web was XML (seriously). So HTML moved from being an SGML-based authoring language to an XML markup language.
如果您从未听说过XHTML,那么这里有个简短的故事。 在2000年代初期,我们都相信Web的未来(严重地是XML)。 因此,HTML已从基于SGML的创作语言转变为XML标记语言。
It was a big change. We had to know, and respect, more rules. Stricter rules.
这是一个很大的变化。 我们必须知道并尊重更多规则。 更严格的规则。
Eventually browser vendors realized this was not the right path for the Web, and they pushed back, creating what is now known as HTML5.
最终,浏览器供应商意识到这不是Web的正确路径,于是他们推迟了,创建了现在称为HTML5的东西。
W3C did not really agree on giving up control of HTML, and for years we had 2 competing standards, each one aiming to be the official one. Eventually on 28 May 2019 it was made official by W3C that the "true" HTML version was the one published by WHATWG.
W3C并未真正同意放弃对HTML的控制,多年来,我们有2个相互竞争的标准,每个标准都旨在成为正式标准。 最终,W3C于2019年5月28日正式宣布,“真正的” HTML版本是WHATWG发布的版本。
I mentioned HTML5. Let me explain this little story. I know, it's kind of confusing up to now, as with many things in life when many actors are involved, yet it's also fascinating.
我提到了HTML5。 让我解释一下这个小故事。 我知道,到目前为止,就像许多演员参与其中的生活中的许多事情一样,这令人感到困惑,但这也令人着迷。
We had HTML version 1 in 1993. Here's the original RFC.
我们在1993年拥有HTML版本1。 这是原始的RFC 。
HTML 2 followed in 1995.
HTML 2于1995年问世。
We got HTML 3 in January 1997, and HTML 4 in December 1997.
我们在1997年1月获得HTML 3,在1997年12月获得HTML 4 。
Busy times!
繁忙的时候!
20+ years went by, we had this entire XHTML thing, and eventually we got to this HTML5 "thing", which is not really just HTML any more.
20多年过去了,我们拥有了整个XHTML,最终我们有了这个HTML5“东西”,它不再仅仅是HTML 。
HTML5 is a term that now defines a whole set of technologies, which includes HTML but adds a lot of APIs and standards like WebGL, SVG and more.
HTML5是一个术语,现在定义了整套技术,其中包括HTML,但增加了许多API和标准,例如WebGL,SVG等。
The key thing to understand here is this: there is no such thing (any more) as an HTML version now. It's a living standard. Like CSS, which is called "3", but in reality is a bunch of independent modules developed separately. Like JavaScript, where we have one new edition each year, but nowadays, the only thing that matters is which individual features are implemented by the engine.
这里要了解的关键是:现在没有(像)HTML版本这样的东西。 这是生活水平。 类似于CSS,称为“ 3”,但实际上是一堆独立开发的独立模块。 与JavaScript一样,我们每年都有一个新版本,但是如今,唯一重要的是引擎实现了哪些单独的功能。
Yes we call it HTML5, but HTML4 is from 1997. That's a long time for anything, let alone for the web.
是的,我们称它为HTML5,但是HTML4是1997年发布的。对于任何事物来说,这都是很长的时间,更不用说对网络了。
This is where the standard now "lives": https://html.spec.whatwg.org/multipage.
这是标准现在“生效”的地方: https : //html.spec.whatwg.org/multipage 。
HTML is the markup language we use to structure content that we consume on the Web.
HTML是用于构造我们在Web上消费的内容的标记语言。
HTML is served to the browser in different ways.
HTML以不同的方式提供给浏览器。
Let's dive into this last case. Although in practice it's probably the least popular way to generate HTML, it's still essential to know the basic building blocks.
让我们深入探讨最后一种情况。 尽管实际上,它可能是生成HTML的最不流行的方法,但是了解基本的构建基块仍然至关重要。
By convention, an HTML file is saved with a .html
or .htm
extension.
按照约定,HTML文件以.html
或.htm
扩展名保存。
Inside this file, we organize the content using tags.
在此文件中,我们使用标签组织内容。
Tags wrap the content, and each tag gives a special meaning to the text it wraps.
标签包装内容,每个标签为其包装的文本赋予特殊含义。
Let's make a few examples.
让我们举几个例子。
This HTML snippet creates a paragraph using the p
tag:
这个HTML片段使用p
标签创建了一个段落:
A paragraph of text
This HTML snippet creates a list of items using the ul
tag, which means unordered list, and the li
tags, which mean list item:
该HTML代码段使用ul
标签(表示无序列表 )和li
标签(表示list item)创建项目列表 :
- First item
- Second item
- Third item
When an HTML page is served by the browser, the tags are interpreted, and the browser renders the elements according to the rules that define their visual appearance.
当浏览器为HTML页面提供服务时,将对标签进行解释,然后浏览器将根据定义其视觉外观的规则来渲染元素。
Some of those rules are built-in, such as how a list renders or how a link is underlined in blue.
其中一些规则是内置的,例如列表的呈现方式或链接的蓝色底线显示方式。
Some other rules are set by you with CSS.
其他一些规则由您使用CSS设置。
HTML is not presentational. It's not concerned with how things look. Instead, it's concerned with what things mean.
HTML不是演示性的。 它与事物的外观无关。 相反,它与事物的含义有关 。
It's up to the browser to determine how things look, with the directives defined by who builds the page, with the CSS language.
浏览器由CSS语言来决定外观,由谁来构建页面来定义指令。
Now, those two examples I made are HTML snippets taken outside of a page context.
现在,我做的两个例子是在页面上下文之外获取HTML代码段。
Let's make an example of a proper HTML page.
让我们举一个合适HTML页面的例子。
Things start with the Document Type Declaration (aka doctype), a way to tell the browser this is an HTML page, and which version of HTML we are using.
事情始于文档类型声明(又称doctype ),一种告诉浏览器这是HTML页面的方式,以及我们使用HTML版本。
Modern HTML uses this doctype:
现代HTML使用以下文档类型:
Then we have the html
element, which has an opening and closing tag:
然后,我们有了html
元素,该元素具有一个开始和结束标记:
...
Most tags come in pairs with an opening tag and a closing tag. The closing tag is written the same as the opening tag, but with a /
:
大多数标签与开始标签和结束标签成对出现。 结束标记与开始标记的书写方式相同,但带有/
:
some content
There are a few self-closing tags, which means they don't need a separate closing tag as they don't contain anything in them.
有一些自闭的标签,这意味着它们并不需要一个单独的结束标记,因为它们不包含其中的任何东西。
The html
starting tag is used at the beginning of the document, right after the document type declaration.
html
起始标记用于文档的开头,紧随文档类型声明之后。
The html
ending tag is the last thing present in an HTML document.
html
结束标记是HTML文档中的最后一件事。
Inside the html
element we have 2 elements: head
and body
:
在html
元素内部,我们有2个元素: head
和body
:
...
...
Inside head
we will have tags that are essential to creating a web page, like the title, the metadata, and internal or external CSS and JavaScript. Mostly things that do not directly appear on the page, but only help the browser (or bots like the Google search bot) display it properly.
在head
内部,我们将具有创建网页必不可少的标签,例如标题,元数据以及内部或外部CSS和JavaScript。 通常情况下,不会直接显示在页面上的东西只会帮助浏览器(或Google搜索机器人等机器人)正确显示它。
Inside body
we will have the content of the page. The visible stuff.
在body
内部,我们将获得页面的内容。 可见的东西 。
I mentioned tags and elements. What's the difference?
我提到了标签和元素。 有什么不同?
Elements have a starting tag and a closing tag. In this example, we use the p
starting and closing tags to create a p
element:
元素具有开始标签和结束标签。 在此示例中,我们使用p
开始和结束标记创建一个p
元素:
A paragraph of text
So, an element constitutes the whole package:
因此,一个元素构成了整个包 :
If an element has doesn't have a closing tag, it is only written with the starting tag, and it cannot contain any text content.
如果元素没有结束标记,则仅使用开始标记编写,并且不能包含任何文本内容。
That said, I might use the tag or element term in the book meaning the same thing, except if I explicitly mention starting tag or ending tag.
就是说,我可能会在书中使用标签或元素一词来表示相同的意思,除非我明确提到开始标签或结束标签。
The starting tag of an element can have special snippets of information we can attach, called attributes.
元素的开始标记可以包含我们可以附加的特殊信息片段,称为属性 。
Attributes have the key="value"
syntax:
属性具有key="value"
语法:
A paragraph of text
You can also use single quotes, but using double quotes in HTML is a nice convention.
您也可以使用单引号,但是在HTML中使用双引号是一个很好的约定。
We can have many of them:
我们可以有很多:
A paragraph of text
and some attributes are boolean, meaning you only need the key:
并且一些属性是布尔值,这意味着您只需要键:
The class
and id
attributes are two of the most common you will find used.
class
和id
属性是您会发现最常用的两个属性。
They have a special meaning, and they are useful both in CSS and JavaScript.
它们具有特殊的含义,在CSS和JavaScript中都非常有用。
The difference between the two is that an id
is unique in the context of a web page; it cannot be duplicated.
两者之间的区别在于, id
在网页的上下文中是唯一的; 它不能重复。
Classes, on the other hand, can appear multiple times on multiple elements.
另一方面,类可以在多个元素上多次出现。
Plus, an id
is just one value. class
can hold multiple values, separated by a space:
另外, id
只是一个值。 class
可以包含多个值,以空格分隔:
A paragraph of text
It's common to use the dash -
to separate words in a class value, but it's just a convention.
通常使用破折号-
将类值中的单词分开,但这只是一个约定。
Those are just two of the possible attributes you can have. Some attributes are only used for one tag. They are highly specialized.
这些只是您可能拥有的两个属性。 有些属性仅用于一个标签。 他们是高度专业化的。
Other attributes can be used in a more general way. You just saw id
and class
, but we have other ones too, like style
which can be used to insert inline CSS rules on an element.
其他属性可以更一般的方式使用。 您刚刚看到了id
和class
,但是我们还有其他一些,例如style
,可以用于在元素上插入内联CSS规则。
HTML is case insensitive. Tags can be written in all caps, or lowercase. In the early days, caps were the norm. Today lowercase is the norm. It is a convention.
HTML不区分大小写。 标签可以全部大写,也可以小写。 在早期,上限是常态。 今天,小写字母已成为常态。 这是一个惯例。
You usually write like this:
您通常这样写:
A paragraph of text
not like this:
不像这样:
A paragraph of text
Pretty important. In HTML, even if you add multiple white spaces into a line, it's collapsed by the browser's CSS engine.
相当重要 在HTML中,即使您在一行中添加了多个空格,浏览器CSS引擎也会将其折叠。
For example the rendering of this paragraph:
例如,本段的呈现:
A paragraph of text
is the same as this:
与此相同:
A paragraph of text
and the same as this:
与此相同:
A paragraph
of
text
> Using the white-space CSS property you can change how things behave. You can find more information on how CSS processes white space in the CSS Spec
> 使用空白CSS属性,您可以更改行为方式。 您可以在CSS Spec中找到有关CSS如何处理空白的更多信息。
I typically favor
我通常喜欢
A paragraph of text
or
要么
A paragraph of text
Nested tags should be indented with 2 or 4 characters, depending on your preference:
嵌套标签应缩进2或4个字符,具体取决于您的偏好:
A paragraph of text
- A list item
Note: this "white space is not relevant" feature means that if you want to add additional space, it can make you pretty mad. I suggest you use CSS to make more space when needed.
注意:此“空白不相关”功能意味着如果您要添加其他空间,可能会使您发疯。 我建议您在需要时使用CSS腾出更多空间。
Note: in special cases, you can use the
HTML entity (an acronym that means non-breaking space) - more on HTML entities later on. I think this should not be abused. CSS is always preferred to alter the visual presentation.
注意:在特殊情况下,您可以使用
HTML实体(缩写,表示不间断空格 )-有关HTML实体的更多信息。 我认为这不应被滥用。 始终首选使用CSS来更改视觉呈现。
The head
tag contains special tags that define the document properties.
head
标签包含定义文档属性的特殊标签。
It's always written before the body
tag, right after the opening html
tag:
它总是写在body
标签之前,紧接在html
标签之后:
...
...
We never use attributes on this tag. And we don't write content in it.
我们永远不会在此标签上使用属性。 而且我们不会在其中编写内容。
It's just a container for other tags. Inside it we can have a wide variety of tags, depending on what you need to do:
它只是其他标签的容器。 在其中,根据您需要执行的操作,我们可以使用多种标签:
title
title
script
script
noscript
noscript
link
link
style
style
base
base
meta
meta
title
标签 (The title
tag)The title
tag determines the page title. The title is displayed in the browser, and it's especially important as it's one of the key factors for Search Engine Optimization (SEO).
title
标签确定页面标题。 标题显示在浏览器中,它尤其重要,因为它是搜索引擎优化(SEO)的关键因素之一。
script
标签 (The script
tag)This tag is used to add JavaScript into the page.
此标记用于将JavaScript添加到页面中。
You can include it inline, using an opening tag, the JavaScript code and then the closing tag:
您可以使用开始标记,JavaScript代码和结束标记来内联包含它:
Or you can load an external JavaScript file by using the src
attribute:
或者,您可以使用src
属性加载外部JavaScript文件:
The type
attribute by default is set to text/javascript
, so it's completely optional.
默认情况下, type
属性设置为text/javascript
,因此它是完全可选的。
There is something pretty important to know about this tag.
关于此标签,有一些非常重要的知识。
Sometimes this tag is used at the bottom of the page, just before the closing
tag. Why? For performance reasons.
有时,在页面底部标记之前使用此标记。 为什么? 出于性能原因。
Loading scripts by default blocks the rendering of the page until the script is parsed and loaded.
默认情况下,加载脚本会阻止页面的呈现,直到脚本被解析并加载为止。
By putting it at the bottom of the page, the script is loaded and executed after the whole page is already parsed and loaded, giving a better experience to the user over keeping it in the head
tag.
通过将其放在页面底部,可以在已经解析并加载整个页面之后加载并执行脚本,与保留在head
标记中相比,可以为用户提供更好的体验。
My opinion is that this is now bad practice. Let script
live in the head
tag.
我的看法是,这现在是不好的做法。 让script
保留在head
标签中。
In modern JavaScript we have an alternative this is more performant than keeping the script at the bottom of the page -- the defer
attribute. This is an example that loads a file.js
file, relative to the current URL:
在现代JavaScript中,我们有一个替代方案,那就是比将脚本保留在页面底部( defer
属性)更有效。 这是一个相对于当前URL加载file.js
文件的示例:
This is the scenario that triggers the faster path to a fast-loading page, and fast-loading JavaScript.
这是触发快速访问快速加载页面和JavaScript的场景。
Note: the async
attribute is similar, but in my opinion a worse option than defer
. I describe why, in more detail, on page https://flaviocopes.com/javascript-async-defer/
注意: async
属性是相似的,但在我看来,比defer
更糟糕的选择。 我会在https://flaviocopes.com/javascript-async-defer/上更详细地说明为什么
noscript
标签 (The noscript
tag)This tag is used to detect when scripts are disabled in the browser.
此标记用于检测何时在浏览器中禁用了脚本。
Note: users can choose to disable JavaScript scripts in the browser settings. Or the browser might not support them by default.
注意:用户可以选择在浏览器设置中禁用JavaScript脚本。 否则,浏览器可能默认不支持它们。
It is used differently depending on whether it's put in the document head or in the document body.
根据将其放置在文档头还是文档正文中,它的用法有所不同。
We're talking about the document head now, so let's first introduce this usage.
现在我们正在谈论文档头,因此让我们首先介绍这种用法。
In this case, the noscript
tag can only contain other tags:
在这种情况下, noscript
标签只能包含其他标签:
link
tags
link
标签
style
tags
style
标签
meta
tags
meta
标记
to alter the resources served by the page, or the meta
information, if scripts are disabled.
如果禁用了脚本,则更改页面提供的资源或meta
信息。
In this example I set an element with the no-script-alert
class to display if scripts are disabled, as it was display: none
by default:
在此示例中,我使用no-script-alert
类设置了一个元素,以显示是否禁用了脚本,因为它是display: none
默认情况下, display: none
:
...
...
...
Let's solve the other case: if put in the body, it can contain content, like paragraphs and other tags, which are rendered in the UI.
让我们解决另一种情况:如果放在正文中,它可以包含在UI中呈现的内容,例如段落和其他标签。
link
标签 (The link
tag)The link
tag is used to set relationships between a document and other resources.
link
标签用于设置文档和其他资源之间的关系。
It's mainly used to link an external CSS file to be loaded.
它主要用于链接要加载的外部CSS文件。
This element has no closing tag.
该元素没有结束标签。
Usage:
用法:
...
...
...
The media
attribute allows the loading of different stylesheets depending on the device capabilities:
media
属性允许根据设备功能加载不同的样式表:
We can also link to resources other than stylesheets.
我们还可以链接到样式表以外的资源。
For example we can associate an RSS feed using
例如,我们可以使用
Or we can associate a favicon using:
或者我们可以使用以下方式关联收藏夹图标:
This tag was also used for multi-page content, to indicate the previous and next page using rel="prev"
and rel="next"
. Mostly for Google. As of 2019, Google announced it does not use this tag any more because it can find the correct page structure without it.
这个标签也用于多页内容,使用指示一个和下一个页面rel="prev"
和rel="next"
。 主要用于Google。 截至2019年, Google宣布不再使用此标签,因为没有它它可以找到正确的页面结构。
style
标签 (The style
tag)This tag can be used to add styles into the document, rather than loading an external stylesheet.
此标记可用于将样式添加到文档中,而不是加载外部样式表。
Usage:
用法:
As with the link
tag, you can use the media
attribute to use that CSS only on the specified medium:
与link
标签一样,您可以使用media
属性仅在指定的介质上使用该CSS:
base
标签 (The base
tag)This tag is used to set a base URL for all relative URLs contained in the page.
此标记用于为页面中包含的所有相对URL设置基本URL。
...
...
...
meta
标记 (The meta
tag)Meta tags perform a variety of tasks and they are very, very important.
元标记执行各种任务,它们非常非常重要。
Especially for SEO.
特别是对于SEO。
meta
elements only have the starting tag.
meta
元素仅具有开始标记。
The most basic one is the description
meta tag:
最基本的一个是description
元标记:
This might be used by Google to generate the page description in its result pages, if it finds it better describes the page than the on-page content (don't ask me how).
这可能是由谷歌被用来生成在结果页面的页面描述,如果找到更好的描述比对网页内容的页面(不要问我怎么样)。
The charset
meta tag is used to set the page character encoding. utf-8
in most cases:
charset
元标记用于设置页面字符编码。 utf-8
在大多数情况下:
The robots
meta tag instructs the Search Engine bots whether to index a page or not:
robots
元标记可指示搜索引擎robots
是否对页面建立索引:
Or if they should follow links or not:
或者他们是否应该遵循链接:
You can set nofollow on individual links, too. This is how you can set nofollow
globally.
您也可以在单个链接上设置nofollow。 这样可以全局设置nofollow
。
You can combine them:
您可以将它们组合:
The default behavior is index, follow
.
默认行为是index, follow
。
You can use other properties, including nosnippet
, noarchive
, noimageindex
and more.
您可以使用其他属性,包括nosnippet
, noarchive
, noimageindex
等。
You can also just tell Google instead of targeting all search engines:
您也可以告诉Google而不是定位所有搜索引擎:
And other search engines might have their own meta tag, too.
其他搜索引擎也可能具有自己的meta标签。
Speaking of which, we can tell Google to disable some features. This prevents the translate functionality in the search engine results:
说到这一点,我们可以告诉Google禁用某些功能。 这会阻止搜索引擎结果中的翻译功能:
The viewport
meta tag is used to tell the browser to set the page width based on the device width.
viewport
元标记用于告诉浏览器根据设备宽度设置页面宽度。
See more about this tag.
查看有关此标签的更多信息 。
Another rather popular meta tag is the http-equiv="refresh"
one. This line tells the browser to wait 3 seconds, then redirect to that other page:
另一个比较流行的meta标签是http-equiv="refresh"
标签。 此行告诉浏览器等待3秒钟,然后重定向到该其他页面:
Using 0 instead of 3 will redirect as soon as possible.
使用0而不是3将尽快重定向。
This is not a full reference; Other less-used meta tags exist.
这不是完整的参考; 存在其他较少使用的元标记。
After this document heading introduction, we can start diving into the document body.
在介绍完本文档标题之后,我们可以开始深入研究文档正文。
After the closing head tag, we can only have one thing in an HTML document: the body
element.
在关闭head标签之后,我们在HTML文档中只能有一件东西: body
元素。
...
...