Hand touch hand to teach you to write your life in the first HTML page

This article is "HTML5 and CSS3 basic grammar self-study program," the second in, starting at the front end of class [Cham] micro-channel public number.

REVIEW : This section mainly on the basic grammar of HTML content, will learn the basic structure of the HTML, by writing the first HTML page <!DOCTYPE>statement, elements, and comments and so on. We can have a comprehensive grasp of basic grammar of HTML by completing this section, which is why we follow when learning other HTML content must be used to.

Tip : Learn basic grammar will need to develop HTML editor, here using Visual Studio Code Editor. Of course, you can also choose your more familiar development editor to use. The basic operation regarding Visual Studio Code Editor reference in this section may be extended to read .

1. HTML basic structure

1.1 Creating your first HTML page

Open the Visual Studio Code Editor, create a new file, and save it as .htmlor .htmfile extension. Then, in the new HTML file input HTML, then the Visual Studio Code Editor will pop-up boxes.

As shown below shows the balloon after the input:

02-01.png

Follow the prompts Visual Studio Code Editor, select [html: 5] this option, you created the basic structure of an HTML.

As shown below shows the HTML template after the content is created:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>

<body>

</body>

</html>

Description : [html: 5] option indicates that the use of HTML5 version of the template.

1.2 HTML templates brief description

Created the basic structure of HTML5 version, then we order from top to bottom, this basic structure core content is simply carried out a preliminary understanding.

1.2.1 <! DOCTYPE> declaration

<!DOCTYPE html>

在 HTML5 基本结构的第一行,这块内容被称为 HTML 声明。HTML 声明的作用是浏览器运行该 HTML 页面时,告知浏览器当前 HTML 页面的版本。

1.2.2 HTML 基本结构

<html lang="en">

    <head></head>
  
    <body></body>

</html>

除了第一行的 HTML 声明之外,其余内容才算是 HTML 的基本结构。接下来我们进行分别说明:

  • <html> 元素:表示当前 HTML 页面的根元素,用来包含所有其他 HTML 元素。
  • <head> 元素:表示当前 HTML 页面的头部,用来定义当前 HTML 页面的基本信息,例如标题、关键字、作者等内容。
  • <body> 元素:表示当前 HTML 页面的主体,用来定义最终显示在浏览器窗口的内容。

2. <!DOCTYPE> 声明

HTML 页面的第一行一般都是编写 HTML 声明。HTML 声明的作用就是当浏览器运行该 HTML 页面时来告知浏览器当前 HTML 页面的版本,这样浏览器会准确地进行解析并展示其内容。

HTML 声明必须要编写在 HTML 页面的第一行,一般都是在 <html> 元素之前。并且 HTML 声明之前不能存在空行或者空格,不然会导致 HTML 声明失效。

浏览器发展至今,其功能也非常的强大。所以,如果 HTML 页面没有定义 <!DOCTYPE> 声明的话,浏览器也可以正确地解析该 HTML 页面并进行显示。但是,还是建议在编写 HTML 页面时定义 <!DOCTYPE> 声明

再有,我们需要注意 HTML5 版本和 HTML 4.01 版本的声明是不同的。

  • 如下示例代码所示展示了 HTML5 版本的声明:
<!DOCTYPE html>
  • HTML 4.01 版本的声明分别存在三个版本。
    • HTML 4.01 Strict
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

    该声明是严格型约束,该 DTD 文件包含所有 HTML 元素和属性,但不包括展示性的和弃用的元素(比如 font),也不允许框架集(Framesets)。

    • HTML 4.01 Transitional
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

    该声明是过渡型约束,该 DTD 文件包含所有 HTML 元素和属性,包括展示性的和弃用的元素(比如 font),但不允许框架集(Framesets)。

    • HTML 4.01 Transitional
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">

    该声明是框架集约束,该 DTD 等同于 HTML 4.01 Transitional,但允许框架集内容。

通过两个版本的 HTML 声明内容,我们也可以看出 HTML5 版本的声明内容简化了不少。不仅没有了版本信息,也不需要约束文件。

说明:约束文件指的是如上述 strict.dtdloose.dtd 或者 frameset.dtd 文件。HTML 的约束文件是用来定义 HTML 的元素以及编写规范。

3. HTML 元素

除了 HTML 声明之外,其他内容都是 HTML 元素。首先,需要搞清楚元素(Element)和标签(Tag)之间的区别,如下图所示:

02-02.png

  • 元素(Element):是用来包含文字、图片或者音视频的内容,一般是由标签和内容组成。
  • 标签(Tag):是元素的组成部分,一般分为开始标签和结束标签。

3.1 HTML 元素

HTML 元素是 HTML 的重要组成部分之一,如下图所示展示了 HTML 元素的语法结构:

02-03.png

HTML 元素可以分为闭合元素和空元素两种类型:

  • 闭合元素:具有开始标签和结束标签,而且开始标签和结束标签是成对出现的。如下示例代码展示了闭合元素:
<div>文本内容</div>
  • 空元素:只有开始标签,而没有结束标签。如下示例代码展示了空元素:
<input type="text">

注意:在编写 HTML 元素时,如果是闭合元素不要忘记结束标签,如果是空元素不要编写结束标签。目前的开发编辑器会有相应的提示功能。

由于 HTML 是大小写不敏感的,所以 HTML 元素的元素名写成大写或小写都是允许的,例如 <div><Div><DIV> 是一样的含义。

但是 W3C 组织早在 HTML4 版本时,建议元素名使用小写形式。后来出现的 XHTML 是强制元素名必须使用小写形式。所以,元素名的编写还是尽量使用小写形式,而且现在的开发编辑器的提供功能也都是小写形式的。

注意<html> 元素是比较特殊的元素,称为根元素。在一个 HTML 页面中只能存在一个 <html> 元素,即使编写了多个 <html> 元素,运行 HTML 页面时浏览器也会自动忽略。

3.2 HTML 标签

HTML 标签实际上是 HTML 元素的组成部分之一,分为开始标签和结束标签。

  • 开始标签(Opening Tag):表示某个元素是从这里开始的。
  • 结束标签(Closing Tag):表示某个元素是到这里结束的。

无论是开始标签还是结束标签都具有的结构:

  • 左尖角号
  • 元素名
  • 右尖角号

如下图所示展示了标签的结构:

02-04.png
而结束标签相比开始标签多了个结束符(/)。如下图所示展示了结束标签的结构:

02-05.png

3.3 HTML 元素的属性

属性是 HTML 元素的重要组成部分,用来定义某个元素的信息。例如为 <div> 元素定义 ID 属性,就是定义了唯一标识。

属性定义在元素的开始标签中,这样无论是闭合元素还是空元素都可以正常使用属性。属性的语法结构是键值对形式的。如下图所示展示了属性的语法结构:

02-06.png

  • 属性名(attribute name):其数量和作用都是 HTML 给定的。
  • 属性值(attribute value):属性对应的值,建议使用一对双引号进行包裹。

注意:不同的属性,对应不同类型的值。

与元素的情况类似,属性的编写 W3C 组织也是建议使用小写形式。目前开发编辑器的提示功能也都是提供小写形式的。

同一个元素是允许编写多个不同的属性的,但在同一个元素中不能同时定义多个相同的属性。再有,HTML 元素的属性可以划分为以下 4 种:

  1. 标准(通用)属性:HTML 元素几乎都具有的属性,例如 id、name、style 和 class 属性等。
  2. 专有(私有)属性:HTML 中某些元素特有的属性,例如 <form> 元素的 action 属性等。
  3. 事件属性:用来为 HTML 元素注册 DOM 事件的属性,例如 onclick 属性等。
  4. 自定义属性:第三方框架中为了完成某个特定功能而定义的属性,例如 Vue 框架的 v-if 属性等。

4. HTML 头部

HTML 头部具体是指 <head> 元素以及该元素所包含的所有元素,其作用是用来定义当前 HTML 页面的基本信息,例如 HTML 页面的标题、编写格式、作者、关键字以及描述等内容。

4.1 <head> 元素

<head> 元素是 HTML 页面基本结构中的组成部分,其作用是定义 HTML 页面的基本信息。可定义在 <head> 元素内的元素有如下:

  • <title> 元素:定义 HTML 页面的标题,显示在浏览器的标题或标签页上。如下示例代码所示展示了 <title> 元素的用法:
<title>Document</title>
  • <base> 元素:定义 HTML 页面中所有相对 URL 的根 URL。如下示例代码所示展示了 <base> 元素的用法:
<base target="_blank" href="http://www.example.com/">

注意:一个 HTML 页面只能定义一个 <base> 元素。如果一个 HTML 页面定义了多个 <base> 元素的话,则只有第一个 <base> 元素有效。

  • <link> 元素:定义 HTML 页面引入的外部资源,比较常见的是引入外部 CSS 文件或图标文件等。

    如下示例代码所示展示了通过 <link> 元素引入外部 CSS 文件:
<link href="link-element-example.css" rel="stylesheet">

  • 如下示例代码所示展示了通过 <link> 元素引入外部图标文件:
<link rel="icon" href="favicon.ico">
  • <style> 元素:定义 HTML 页面的 CSS 样式,一般称为内嵌样式表。如下示例代码所示展示了通过 <style> 元素定义内嵌样式表:
<style type="text/css">
    body {
    color:red;
    }
</style>
  • <meta> 元素:定义 HTML 页面的元数据信息,例如编码格式、作者、关键字等。如下示例代码所示展示了通过 <meta> 元素的用法:
<meta charset="UTF-8">
  • <script> 元素:定义 HTML 页面的可执行的脚本,一般多为 JavaScript 脚本。如下示例代码所示展示了通过 <script> 元素定义 JavaScript 脚本代码:
<script type="text/javascript">
    console.log('打印一个测试信息.');
</script>
  • <noscript> 元素:定义当 HTML 页面的脚本代码不被支持或者浏览器关闭了脚本执行时的替代内容。如下示例代码所示展示了 <noscript> 元素的用法:
<noscript>
    <a href="http://www.example.com/">这是一个链接</a>
</noscript>
  • <command> 元素:定义 HTML 页面允许用户可以调用的命令。该元素已被废弃!

4.2 <meta> 元素

<meta> 元素是用来定义不能由 <base><link><script><style><title> 元素定义的元数据信息。并且 <meta> 元素是个空元素。

<meta> 元素常用的用法如下所示:

  • 为搜索引擎定义关键词:
<meta name="keywords" content="HTML, CSS, XML, XHTML, JavaScript">
  • 为网页定义描述内容:
<meta name="description" content="Free Web tutorials on HTML and CSS">
  • 定义网页作者:
<meta name="author" content="KingJ">
  • 每30秒中刷新当前页:
<meta http-equiv="refresh" content="30">
  • HTML5 版本定义编码格式:
<meta charset="UTF-8">
  • 定义 HTML 页面的视口:
<meta name="viewport" content="width=device-width, initial-scale=1.0">

提示:除了以上罗列的常见用法之外,<meta> 元素还有很多其他用法,具体的用法在对应的章节进行详细讲解。

5. HTML 注释

与很多开发语言类似,HTML 语言提供了编写注释的语法内容。在浏览器运行 HTML 页面时,浏览器会自动忽略注释里面的内容,用户是看不到 HTML 页面中有关注释的内容的。

当一段内容被 <!----> 包裹起来时,那么这段内容就是一个注释内容了。如下图所示展示了 HTML 注释的语法结构:

02-07.png

注释可以用来更好地描述某一段 HTML 代码的含义或作用。无论是自己一段时间之后回顾(review)代码,还是别人处理这段代码,注释都是起到了很大作用的。

说明:编写注释也是程序员在开发工作中的一个优良习惯,希望你可以学习并保持这一优良习惯。

6. 扩展阅读

6.1 Visual Studio Code 下载与安装

Visual Studio Code 编辑器是 Microsoft 公司在 2015 年 4 月 30 日 Build 开发者大会上推出的一款运行于 Mac OS X、Windows 和 Linux 之上的,针对于编写现代 Web 和云应用的跨平台源代码编辑器。

我们可以通过访问 Visual Studio Code 编辑器的 官网 来了解,如下图所示展示了 Visual Studio Code 官网的部分界面:

02-08.png

Visual Studio Code 编辑器的官网会自动识别我们当前的操作系统,并提供对应的安装包下载。我们需要做的就是下载对应版本的安装包到本地电脑,再进行安装即可。

提示:这里有关具体的下载和安装步骤就不做详细说明了,因为非常的简单。

6.2 Visual Studio Code 设置中文界面

一般情况下,安装好的 Visual Studio Code 编辑器的界面就是中文的。但也不妨有特殊情况出现,如果你在安装完 Visual Studio Code 打开后发现界面是英文(或者是任何其他语言)界面可以通过扩展安装中文语言插件来解决。具体可以按照如下步骤进行操作:

提示:以下操作请在你的电脑可以正常联网的情况下进行操作。

a. 打开 Visual Studio Code 编辑器的扩展界面

打开 Visual Studio Code 编辑器之后,我们可以看到在最左边是有 5 个菜单的。其中第 5 个菜单(图标)表示是扩展,如下图所示展示了扩展菜单所在的位置:

02-09.png

b. 搜索中文语言插件包

在扩展界面的搜索框中,输入【Chinese】关键字来搜索中文语言插件包。搜索结果中的第一个结果一般就是我们要找的中文语言插件包,如下图所示展示了搜索结果:

02-10.png

在搜索中第一个结果点击【Install】按钮,来安装对应的插件包即可。

c. 重启 Visual Studio Code 编辑器

安装成功之后 Visual Studio Code 编辑器会弹出让你重启编辑器的提示框,如下图所示展示安装插件成功之后的提示框:

02-11.png

这时点击提示框中的【Restart Now】按钮,来重启 Visual Studio Code 编辑器。重启之后,Visual Studio Code 编辑器的界面就成功地改为了中文。如下图所示展示了中文操作界面的 Visual Studio Code 编辑器:

02-12.png

6.3 Visual Studio Code 新建文件

Visual Studio Code 编辑器新建文件有两种方式进行操作:

a. 通过菜单完成新建操作

点击 Visual Studio Code 编辑器顶部菜单中的【文件】,在弹出的菜单中选择【新建文件】选项,完成新建文件的操作。

如下图所示展示了新建文件的菜单位置:

02-13.png

b. 通过快捷键完成新建操作

如果你的操作系统是 Windows 的话,可以通过【Ctrl+N】快捷键直接创建一个新的文件。如果你的操作系统是 Mac OS 的话,可以通过【Command+N】快捷键直接创建一个新的文件。

Note : The new documents in the case do not save, there is no extension. In other words, the new document does not belong to any file type.

6.4 Visual Studio Code Save the file

Visual Studio Code Editor save file has to operate in two ways:

a. menu save operation is completed

Click Visual Studio Code Editor in the top menu [File] in the pop-up menu, select [Save] option, save the file to complete the operation.

It is shown in the following figure shows the location of the menu file is stored:

02-14.png

b. By shortcut keys save operation

If your operating system is Windows, then by [Ctrl S +] shortcut keys to save the file operation. If your operating system is Mac OS, you can [by] shortcut Command + S to save the file to complete the operation.

Note : Be sure to save the file when the specified file extension. If you do not specify a file name extension, the file will not belong to any file type, and can affect the contents of the file to write.

7. Summary

This section begins to explain the basic structure of the HTML and HTML separately according to explain the basic structure, including <!DOCTYPE>a statement, HTML elements, HTML header and HTML comments and so on. among them:

  • <!DOCTYPE> Disclaimer: focus on what is its role, usage and precautions, as well as HTML 4.01 and HTML 5 are two versions of the wording.
  • HTML elements: focus on the grammatical structure of HTML elements, classification and sorting features, and related content attributes.
  • HTML head: focus on the role of head of HTML, which elements and functions, as well as common include <meta>the use of elements.

Notice : the next section, we introduce basic information about CSS, including CSS concept, development of CSS and CSS version of the development process and so on.

作者二维码.png

Guess you like

Origin www.cnblogs.com/qianduankezhan/p/12052175.html