html手册_HTML手册

html手册

Note: you can download a PDF / ePub / Mobi version of this book so you can read it offline.

注意:您可以下载本书的PDF / ePub / Mobi版本,以便离线阅读。

html手册_HTML手册_第1张图片

介绍 (Introduction)

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 。

图书索引 (Book Index)

  • 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

    辅助功能

前言 (PREFACE)

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基础 (HTML BASICS)

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以不同的方式提供给浏览器。

  • It can be generated by a server-side application that builds it depending on the request or the session data, for example a Rails or Laravel or Django application.

    它可以由根据请求或会话数据构建它的服务器端应用程序生成,例如Rails或Laravel或Django应用程序。
  • It can be generated by a JavaScript client-side application that generates HTML on the fly.

    它可以由动态生成HTMLJavaScript客户端应用程序生成。
  • In the simplest case, it can be stored in a file and served to the browser by a Web server.

    在最简单的情况下,它可以存储在文件中,并由Web服务器提供给浏览器。

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 litags, 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代码段。

HTML页面结构 (HTML page structure)

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个元素: headbody



    
    ...
    
    
    ...
    

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内部,我们将获得页面的内容。 可见的东西

标签与元素 (Tags vs elements)

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:

因此,一个元素构成了整个

  • starting tag

    起始标签
  • text content (and possibly other elements)

    文字内容(可能还有其他元素)
  • closing tag

    结束标签

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.

就是说,我可能会在书中使用标签或元素一词来表示相同的意思,除非我明确提到开始标签或结束标签。

属性 (Attributes)

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.

classid属性是您会发现最常用的两个属性。

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.

其他属性可以更一般的方式使用。 您刚刚看到了idclass ,但是我们还有其他一些,例如style ,可以用于在元素上插入内联CSS规则。

不区分大小写 (Case insensitive)

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

空格 (White space)

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 DOCUMENT HEADING)

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中呈现的内容,例如段落和其他标签。

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.

您可以使用其他属性,包括nosnippetnoarchivenoimageindex等。

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.

在介绍完本文档标题之后,我们可以开始深入研究文档正文。

文件主体 (THE DOCUMENT BODY)

After the closing head tag, we can only have one thing in an HTML document: the body element.

在关闭head标签之后,我们在HTML文档中只能有一件东西: body元素。



    
        ...
    
    
        ...
    

Just like the head and html tags, we can only have one body tag in one page.

就像headhtml标签一样,我们在一页中只能有一个body标签。

Inside the body tag we have all the tags that define the content of the page.

body标签内,我们具有定义页面内容的所有标签。

Technically, the start and ending tags are optional. But I consider it a good practice to add them. Just for clarity.

从技术上讲,开始和结束标记是可选的。 但是我认为添加它们是一个好习惯。 为了清楚起见。

In the next chapters we'll define the variety of tags you can use inside the page body.

在下一章中,我们将定义可在页面正文中使用的各种标签。

But before, we must introduce a difference between block elements and inline elements.

但是,在此之前,我们必须在块元素和内联元素之间引入区别。

块元素与内联元素 (Block elements vs inline elements)

Visual elements, the ones defined in the page body, can be generally classified in 2 categories:

在页面正文中定义的视觉元素通常可以分为两类:

  • block elements (p, div, heading elements, lists and list items, ...)

    块元素( pdiv ,标题元素,列表和列表项,...)

  • inline elements (a, span, img, ...)

    内联元素( aspanimg ,...)

What is the difference?

有什么区别?

Block elements, when positioned in the page, do not allow other elements next to them. To the left, or to the right.

块元素位于页面中时,不允许其旁边有其他元素。 在左边或右边。

Inline elements instead can sit next to other inline elements.

相反,内联元素可以坐在其他内联元素旁边。

The difference also lies in the visual properties we can edit using CSS. We can alter the width/height, margin, padding and border of block elements. We can't do that for inline elements.

区别还在于我们可以使用CSS编辑的视觉属性。 我们可以更改块元素的宽度/高度,边距,填充和边框。 我们不能对内联元素执行此操作。

Note that using CSS we can change the default for each element, setting a p tag to be inline, for example, or a span to be a block element.

请注意,使用CSS我们可以更改每个元素的默认设置,例如将p标签设置为内联,或将span设置为block元素。

Another difference is that inline elements can be contained in block elements. The reverse is not true.

另一个区别是内联元素可以包含在块元素中。 反之则不成立。

Some block elements can contain other block elements, but it depends. The p tag for example does not allow such option.

某些块元素可以包含其他块元素,但这要视情况而定。 例如, p标签不允许这种选项。

与文字互动的标签 (TAGS THAT INTERACT WITH TEXT)

p标签 (The p tag)

This tag defines a paragraph of text.

该标签定义了一段文本。

Some text

It's a block element.

这是一个块元素。

Inside it, we can add any inline element we like, like span or a.

在其中,我们可以添加任何喜欢的内联元素,例如spana

We cannot add block elements.

我们不能添加块元素。

We cannot nest a p element into another one.

我们不能将p元素嵌套到另一个元素中。

By default browsers style a paragraph with a margin on top and at the bottom. 16px in Chrome, but the exact value might vary between browsers.

默认情况下,浏览器为段落设置样式,在顶部和底部留有空白。 在Chrome中为16px ,但实际值可能因浏览器而异。

This causes two consecutive paragraphs to be spaced, replicating what we think of a "paragraph" in printed text.

这将导致两个连续的段落被隔开,从而复制了我们在打印文本中所认为的“段落”。

span标签 (The span tag)

This is an inline tag that can be used to create a section in a paragraph that can be targeted using CSS:

这是一个内联标签,可用于在可使用CSS定位的段落中创建一个部分:

A part of the text and here another part

br标签 (The br tag)

This tag represents a line break. It's an inline element, and does not need a closing tag.

此标记表示换行符。 这是一个内联元素,不需要结束标记。

We use it to create a new line inside a p tag, without creating a new paragraph.

我们使用它在p标记内创建新行,而无需创建新段落。

And compared to creating a new paragraph, it does not add additional spacing.

与创建新段落相比,它不会增加额外的间距。

Some text
A new line

标题标签 (The heading tags)

HTML provides us 6 heading tags. From most important to least important, we have h1, h2, h3, h4, h5, h6.

HTML为我们提供了6个标题标签。 从最重要到最不重要,我们有h1h2h3h4h5h6

Typically a page will have one h1 element, which is the page title. Then you might have one or more h2 elements depending on the page content.

通常,页面将具有一个h1元素,即页面标题。 然后,根据页面内容,您可能会有一个或多个h2元素。

Headings, especially the heading organization, are also essential for SEO, and search engines use them in various ways.

标题,尤其是标题组织,对于SEO也是必不可少的,搜索引擎以各种方式使用它们。

The browser by default will render the h1 tag bigger, and will make the elements size smaller as the number near h increases:

默认情况下,浏览器将使h1标签更大,并且随着h附近的数字增加,元素的大小将减小:

html手册_HTML手册_第2张图片

All headings are block elements. They cannot contain other elements, just text.

所有标题都是块元素。 它们不能包含其他元素,而只能是文本。

strong标签 (The strong tag)

This tag is used to mark the text inside it as strong. This is pretty important, it's not a visual hint, but a semantic hint. Depending on the medium used, its interpretation will vary.

此标记用于将其中的文本标记为 。 这非常重要,它不是视觉提示,而是语义提示。 根据使用的介质,其解释会有所不同。

Browsers by default make the text in this tag bold.

默认情况下,浏览器使此标记中的文本变为粗体

em标签 (The em tag)

This tag is used to mark the text inside it as emphasized. Like with strong, it's not a visual hint but a semantic hint.

此标记用于将其中的文本标记为强调 。 像strong一样,它不是视觉提示,而是语义提示。

Browsers by default make the text in this italic.

默认情况下,浏览器使用斜体显示文本。

行情 (Quotes)

The blockquote HTML tag is useful to insert citations in the text.

blockquote HTML标记可用于在文本中插入引文。

Browsers by default apply a margin to the blockquote element. Chrome applies a 40px left and right margin, and a 10px top and bottom margin.

默认情况下,浏览器对blockquote元素应用边距。 Chrome会应用40px左右边距,以及10px上下边距。

The q HTML tag is used for inline quotes.

q HTML标记用于内联引号。

水平线 (Horizontal line)

Not really based on text, but the hr tag is often used inside a page. It means horizontal rule, and it adds a horizontal line in the page.

并非完全基于文本,但是hr标签通常在页面内使用。 它表示horizontal rule ,并在页面中添加一条水平线。

Useful to separate sections in the page.

有助于分隔页面中的各个部分。

代码块 (Code blocks)

The code tag is especially useful to show code, because browsers give it a monospaced font.

code标签对于显示代码特别有用,因为浏览器为它提供了等宽字体。

That's typically the only thing that browsers do. This is the CSS applied by Chrome:

这通常是浏览器唯一要做的。 这是Chrome应用CSS:

code {
    font-family: monospace;
}

This tag is typically wrapped in a pre tag, because the code element ignores whitespace and line breaks. Like the p tag.

此标记通常包装在pre标记中,因为code元素会忽略空格和换行符。 像p标签一样。

Chrome gives pre this default styling:

Chrome会pre此默认样式:

pre {
    display: block;
    font-family: monospace;
    white-space: pre;
    margin: 1em 0px;
}

which prevents white space collapsing and makes it a block element.

这样可以防止空格崩溃并使其成为块元素。

清单 (Lists)

We have 3 types of lists:

我们有3种类型的列表:

  • unordered lists

    无序列表
  • ordered lists

    有序列表
  • definition lists

    定义清单

Unordered lists are created using the ul tag. Each item in the list is created with the li tag:

使用ul标签创建无序列表。 列表中的每个项目都是使用li标签创建的:

  • First
  • Second

Ordered lists are similar, just made with the ol tag:

有序列表是相似的,只是用ol标签制成:

  1. First
  2. Second

The difference between the two is that ordered lists have a number before each item:

两者之间的区别在于,有序列表在每个项目之前都有一个数字:

html手册_HTML手册_第3张图片

Definition lists are a bit different. You have a term, and its definition:

定义列表有些不同。 您有一个术语及其定义:

Flavio
The name
Copes
The surname

This is how browsers typically render them:

这是浏览器通常呈现它们的方式:

html手册_HTML手册_第4张图片

I must say you rarely see them in the wild, for sure not much as ul and ol, but sometimes they might be useful.

我必须说,您很少在野外看到它们,肯定不会像ulol那样多,但是有时它们可​​能有用。

其他文字标签 (Other text tags)

There is a number of tags with presentational purposes:

有许多具有演示目的的标签:

  • the mark tag

    mark标签

  • the ins tag

    ins标签

  • the del tag

    del标签

  • the sup tag

    sup标签

  • the sub tag

    sub标签

  • the small tag

    small标签

  • the i tag

    i标签

  • the b tag

    b标签

This is an example of the visual rendering of them which is applied by default by browsers:

这是它们的视觉渲染的示例,默认情况下浏览器会应用它们:

html手册_HTML手册_第5张图片

You might wonder, how is b different than strong? And how i is different than em?

您可能想知道, bstrong什么不同? 怎么i比不同的em

The difference lies in the semantic meaning. While b and i are a direct hint at the browser to make a piece of text bold or italic, strong and em give the text a special meaning, and it's up to the browser to give the styling. Which happens to be exactly the same as b and i, by default. Although you can change that using CSS.

区别在于语义上。 bi是浏览器的直接提示,使一段文本变为粗体或斜体, strongem赋予该文本特殊的含义,这取决于浏览器的样式。 默认情况下,它恰好与bi完全相同。 尽管您可以使用CSS进行更改。

There are a number of other, less used tags related to text. I just mentioned the ones that I see used the most.

还有许多其他与文本相关的较少使用的标签。 我刚刚提到了使用最多的那些。

链接 (LINKS)

Links are defined using the a tag. The link destination is set via its href attribute.

链接是使用a标签定义的。 链接目标是通过其href属性设置的。

Example:

例:

click here

Between the starting and closing tag we have the link text.

在开始标记和结束标记之间,我们有链接文本。

The above example is an absolute URL. Links also work with relative URLs:

上面的示例是绝对URL。 链接也可以使用相对URL:

click here

In this case, when clicking the link the user is moved to the /test URL on the current origin.

在这种情况下,单击链接时,用户将移动到当前来源的/test URL。

Be careful with the / character. If omitted, instead of starting from the origin, the browser will just add the test string to the current URL.

请注意/字符。 如果省略,则浏览器将代替将test字符串添加到当前URL,而不是从起点开始。

Example, I'm on the page https://flaviocopes.com/axios/ and I have these links:

例如,我在页面https://flaviocopes.com/axios/ ,我具有以下链接:

  • /test once clicked brings me to https://flaviocopes.com/test

    /test一次点击将我带到https://flaviocopes.com/test

  • test once clicked brings me to https://flaviocopes.com/axios/test

    单击一次test即可将我带到https://flaviocopes.com/axios/test

Link tags can include other things inside them, not just text. For example, images:

链接标签可以在其中包含其他内容,而不仅仅是文本。 例如,图像:


    

or any other elements, except other tags.

或其他任何元素,但其他标记除外。

If you want to open the link in a new tab, you can use the target attribute:

如果要在新选项卡中打开链接,则可以使用target属性:

open in new tab

容器标签和页面结构HTML (CONTAINER TAGS AND PAGE STRUCTURE HTML)

容器标签 (Container tags)

HTML provides a set of container tags. Those tags can contain an unspecified set of other tags.

HTML提供了一组容器标记。 这些标签可以包含一组未指定的其他标签。

We have:

我们有:

  • article

    article

  • section

    section

  • div

    div

and it can be confusing to understand the difference between them.

理解它们之间的差异可能会造成混淆。

Let's see when to use each one of them.

让我们看看何时使用它们中的每一个。

article (article)

The article tag identifies a thing that can be independent from other things in a page.

商品标签标识,可以是独立于页面其他东西事情

For example a list of blog posts in the homepage.

例如,主页中的博客文章列表。

Or a list of links.

或链接列表。

We're not limited to lists: an article can be the main element in a page.

我们不仅限于列表:文章可以是页面中的主要元素。

A blog post

Here is the content...

Inside an article tag we should have a title (h1-h6) and

内部article标签,我们应该有一个标题( h1 - h6 )和

section (section)

Represents a section of a document. Each section has a heading tag (h1-h6), then the section body.

代表文档的一部分。 每个节都有一个标题标签( h1 - h6 ),然后是节正文

Example:

例:

A section of the page

...

It's useful to break a long article into different sections.

将较长的文章分成不同的部分很有用。

Shouldn't be used as a generic container element. div is made for this.

不应用作通用容器元素。 div是为此而设计的。

div (div)

div is the generic container element:

div是通用容器元素:

...

You often add a class or id attribute to this element, to allow it to be styled using CSS.

您经常向该元素添加classid属性,以允许使用CSS设置其样式。

We use div in any place where we need a container but the existing tags are not suited.

我们在需要容器的任何地方使用div ,但现有标签不合适。

This tag is used to create the markup that defines the page navigation. Into this we typically add an ulor ol list:

此标记用于创建定义页面导航的标记。 通常,向其中添加一个ulol列表:

aside (aside)

The aside tag is used to add a piece of content that is related to the main content.

aside标签用于添加与主要内容相关的内容。

A box where to add a quote, for example. Or a sidebar.

例如,在其中添加引号的框。 或侧边栏。

Example:

例:

some text..

other text...

Using aside is a signal that the things it contains are not part of the regular flow of the section it lives into.

使用aside表示它包含的内容不是它所在的部分的常规流程的一部分。

The header tag represents a part of the page that is the introduction. It can for example contain one or more heading tag (h1-h6), the tagline for the article, an image.

header标记表示页面的一部分,即简介。 例如,它可以包含一个或多个标题标签( h1 - h6 ),商品的标语,图像。

Article title

...

main (main)

The main tag represents the main part of a page:

main标签代表页面的主要部分:


  ....
  

....

The footer tag is used to determine the footer of an article, or the footer of the page:

footer标记用于确定文章的页脚或页面的页脚:

....

Footer notes..

形式 (FORMS)

Forms are the way you can interact with a page, or an app, built with Web technologies.

表单是您与使用Web技术构建的页面或应用程序进行交互的方式。

You have a set of controls, and when you submit the form, either with a click to a "submit" button or programmatically, the browser will send the data to the server.

您具有一组控件,并且在提交表单时(单击“提交”按钮或以编程方式单击),浏览器会将数据发送到服务器。

By default this data sending causes the page to reload after the data is sent, but using JavaScript you can alter this behavior (not going to explain how in this book).

默认情况下,此数据发送导致页面在发送数据后重新加载,但是使用JavaScript可以更改此行为(在本书中不做解释)。

A form is created using the form tag:

使用form标记创建一个form

...

By default forms are submitted using the GET HTTP method. Which has its drawbacks, and usually you want to use POST.

默认情况下,表单是使用GET HTTP方法提交的。 这有其缺点,通常您想使用POST。

You can set the form to use POST when submitted by using the method attribute:

您可以使用method属性将表单设置为在提交时使用POST:

...

The form is submitted, either using GET or POST, to the same URL where it resides.

使用GET或POST将表单提交到其驻留的相同URL。

So if the form is in the https://flaviocopes.com/contacts page, pressing the "submit" button will make a request to that same URL.

因此,如果表单位于https://flaviocopes.com/contacts页面中,则按“提交”按钮将向该URL发出请求。

Which might result in nothing happening.

可能什么也不会发生。

You need something server-side to handle the request, and typically you "listen" for those form submit events on a dedicated URL.

您需要服务器端来处理请求,通常您会“监听”专用URL上的那些表单提交事件。

You can specify the URL via the action parameter:

您可以通过action参数指定URL:

...

This will cause the browser to submit the form data using POST to the /new-contact URL on the same origin.

这将导致浏览器使用POST将表单数据提交到同一来源的/new-contact URL。

If the origin (protocol + domain + port) is https://flaviocopes.com (port 80 is the default), this means the form data will be sent to https://flaviocopes.com/new-contact.

如果来源(协议+域+端口)为https://flaviocopes.com (默认端口80),则意味着表单数据将发送到https://flaviocopes.com/new-contact

I talked about data. Which data?

我谈到了数据。 哪些数据?

Data is provided by users via the set of controls that are available on the Web platform:

用户通过Web平台上可用的一组控件提供数据:

  • input boxes (single line text)

    输入框(单行文本)
  • text areas (multiline text)

    文字区域(多行文字)
  • select boxes (choose one option from a drop-down menu)

    选择框(从下拉菜单中选择一个选项)
  • radio buttons (choose one option from a list always visible)

    单选按钮(从始终可见的列表中选择一个选项)
  • checkboxes (choose zero, one or more option)

    复选框(选择零,一个或多个选项)
  • file uploads

    文件上传
  • and more!

    和更多!

Let's introduce each one of them in the following form fields overview.

让我们在以下表单字段概述中介绍其中的每一个。

input标签 (The input tag)

The input field is one of the most widely used form elements. It's also a very versatile element, and it can completely change behavior based on the type attribute.

input字段是使用最广泛的表单元素之一。 这也是一个非常通用的元素,它可以根据type属性完全更改行为。

The default behavior is to be a single-line text input control:

默认行为是单行文本输入控件:

Equivalent to using:

等效于使用:

As with all the other fields that follow, you need to give the field a name in order for its content to be sent to the server when the form is submitted:

与后面的所有其他字段一样,您需要给该字段命名,以便在提交表单时将其内容发送到服务器:

The placeholder attribute is used to have some text showing up, in light gray, when the field is empty. Useful to add a hint to the user for what to type in:

当字段为空时, placeholder属性用于以浅灰色显示一些文本。 有助于向用户提示输入内容:

电子邮件 (Email)

Using type="email" will validate client-side (in the browser) an email for correctness (semantic correctness, not ensuring the email address is existing) before submitting.

使用type="email"将在提交之前验证客户端(在浏览器中)电子邮件的正确性(语义正确性,不能确保电子邮件地址存在)。

密码 (Password)

Using type="password" will make every key entered appear as an asterisk (*) or dot, useful for fields that host a password.

使用type="password"将使输入的每个键都显示为星号(*)或点,这对于承载密码的字段很有用。

号码 (Numbers)

You can have an input element accept only numbers:

您可以让输入元素仅接受数字:

You can specify a minimum and maximum value accepted:

您可以指定可接受的最小值和最大值:

The step attribute helps identify the steps between different values. For example this accepts a value between 10 and 50, at steps of 5:

step属性有助于识别不同值之间的步骤。 例如,以5的步长接受10到50之间的值:

隐藏的领域 (Hidden field)

Fields can be hidden from the user. They will still be sent to the server upon the form submit:

可以向用户隐藏字段。 提交表单后,它们仍将发送到服务器:

This is commonly used to store values like a CSRF token, used for security and user identification, or even to detect robots sending spam, using special techniques.

它通常用于存储诸如CSRF令牌之类的值,用于安全性和用户标识,甚至使用特殊技术来检测发送垃圾邮件的机器人。

It can also just be used to identify a form and its action.

它也可以仅用于识别表单及其动作。

设定默认值 (Setting a default value)

All those fields accept a predefined value. If the user does not change it, this will be the value sent to the server:

所有这些字段均接受预定义的值。 如果用户不更改它,那么它将是发送到服务器的值:

If you set a placeholder, that value will appear if the user clears the input field value:

如果设置了占位符,则当用户清除输入字段值时,将显示该值:

表格提交 (Form submit)

The type="submit" field is a button that, once pressed by the user, submits the form:

type="submit"字段是一个按钮,一旦被用户按下,就会提交表单:

The value attribute sets the text on the button, which if missing shows the "Submit" text:

value属性设置按钮上的文本,如果缺少该文本,则会显示“提交”文本:

表格验证 (Form validation)

Browsers provide client-side validation functionality to forms.

浏览器为表单提供客户端验证功能。

You can set fields as required, ensuring they are filled, and enforce a specific format for the input of each field.

您可以根据需要设置字段,以确保将其填充,并对每个字段的输入强制采用特定格式。

Let's see both options.

让我们看看两个选项。

根据需要设置字段 (Set fields as required)

The required attribute helps you with validation. If the field is not set, client-side validation fails and the browser does not submit the form:

required属性可帮助您进行验证。 如果未设置该字段,则客户端验证将失败,并且浏览器不会提交表单:

强制执行特定格式 (Enforce a specific format)

I described the type="email" field above. It automatically validates the email address according to a format set in the specification.

我在上面描述了type="email"字段。 它会根据规范中设置的格式自动验证电子邮件地址。

In the type="number" field, I mentioned the min and max attribute to limit values entered to an interval.

type="number"字段中,我提到了minmax属性以限制输入到间隔的值。

You can do more.

您可以做更多。

You can enforce a specific format on any field.

您可以在任何字段上强制使用特定格式。

The pattern attribute gives you the ability to set a regular expression to validate the value against.

pattern属性使您能够设置正则表达式来验证值。

I recommend reading my Regular Expressions Guide at flaviocopes.com/javascript-regular-expressions/.

我建议阅读flaviocopes.com/javascript-regular-expressions/上的《正则表达式指南》。

pattern="https://.*"

pattern =“ https://.* ”

其他领域 (Other fields)

文件上传 (File uploads)

You can load files from your local computer and send them to the server using a type="file" input element:

您可以从本地计算机加载文件,然后使用type="file"输入元素将其发送到服务器:

You can attach multiple files:

您可以附加多个文件:

You can specify one or more file types allowed using the accept attribute. This accepts images:

您可以使用accept属性指定一种或多种允许的文件类型。 这接受图像:

You can use a specific MIME type, like application/json or set a file extension like .pdf. Or set multiple file extensions, like this:

您可以使用特定的MIME类型(例如application/json或设置文件扩展名(例如.pdf 。 或设置多个文件扩展名,如下所示:

纽扣 (Buttons)

The type="button" input fields can be used to add additional buttons to the form, that are not submit buttons:

type="button"输入字段可用于向表单添加不是提交按钮的其他按钮:

They are used to programmatically do something, using JavaScript.

它们用于使用JavaScript以编程方式执行某些操作。

There is a special field rendered as a button, whose special action is to clear the entire form and bring back the state of the fields to the initial one:

有一个特殊的字段呈现为按钮,其特殊动作是清除整个表单并将字段的状态恢复为初始状态:

单选按钮 (Radio buttons)

Radio buttons are used to create a set of choices, of which one is pressed and all the others are disabled.

单选按钮用于创建一组选项,其中一个选项被按下,其他所有选项均被禁用。

The name comes from old car radios that had this kind of interface.

这个名字来自具有这种接口的老式汽车收音机。

You define a set of type="radio" inputs, all with the same name attribute, and different valueattribute:

您定义一组type="radio"输入,所有输入都具有相同的name属性和不同的value属性:



Once the form is submitted, the color data property will have one single value.

提交表单后, color数据属性将只有一个值。

There's always one element checked. The first item is the one checked by default.

总会检查一个元素。 第一项是默认情况下选中的项。

You can set the value that's pre-selected using the checked attribute. You can use it only once per radio inputs group.

您可以使用checked属性设置预先选择的值。 每个无线电输入组只能使用一次。

选框 (Checkboxes)

Similar to radio boxes, but they allow multiple values to be chosen, or none at all.

与单选框相似,但是它们允许选择多个值,或者根本不选择。

You define a set of type="checkbox" inputs, all with the same name attribute, and different valueattribute:

您定义一组type="checkbox"输入,所有输入均具有相同的name属性和不同的value属性:



All those checkboxes will be unchecked by default. Use the checked attribute to enable them on page load.

All those checkboxes will be unchecked by default. Use the checked attribute to enable them on page load.

Since this input field allows multiple values, upon form submit the value(s) will be sent to the server as an array.

Since this input field allows multiple values, upon form submit the value(s) will be sent to the server as an array.

Date and time (Date and time)

We have a few input types to accept date values.

We have a few input types to accept date values.

The type="date" input field allows the user to enter a date, and shows a date picker if needed:

The type="date" input field allows the user to enter a date, and shows a date picker if needed:

The type="time" input field allows the user to enter a time, and shows a time picker if needed:

The type="time" input field allows the user to enter a time, and shows a time picker if needed:

The type="month" input field allows the user to enter a month and a year:

The type="month" input field allows the user to enter a month and a year:

The type="week" input field allows the user to enter a week and a year:

The type="week" input field allows the user to enter a week and a year:

All those fields allow to limit the range and the step between each value. I recommend checking MDN for the little details on their usage.

All those fields allow to limit the range and the step between each value. I recommend checking MDN for the little details on their usage.

The type="datetime-local" field lets you choose a date and a time.

The type="datetime-local" field lets you choose a date and a time.

Here is a page to test them all: https://codepen.io/flaviocopes/pen/ZdWQPm

Here is a page to test them all: https://codepen.io/flaviocopes/pen/ZdWQPm

Color picker (Color picker)

You can let users pick a color using the type="color" element:

You can let users pick a color using the type="color" element:

You set a default value using the value attribute:

You set a default value using the value attribute:

The browser will take care of showing a color picker to the user.

The browser will take care of showing a color picker to the user.

Range (Range)

This input element shows a slider element. People can use it to move from a starting value to an ending value:

This input element shows a slider element. People can use it to move from a starting value to an ending value:

You can provide an optional step:

You can provide an optional step:

Telephone (Telephone)

The type="tel" input field is used to enter a phone number:

The type="tel" input field is used to enter a phone number:

The main selling point for using tel over text is on mobile, where the device can choose to show a numeric keyboard.

The main selling point for using tel over text is on mobile, where the device can choose to show a numeric keyboard.

Specify a pattern attribute for additional validation:

Specify a pattern attribute for additional validation:

URL (URL)

The type="url" field is used to enter a URL.

The type="url" field is used to enter a URL.

You can validate it using the pattern attribute:

You can validate it using the pattern attribute:

The textarea tag (The textarea tag)

The textarea element allows users to enter multi-line text. Compared to input, it requires an ending tag:

The textarea element allows users to enter multi-line text. Compared to input , it requires an ending tag:

You can set the dimensions using CSS, but also using the rows and cols attributes:

You can set the dimensions using CSS, but also using the rows and cols attributes:

As with the other form tags, the name attribute determines the name in the data sent to the server:

As with the other form tags, the name attribute determines the name in the data sent to the server:

The select tag (The select tag)

This tag is used to create a drop-down menu.

This tag is used to create a drop-down menu.

The user can choose one of the options available.

The user can choose one of the options available.

Each option is created using the option tag. You add a name to the select, and a value to each option:

Each option is created using the option tag. You add a name to the select, and a value to each option:

You can set an option disabled:

You can set an option disabled:

You can have one empty option:

You can have one empty option:

Options can be grouped using the optgroup tag. Each option group has a label attribute:

Options can be grouped using the optgroup tag. Each option group has a label attribute:

TABLES (TABLES)

In the early days of the web tables were a very important part of building layouts.

In the early days of the web tables were a very important part of building layouts.

Later on they were replaced by CSS and its layout capabilities, and today we have powerful tools like CSS Flexbox and CSS Grid to build layouts. Tables are now used just for, guess what, building tables!

Later on they were replaced by CSS and its layout capabilities, and today we have powerful tools like CSS Flexbox and CSS Grid to build layouts. Tables are now used just for, guess what, building tables!

The table tag (The table tag)

You define a table using the table tag:

You define a table using the table tag:

Inside the table we'll define the data. We reason in terms of rows, which means we add rows into a table (not columns). We'll define columns inside a row.

Inside the table we'll define the data. We reason in terms of rows, which means we add rows into a table (not columns). We'll define columns inside a row.

Rows (Rows)

A row is added using the tr tag, and that's the only thing we can add into a table element:

A row is added using the tr tag, and that's the only thing we can add into a table element:

This is a table with 3 rows.

This is a table with 3 rows.

The first row can take the role of the header.

The first row can take the role of the header.

Column headers (Column headers)

The table header contains the name of a column, typically in a bold font.

The table header contains the name of a column, typically in a bold font.

Think about an Excel / Google Sheets document. The top A-B-C-D... header.

Think about an Excel / Google Sheets document. The top ABCD... header.

html手册_HTML手册_第6张图片

We define the header using the th tag:

We define the header using the th tag:

Column 1 Column 2 Column 3

The table content (The table content)

The content of the table is defined using td tags, inside the other tr elements:

The content of the table is defined using td tags, inside the other tr elements:

Column 1 Column 2 Column 3
Row 1 Column 1 Row 1 Column 2 Row 1 Column 3
Row 2 Column 1 Row 2 Column 2 Row 2 Column 3

This is how browsers render it, if you don't add any CSS styling:

This is how browsers render it, if you don't add any CSS styling:

html手册_HTML手册_第7张图片

Adding this CSS:

Adding this CSS:

th, td {
  padding: 10px;
  border: 1px solid #333;
}

makes the table look more like a proper table:

makes the table look more like a proper table:

html手册_HTML手册_第8张图片

Span columns and rows (Span columns and rows)

A row can decide to span over 2 or more columns, using the colspan attribute:

A row can decide to span over 2 or more columns, using the colspan attribute:

Column 1 Column 2 Column 3
Row 1 Columns 1-2 Row 1 Column 3
Row 2 Columns 1-3
html手册_HTML手册_第9张图片

Or it can span over 2 or more rows, using the rowspan attribute:

Or it can span over 2 or more rows, using the rowspan attribute:

Column 1 Column 2 Column 3
Rows 1-2 Columns 1-2 Row 1 Column 3
Row 2 Column 3
html手册_HTML手册_第10张图片

Row headings (Row headings)

Before I explained how you can have column headings, using the th tag inside the first tr tag of the table.

Before I explained how you can have column headings, using the th tag inside the first tr tag of the table.

You can add a th tag as the first element inside a tr that's not the first tr of the table, to have row headings:

You can add a th tag as the first element inside a tr that's not the first tr of the table, to have row headings:

Column 2 Column 3
Row 1 Col 2 Col 3
Row 2 Col 2 Col 3
html手册_HTML手册_第11张图片

More tags to organize the table (More tags to organize the table)

You can add 3 more tags into a table, to have it more organized.

You can add 3 more tags into a table, to have it more organized.

This is best when using big tables. And to properly define a header and a footer, too.

This is best when using big tables. And to properly define a header and a footer, too.

Those tags are

Those tags are

  • thead

    thead

  • tbody

    tbody

  • tfoot

    tfoot

They wrap the tr tags to clearly define the different sections of the table. Here's an example:

They wrap the tr tags to clearly define the different sections of the table. 这是一个例子:

Column 2 Column 3
Row 1 Col 2 Col 3
Row 2 Col 2 Col 3
Footer of Col 1 Footer of Col 2
html手册_HTML手册_第12张图片

Table caption (Table caption)

A table should have a caption tag that describes its content. That tag should be put immediately after the opening table tag:

A table should have a caption tag that describes its content. That tag should be put immediately after the opening table tag:

Dogs age
Dog Age
Roger 7

MULTIMEDIA TAGS: AUDIO AND VIDEO (MULTIMEDIA TAGS: AUDIO AND VIDEO)

In this section I want to show you the audio and video tags.

In this section I want to show you the audio and video tags.

The audio tag (The audio tag)

This tag allows you to embed audio content in your HTML pages.

This tag allows you to embed audio content in your HTML pages.

This element can stream audio, maybe using a microphone via getUserMedia(), or it can play an audio source which you reference using the src attribute:

This element can stream audio, maybe using a microphone via getUserMedia() , or it can play an audio source which you reference using the src attribute:

By default the browser does not show any controls for this element. Which means the audio will play only if set to autoplay (more on this later) and the user can't see how to stop it or control the volume or move through the track.

By default the browser does not show any controls for this element. Which means the audio will play only if set to autoplay (more on this later) and the user can't see how to stop it or control the volume or move through the track.

To show the built-in controls, you can add the controls attribute:

To show the built-in controls, you can add the controls attribute:

Controls can have a custom skin.

Controls can have a custom skin.

You can specify the MIME type of the audio file using the type attribute. If not set, the browser will try to automatically determine it:

You can specify the MIME type of the audio file using the type attribute. If not set, the browser will try to automatically determine it:

An audio file by default does not play automatically. Add the autoplay attribute to play the audio automatically:

An audio file by default does not play automatically. Add the autoplay attribute to play the audio automatically:

Note: mobile browsers don't allow autoplay

Note: mobile browsers don't allow autoplay

The loop attribute restarts the audio playing at 0:00 if set; otherwise, if not present, the audio stops at the end of the file:

The loop attribute restarts the audio playing at 0:00 if set; otherwise, if not present, the audio stops at the end of the file:

You can also play an audio file muted using the muted attribute (not really sure what's the usefulness of this):

You can also play an audio file muted using the muted attribute (not really sure what's the usefulness of this):

Using JavaScript you can listen for various events happening on an audio element, the most basic of which are:

Using JavaScript you can listen for various events happening on an audio element, the most basic of which are:

  • play when the file starts playing

    play when the file starts playing

  • pause when the audio playing was paused

    pause when the audio playing was paused

  • playing when the audio is resumed from a pause

    playing when the audio is resumed from a pause

  • ended when the end of the audio file was reached

    ended when the end of the audio file was reached

The video tag (The video tag)

This tag allows you to embed video content in your HTML pages.

This tag allows you to embed video content in your HTML pages.

This element can stream video, using a webcam via getUserMedia() or WebRTC, or it can play a video source which you reference using the src attribute:

This element can stream video, using a webcam via getUserMedia() or WebRTC , or it can play a video source which you reference using the src attribute:

By default the browser does not show any controls for this element, just the video.

By default the browser does not show any controls for this element, just the video.

Which means the video will play only if set to autoplay (more on this later) and the user can't see how to stop it, pause it, control the volume or skip to a specific position in the video.

Which means the video will play only if set to autoplay (more on this later) and the user can't see how to stop it, pause it, control the volume or skip to a specific position in the video.

To show the built-in controls, you can add the controls attribute:

To show the built-in controls, you can add the controls attribute:

Controls can have a custom skin.

Controls can have a custom skin.

You can specify the MIME type of the video file using the type attribute. If not set, the browser will try to automatically determine it:

You can specify the MIME type of the video file using the type attribute. If not set, the browser will try to automatically determine it:

A video file by default does not play automatically. Add the autoplay attribute to play the video automatically:

A video file by default does not play automatically. Add the autoplay attribute to play the video automatically:

Some browsers also require the muted attribute to autoplay. The video autoplays only if muted:

Some browsers also require the muted attribute to autoplay. The video autoplays only if muted:

The loop attribute restarts the video playing at 0:00 if set; otherwise, if not present, the video stops at the end of the file:

The loop attribute restarts the video playing at 0:00 if set; otherwise, if not present, the video stops at the end of the file:

You can set an image to be the poster image:

You can set an image to be the poster image:

If not present, the browser will display the first frame of the video as soon as it's available.

If not present, the browser will display the first frame of the video as soon as it's available.

You can set the width and height attributes to set the space that the element will take so that the browser can account for it and it does not change the layout when it's finally loaded. It takes a numeric value, expressed in pixels.

You can set the width and height attributes to set the space that the element will take so that the browser can account for it and it does not change the layout when it's finally loaded. It takes a numeric value, expressed in pixels.

Using JavaScript you can listen for various events happening on an video element, the most basic of which are:

Using JavaScript you can listen for various events happening on an video element, the most basic of which are:

  • play when the file starts playing

    play when the file starts playing

  • pause when the video was paused

    pause when the video was paused

  • playing when the video is resumed from a pause

    playing when the video is resumed from a pause

  • ended when the end of the video file was reached

    ended when the end of the video file was reached

IFRAMES (IFRAMES)

The iframe tag allows us to embed content coming from other origins (other sites) into our web page.

The iframe tag allows us to embed content coming from other origins (other sites) into our web page.

Technically, an iframe creates a new nested browsing context. This means that anything in the iframe does not interfere with the parent page, and vice versa. JavaScript and CSS do not "leak" to/from iframes.

Technically, an iframe creates a new nested browsing context. This means that anything in the iframe does not interfere with the parent page, and vice versa. JavaScript and CSS do not "leak" to/from iframes.

Many sites use iframes to perform various things. You might be familiar with Codepen, Glitch or other sites that allow you to code in one part of the page, and you see the result in a box. That's an iframe.

Many sites use iframes to perform various things. You might be familiar with Codepen, Glitch or other sites that allow you to code in one part of the page, and you see the result in a box. That's an iframe.

You create one this way:

You create one this way:

You can load an absolute URL, too:

You can load an absolute URL, too:

You can set a set of width and height parameters (or set them using CSS) otherwise the iframe will use the defaults, a 300x150 pixels box:

You can set a set of width and height parameters (or set them using CSS) otherwise the iframe will use the defaults, a 300x150 pixels box:

Srcdoc (Srcdoc)

The srcdoc attribute lets you specify some inline HTML to show. It's an alternative to src, but recent and not supported in Edge 18 and lower, and in IE:

The srcdoc attribute lets you specify some inline HTML to show. It's an alternative to src , but recent and not supported in Edge 18 and lower, and in IE:

Sandbox (Sandbox)

The sandbox attribute allows us to limit the operations allowed in the iframes.

The sandbox attribute allows us to limit the operations allowed in the iframes.

If we omit it, everything is allowed:

If we omit it, everything is allowed:

If we set it to "", nothing is allowed:

If we set it to "", nothing is allowed:

We can select what to allow by adding options in the sandbox attribute. You can allow multiple ones by adding a space in between. Here's an incomplete list of the options you can use:

We can select what to allow by adding options in the sandbox attribute. You can allow multiple ones by adding a space in between. Here's an incomplete list of the options you can use:

  • allow-forms: allow to submit forms

    allow-forms : allow to submit forms

  • allow-modals allow to open modals windows, including calling alert() in JavaScript

    allow-modals allow to open modals windows, including calling alert() in JavaScript

  • allow-orientation-lock allow to lock the screen orientation

    allow-orientation-lock allow to lock the screen orientation

  • allow-popups allow popups, using window.open() and target="_blank" links

    allow-popups allow popups, using window.open() and target="_blank" links

  • allow-same-origin treat the resource being loaded as same origin

    allow-same-origin treat the resource being loaded as same origin

  • allow-scripts lets the loaded iframe run scripts (but not create popups).

    allow-scripts lets the loaded iframe run scripts (but not create popups).

  • allow-top-navigation gives access to the iframe to the top level browsing context

    allow-top-navigation gives access to the iframe to the top level browsing context

Allow (Allow)

Currently experimental and only supported by Chromium-based browsers, this is the future of resource sharing between the parent window and the iframe.

Currently experimental and only supported by Chromium-based browsers, this is the future of resource sharing between the parent window and the iframe.

It's similar to the sandbox attribute, but lets us allow specific features, including:

It's similar to the sandbox attribute, but lets us allow specific features, including:

  • accelerometer gives access to the Sensors API Accelerometer interface

    accelerometer gives access to the Sensors API Accelerometer interface

  • ambient-light-sensor gives access to the Sensors API AmbientLightSensor interface

    ambient-light-sensor gives access to the Sensors API AmbientLightSensor interface

  • autoplay allows to autoplay video and audio files

    autoplay allows to autoplay video and audio files

  • camera allows to access the camera from the getUserMedia API

    camera allows to access the camera from the getUserMedia API

  • display-capture allows to access the screen content using the getDisplayMedia API

    display-capture allows to access the screen content using the getDisplayMedia API

  • fullscreen allows to access fullscreen mode

    fullscreen allows to access fullscreen mode

  • geolocation allows to access the Geolocation API

    geolocation allows to access the Geolocation API

  • gyroscope gives access to the Sensors API Gyroscope interface

    gyroscope gives access to the Sensors API Gyroscope interface

  • magnetometer gives access to the Sensors API Magnetometer interface

    magnetometer gives access to the Sensors API Magnetometer interface

  • microphone gives access to the device microphone using the getUserMedia API

    microphone gives access to the device microphone using the getUserMedia API

  • midi allows access to the Web MIDI API

    midi allows access to the Web MIDI API

  • payment gives access to the Payment Request API

    payment gives access to the Payment Request API

  • speaker allows access to playing audio through the device speakers

    speaker allows access to playing audio through the device speakers

  • usb gives access to the WebUSB API.

    usb gives access to the WebUSB API.

  • vibrate gives access to the Vibration API

    vibrate gives access to the Vibration API

  • vr gives access to the WebVR API

    vr gives access to the WebVR API

Referrer (Referrer)

When loading an iframe, the browser sends it important information about who is loading it in the Referer header (notice the single r, a typo we must live with).

When loading an iframe, the browser sends it important information about who is loading it in the Referer header (notice the single r , a typo we must live with).

The misspelling of referrer originated in the original proposal by computer scientist Phillip Hallam-Baker to incorporate the field into the HTTP specification. The misspelling was set in stone by the time of its incorporation into the Request for Comments standards document RFC 1945

The misspelling of referrer originated in the original proposal by computer scientist Phillip Hallam-Baker to incorporate the field into the HTTP specification. The misspelling was set in stone by the time of its incorporation into the Request for Comments standards document RFC 1945

The referrerpolicy attribute lets us set the referrer to send to the iframe when loading it. The referrer is an HTTP header that lets the page know who is loading it. These are the allowed values:

The referrerpolicy attribute lets us set the referrer to send to the iframe when loading it. The referrer is an HTTP header that lets the page know who is loading it. These are the allowed values:

  • no-referrer-when-downgrade it's the default, and does not send the referrer when the current page is loaded over HTTPS and the iframe loads on the HTTP protocol

    no-referrer-when-downgrade it's the default, and does not send the referrer when the current page is loaded over HTTPS and the iframe loads on the HTTP protocol

  • no-referrer does not send the referrer header

    no-referrer does not send the referrer header

  • origin the referrer is sent, and only contains the origin (port, protocol, domain), not the origin + path which is the default

    origin the referrer is sent, and only contains the origin (port, protocol, domain), not the origin + path which is the default

  • origin-when-cross-origin when loading from the same origin (port, protocol, domain) in the iframe, the referrer is sent in its complete form (origin + path). Otherwise only the origin is sent

    origin-when-cross-origin when loading from the same origin (port, protocol, domain) in the iframe, the referrer is sent in its complete form (origin + path). Otherwise only the origin is sent

  • same-origin the referrer is sent only when loading from the same origin (port, protocol, domain) in the iframe

    same-origin the referrer is sent only when loading from the same origin (port, protocol, domain) in the iframe

  • strict-origin sends the origin as the referrer if the current page is loaded over HTTPS and the iframe also loads on the HTTPS protocol. Sends nothing if the iframe is loaded over HTTP

    strict-origin sends the origin as the referrer if the current page is loaded over HTTPS and the iframe also loads on the HTTPS protocol. Sends nothing if the iframe is loaded over HTTP

  • strict-origin-when-cross-origin sends the origin + path as the referrer when working on the same origin. Sends the origin as the referrer if the current page is loaded over HTTPS and the iframe also loads on the HTTPS protocol. Sends nothing if the iframe is loaded over HTTP

    strict-origin-when-cross-origin sends the origin + path as the referrer when working on the same origin. Sends the origin as the referrer if the current page is loaded over HTTPS and the iframe also loads on the HTTPS protocol. Sends nothing if the iframe is loaded over HTTP

  • unsafe-url: sends the origin + path as the referrer even when loading resources from HTTP and the current page is loaded over HTTPS

    unsafe-url : sends the origin + path as the referrer even when loading resources from HTTP and the current page is loaded over HTTPS

IMAGES (IMAGES)

Images can be displayed using the img tag.

Images can be displayed using the img tag.

This tag accepts a src attribute, which we use to set the image source:

This tag accepts a src attribute, which we use to set the image source:

We can use a wide set of images. The most common ones are PNG, JPEG, GIF, SVG and more recently WebP.

We can use a wide set of images. The most common ones are PNG, JPEG, GIF, SVG and more recently WebP.

The HTML standard requires an alt attribute to be present, to describe the image. This is used by screen readers and also by search engine bots:

The HTML standard requires an alt attribute to be present, to describe the image. This is used by screen readers and also by search engine bots:

A picture of a dog

You can set the width and height attributes to set the space that the element will take, so that the browser can account for it and it does not change the layout when it's fully loaded. It takes a numeric value, expressed in pixels.

You can set the width and height attributes to set the space that the element will take, so that the browser can account for it and it does not change the layout when it's fully loaded. It takes a numeric value, expressed in pixels.

A picture of a dog

The figure tag (The figure tag)

The figure tag is often used along with the img tag.

The figure tag is often used along with the img tag.

figure is a semantic tag often used when you want to display an image with a caption. You use it like this:

figure is a semantic tag often used when you want to display an image with a caption. You use it like this:

A nice dog
A nice dog

The figcaption tag wraps the caption text.

The figcaption tag wraps the caption text.

Responsive images using srcset (Responsive images using srcset)

The srcset attribute allows you to set responsive images that the browser can use depending on the pixel density or window width, according to your preferences. This way, it can only download the resources it needs to render the page, without downloading a bigger image if it's on a mobile device, for example.

The srcset attribute allows you to set responsive images that the browser can use depending on the pixel density or window width, according to your preferences. This way, it can only download the resources it needs to render the page, without downloading a bigger image if it's on a mobile device, for example.

Here's an example, where we give 4 additional images for 4 different screen sizes:

Here's an example, where we give 4 additional images for 4 different screen sizes:

A picture of a dog

In the srcset we use the w measure to indicate the window width.

In the srcset we use the w measure to indicate the window width.

Since we do so, we also need to use the sizes attribute:

Since we do so, we also need to use the sizes attribute:

A picture of a dog

In this example the (max-width: 500px) 100vw, (max-width: 900px) 50vw, 800px string in the sizes attribute describes the size of the image in relation to the viewport, with multiple conditions separated by a semicolon.

In this example the (max-width: 500px) 100vw, (max-width: 900px) 50vw, 800px string in the sizes attribute describes the size of the image in relation to the viewport, with multiple conditions separated by a semicolon.

The media condition max-width: 500px sets the size of the image in correlation to the viewport width. In short, if the window size is < 500px, it renders the image at 100% of the window size.

The media condition max-width: 500px sets the size of the image in correlation to the viewport width. In short, if the window size is < 500px, it renders the image at 100% of the window size.

If the window size is bigger but < 900px, it renders the image at 50% of the window size.

If the window size is bigger but < 900px , it renders the image at 50% of the window size.

And if even bigger, it renders the image at 800px.

And if even bigger, it renders the image at 800px.

The vw unit of measure can be new to you, and in short we can say that 1 vw is 1% of the window width, so 100vw is 100% of the window width.

The vw unit of measure can be new to you, and in short we can say that 1 vw is 1% of the window width, so 100vw is 100% of the window width.

A useful website to generate the srcset and progressively smaller images is https://responsivebreakpoints.com/.

A useful website to generate the srcset and progressively smaller images is https://responsivebreakpoints.com/ .

The picture tag (The picture tag)

HTML also gives us the picture tag, which does a very similar job to srcset, and the differences are very subtle.

HTML also gives us the picture tag, which does a very similar job to srcset , and the differences are very subtle.

You use picture when instead of just serving a smaller version of a file, you completely want to change it. Or serve a different image format.

You use picture when instead of just serving a smaller version of a file, you completely want to change it. Or serve a different image format.

The best use case I found is when serving a WebP image, which is a format still not widely supported. In the picture tag you specify a list of images, and they will be used in order, so in the next example, browsers that support WebP will use the first image, and fallback to JPG if not:

The best use case I found is when serving a WebP image, which is a format still not widely supported. In the picture tag you specify a list of images, and they will be used in order, so in the next example, browsers that support WebP will use the first image, and fallback to JPG if not:


  
  An image

The source tag defines one (or more) formats for the images. The img tag is the fallback in case the browser is very old and does not support the picture tag.

The source tag defines one (or more) formats for the images. The img tag is the fallback in case the browser is very old and does not support the picture tag.

In the source tag inside picture you can add a media attribute to set media queries.

In the source tag inside picture you can add a media attribute to set media queries.

The example that follows kind of works like the above example with srcset:

The example that follows kind of works like the above example with srcset :


  
  
  
  
  A dog image

But that's not its use case, because as you can see it's much more verbose.

But that's not its use case, because as you can see it's much more verbose.

The picture tag is recent but is now supported by all the major browsers except Opera Mini and IE (all versions).

The picture tag is recent but is now supported by all the major browsers except Opera Mini and IE (all versions).

ACCESSIBILITY (ACCESSIBILITY)

It's important we design our HTML with accessibility in mind.

It's important we design our HTML with accessibility in mind.

Having accessible HTML means that people with disabilities can use the Web. There are totally blind or visually impaired users, people with hearing loss issues and a multitude of other different disabilities.

Having accessible HTML means that people with disabilities can use the Web. There are totally blind or visually impaired users, people with hearing loss issues and a multitude of other different disabilities.

Unfortunately this topic does not take the importance it needs, and it doesn't seem as cool as others.

Unfortunately this topic does not take the importance it needs, and it doesn't seem as cool as others.

What if a person can't see your page, but still wants to consume its content? First, how do they do that? They can't use the mouse, they use something called a screen reader. You don't have to imagine that. You can try one now: Google provides the free ChromeVox Chrome Extension. Accessibility must also take care of allowing tools to easily select elements or navigate through the pages.

What if a person can't see your page, but still wants to consume its content? First, how do they do that? They can't use the mouse, they use something called a screen reader . You don't have to imagine that. You can try one now: Google provides the free ChromeVox Chrome Extension . Accessibility must also take care of allowing tools to easily select elements or navigate through the pages.

Web pages and Web apps are not always built with accessibility as one of their first goals, and maybe version 1 is released not accessible but it's possible to make a web page accessible after the fact. Sooner is better, but it's never too late.

Web pages and Web apps are not always built with accessibility as one of their first goals, and maybe version 1 is released not accessible but it's possible to make a web page accessible after the fact. Sooner is better, but it's never too late.

It's important and in my country, websites built by the government or other public organizations must be accessible.

It's important and in my country, websites built by the government or other public organizations must be accessible.

What does this mean to make an HTML accessible? Let me illustrate the main things you need to think about.

What does this mean to make an HTML accessible? Let me illustrate the main things you need to think about.

Note: there are several other things to take care about, which might go in the CSS topic, like colors, contrast and fonts. Or how to make SVG images accessible. I don't talk about them here.

Note: there are several other things to take care about, which might go in the CSS topic, like colors, contrast and fonts. Or how to make SVG images accessible . I don't talk about them here.

Use semantic HTML (Use semantic HTML)

Semantic HTML is very important and it's one of the main things you need to take care of. Let me illustrate a few common scenarios.

Semantic HTML is very important and it's one of the main things you need to take care of. Let me illustrate a few common scenarios.

It's important to use the correct structure for heading tags. The most important is h1, and you use higher numbers for less important ones, but all the same-level headings should have the same meaning (think about it like a tree structure)

It's important to use the correct structure for heading tags. The most important is h1 , and you use higher numbers for less important ones, but all the same-level headings should have the same meaning (think about it like a tree structure)

h1
    h2
        h3
    h2
    h2
        h3
            h4

Use strong and em instead of b and i. Visually they look the same, but the first 2 have more meaning associated with them. b and i are more visual elements.

Use strong and em instead of b and i . Visually they look the same, but the first 2 have more meaning associated with them. b and i are more visual elements.

Lists are important. A screen reader can detect a list and provide an overview, then let the user choose to get into the list or not.

Lists are important. A screen reader can detect a list and provide an overview, then let the user choose to get into the list or not.

A table should have a caption tag that describes its content:

A table should have a caption tag that describes its content:

Dogs age
Dog Age
Roger 7

Use alt attributes for images (Use alt attributes for images)

All images must have an alt tag describing the image content. It's not just a good practice, it's required by the HTML standard and your HTML without it is not validated.

All images must have an alt tag describing the image content. It's not just a good practice, it's required by the HTML standard and your HTML without it is not validated.

picture of my dog

It's also good for search engines, if that's an incentive for you to add it.

It's also good for search engines, if that's an incentive for you to add it.

Use the role attribute (Use the role attribute)

The role attribute lets you assign specific roles to the various elements in your page.

The role attribute lets you assign specific roles to the various elements in your page.

You can assign lots of different roles: complementary, list, listitem, main, navigation, region, tab, alert, application, article, banner, button, cell, checkbox, contentinfo, dialog, document, feed, figure, form, grid, gridcell, heading, img, listbox, row, rowgroup, search, switch, table, tabpanel, textbox, timer.

You can assign lots of different roles: complementary, list, listitem, main, navigation, region, tab, alert, application, article, banner, button, cell, checkbox, contentinfo, dialog, document, feed, figure, form, grid, gridcell, heading, img, listbox, row, rowgroup, search, switch, table, tabpanel, textbox, timer.

It's a lot and for the full reference of each of them I give you this MDN link. But you don't need to assign a role to every element in the page. Screen readers can infer from the HTML tag in most cases. For example you don't need to add a role tag to semantic tags like nav, button, form.

It's a lot and for the full reference of each of them I give you this MDN link . But you don't need to assign a role to every element in the page. Screen readers can infer from the HTML tag in most cases. For example you don't need to add a role tag to semantic tags like nav , button , form .

Let's take the nav tag example. You can use it to define the page navigation like this:

Let's take the nav tag example. You can use it to define the page navigation like this:

If you were forced to use a div tag instead of nav, you'd use the navigation role:

If you were forced to use a div tag instead of nav , you'd use the navigation role:

So here you got a practical example: role is used to assign a meaningful value when the tag does not convey the meaning already.

So here you got a practical example: role is used to assign a meaningful value when the tag does not convey the meaning already.

Use the tabindex attribute (Use the tabindex attribute)

The tabindex attribute allows you to change the order of how pressing the Tab key selects "selectable" elements. By defaults only links and form elements are "selectable" by navigation using the Tab key (and you don't need to set tabindex on them).

The tabindex attribute allows you to change the order of how pressing the Tab key selects "selectable" elements. By defaults only links and form elements are "selectable" by navigation using the Tab key (and you don't need to set tabindex on them).

Adding tabindex="0" makes an element selectable:

Adding tabindex="0" makes an element selectable:

...

Using tabindex="-1" instead removes an element from this tab-based navigation, and it can be pretty useful.

Using tabindex="-1" instead removes an element from this tab-based navigation, and it can be pretty useful.

Use the aria attributes (Use the aria attributes)

ARIA is an acronym that means Accessible Rich Internet Applications and defines semantics that can be applied to elements.

ARIA is an acronym that means Accessible Rich Internet Applications and defines semantics that can be applied to elements.

aria-label (aria-label)

This attribute is used to add a string to describe an element.

This attribute is used to add a string to describe an element.

Example:

例:

...

I use this attribute on my blog sidebar, where I have an input box for search without an explicit label, as it has a placeholder attribute.

I use this attribute on my blog sidebar, where I have an input box for search without an explicit label, as it has a placeholder attribute.

aria-labelledby (aria-labelledby)

This attribute sets a correlation between the current element and the one that labels it.

This attribute sets a correlation between the current element and the one that labels it.

If you know how an input element can be associated to a label element, that's similar.

If you know how an input element can be associated to a label element, that's similar.

We pass the item id that describes the current element.

We pass the item id that describes the current element.

Example:

例:

The description of the product

...

aria-describedby (aria-describedby)

This attribute lets us associate an element with another element that serves as description.

This attribute lets us associate an element with another element that serves as description.

Example:

例:



Clicking the button will send you to our Stripe form!

Use aria-hidden to hide content (Use aria-hidden to hide content)

I like a minimalistic design in my sites. My blog for example is mostly just content, with some links in the sidebar. But some things in the sidebar are just visual elements that don't add up to the experience of a person that can't see the page. Like my logo picture, or the dark/bright theme selector.

I like a minimalistic design in my sites. My blog for example is mostly just content, with some links in the sidebar. But some things in the sidebar are just visual elements that don't add up to the experience of a person that can't see the page. Like my logo picture, or the dark/bright theme selector.

Adding the aria-hidden="true" attribute will tell screen readers to ignore that element.

Adding the aria-hidden="true" attribute will tell screen readers to ignore that element.

Where to learn more (Where to learn more)

This is just an introduction to the topic. To learn more, I recommend these resources:

This is just an introduction to the topic. To learn more, I recommend these resources:

  • https://www.w3.org/TR/WCAG20/

    https://www.w3.org/TR/WCAG20/

  • https://webaim.org

    https://webaim.org

  • https://developers.google.com/web/fundamentals/accessibility/

    https://developers.google.com/web/fundamentals/accessibility/



You reached the end of the HTML Handbook.

You reached the end of the HTML Handbook.

html手册_HTML手册_第13张图片

Click here to get a PDF / ePub / Mobi version of this book to read offline!

Click here to get a PDF / ePub / Mobi version of this book to read offline !

翻译自: https://www.freecodecamp.org/news/the-html-handbook/

html手册

你可能感兴趣的:(html,oauth,人工智能,java,meta)