成 | 语言 | 描述 |
结构 | HTML | 网页元素和内容 |
表现 | CSS | 网页元素页面样式 |
行为 | JavaScript | 网页交互 |
HTML,超文本标记语言(Hyper Text Markup Language),是一门描述性语言。标记,标签,元素,叫法不同,意思相同。HTML超文本标记语言主要通过标签的方式,对网页页面的文本、图片、音频、视频等内容进行描述。学习HTML,就是学习各种标签,来搭建网页的结构。
结构:!DOCTYPE
说明:作用是告诉浏览器用哪个文档规范来解析文档
标签:html
说明:用于搭建HTML网页文档结构和网页布局
标签:head
说明:用于定义HTML网页文档的头部,它是所有头部元素的容器
标签:body
说明:用来定义HTML网页文档的主体区域
标签:meta
说明:用来描述HTML网页文档的属性
标签:title
说明:用来放到HTML网页文档的头部,是搜索引擎首要抓取的目标代码
标签,也叫作标记,是由一对尖括号<>,里面包含单词组成
<html></html>
<br>
嵌套关系
<html>
<head>
</head>
</html>
并列关系
<head>
</head>
<body>
</body>
注释用来帮助程序员记录程序设计方法,辅助程序阅读
双标签,定义网页的标题
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>百度一下,你就知道了</title>
</head>
<body>
</body>
</html>
是单标签,用来描述HTML网页文档的属性
属性值 | 说明 |
keywords | 网页关键字,多个逗号隔开 |
description | 网页描述 |
author | 作者 |
copyright | 版权信息 |
<!DOCTYPE html>
<html lang="en">
<head>
<!-- 网页关键字 -->
<meta name="keywords" content="html,css,javascript">
<!-- 网页描述 -->
<meta name="description" content="基础前端知识">
<!-- 网页作者 -->
<meta name="author" content="buddha">
<!-- 网页版权信息 -->
<meta name="copyright" content="版权所有,翻版必究">
</head>
<body>
</body>
</html>
标签属性:
1、标签的属性写在开始标签内部
2、标签名与属性之间要有空格隔开
3、一个标签可以同时存在多个属性
4、属性之间以空格隔开
5、属性没有先后顺序之分
属性值 | 说明 |
Content-Type | 定义网页所使用编码 |
refresh | 定义网页自动刷新跳转 |
<!DOCTYPE html>
<html lang="en">
<head>
<!-- 设置网页编码完整写法 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<!-- 设置网页编码简写写法 -->
<meta charset="UTF-8">
<!-- 网页打开3秒后跳去百度 -->
<meta http-equiv="refresh" content="3;url=https://www.baidu.com">
</head>
<body>
</body>
</html>
是双标签,用来定义标签的css样式
<!DOCTYPE html>
<html lang="en">
<head>
<style type="text/css">
/* css内联样式写这里 */
</style>
</head>
<body>
</body>
</html>
是单标签,是用来引入外部css样式文件
<link rel="stylesheet" href="css/index.css" type="text/css">
是双标签,是用来写JavaScript代码的地方
<!DOCTYPE html>
<html lang="en">
<head>
<script>
/* 这里写JavaScript代码 */
</script>
</head>
<body>
</body>
</html>
是单标签,是用来设置整个网页的基础路径。
<!DOCTYPE html>
<html lang="en">
<head>
<base href="https://pic.rmb.bdstatic.com">
</head>
<body>
<img src="bjh/news/e7fb4c2be6a2e439ff7e3197fa205d8f1336.gif">
</body>
</html>
开发中很少用到,有人使用知道就行
上面所述标签是放在head标签里的,接下来接触的标签都是放在body标签内的
是双标签,h是header的缩写
<h1>h1标签:一级标题</h1>
<h2>h2标签:二级标题</h2>
<h3>h3标签:三级标题</h3>
<h4>h4标签:四级标题</h4>
<h5>h5标签:五级标题</h5>
<h6>h6标签:六级标题</h6>
特点:
1、字体加粗
2、独占一行
3、从h1到h6,字体逐渐减小
4、使用<h>标签的主要意义是告诉搜索引擎这是一段文字的标题
5、<h1>在一个页面最多只能有一个,不要用多个
是双标签,p是paragraph的缩写
<p>这是一段文字</p>
<p>这是一段文字</p>
<p>这是一段文字</p>
特点:
1、独占一行
2、段落与段落之间,存在间隙
是单标签,br是break的缩写
<p>这是一段<br>文字</p>
特点:
1、强制换行
2、单标签
是单标签,hr是horizon地平线的缩写
<p>这是一段文字</p>
<hr>
<p>这是一段文字</p>
特点:
1、在页面中显示一条水平线
2、单标签
标签1 | 标签2 | 说明 |
b | strong | 加粗 |
u | ins | 下划线 |
i | em | 倾斜 |
s | del | 删除线 |
<b>这是一段文字</b>
<strong>这是一段文字</strong>
<br>
<u>这是一段文字</u>
<ins>这是一段文字</ins>
<br>
<i>这是一段文字</i>
<em>这是一段文字</em>
<br>
<s>这是一段文字</s>
<del>这是一段文字</del>
特点:
1、不会独占一行
2、推荐使用标签2所在列标签
sup是superscripted这个单词的缩写
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
a<sup>2</sup>
</body>
</html>
sub是subscripted这个单词的缩写
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
H<sub>2</sub>O
</body>
</html>
在网页中展示特殊符号效果时,需要使用字符实体替代
显示结果 | 描述 | 实体名称 |
空格 |
| |
< | 小于号 | < |
> | 大于号 | > |
& | 与 | & |
" | 双引号 | " |
x | 乘号 | × |
÷ | 除号 | ÷ |
- | 长破折号 | — |
| | 竖线 | | |
‘ | 左单引号 | ‘ |
‘ | 右单引号 | ’ |
© | 版权符 | © |
® | 注册商标 | ® |
™ | 商标 | ™ |
° | 度 | ° |
<img src="./001.jpg" alt="">
img标签常见属性:
属性名 | 说明 |
src | 图片路径(绝对路径、相对路径) |
alt | 图片加载失败时,显示的文字 |
title | 鼠标悬停时,显示的文字 |
width | 图片宽度 |
height | 图片高度 |
只设置宽或高,会自动等比缩放,宽高只需要数字,不需要'px'
<img src="./001.jpg" alt="图片加载失败" title="这是程序兔" width="200" height="200">
绝对路径:指目录下的绝对位置,比如从根目录开始的路径,或完整的网络地址
相对路径:从当前文件开始出发找目标文件的过程
<audio src="music.mp3" controls autoplay loop></audio>
audio标签常见属性:
属性名 | 说明 |
src | 音频路径 |
controls | 显示播放控件 |
autoplay | 自动播放 |
loop | 循环播放 |
支持mp3、wav、ogg三种音频格式
<video src="video.mp4" controls loop autoplay></video>
属性名 | 说明 |
src | 视频路径 |
controls | 显示播放控件 |
autoplay | 自动播放 |
loop | 循环播放 |
支持mp4、webm、ogg三种视频格式
超链接,是双标签,实现各个独立页面之间进行跳转,可以跳去站外也可以在站内之间跳转
<a href="链接地址">文本或图片</a>
站外跳转,采用绝对路径
<a href="http://www.baidu.com" target="_blank">百度</a>
站内跳转,采用相对路径
<!-- a页面 -->
<a href="b.html">跳去b页面</a>
<!-- b页面 -->
<p>b页面</p>
页面内跳转
<a href="#ms">美食</a>
<a href="#jd">景点</a>
<h3 id="ms">推荐美食</h3>
<!-- 省略n个br标签 -->
<br>
<h3 id="jd">推荐景点</h3>
属性名 | 说明 |
href | 跳转链接 |
target | 链接打开方式 |
target属性值
属性值 | 说明 |
_self | 默认,原窗口打开链接 |
_blank | 在新窗口打开链接 |
_parent | 在父窗口打开链接 |
_top | 在顶层窗口打开超链接 |
target属性值一般使用_self(默认)和_blank
<ul type="属性值">
<li>列表项</li>
<li>列表项</li>
<li>列表项</li>
</ul>
解释:
1、ul,unordered lists,无序列表,li,list item,列表项
2、ul标签子标签只允许是li标签
3、li标签可以包含任意内容
type属性值
属性值 | 说明 |
disc | 默认,实心圆 |
circle | 空心圆 |
square | 实心方型 |
<ul>
<li>你</li>
<li>我</li>
<li>他</li>
</ul>
<ol type="属性值">
<li>列表项</li>
<li>列表项</li>
<li>列表项</li>
</ol>
解释:
1、ol,ordered lists,有序列表,li,list item,列表项
2、ol标签子标签只允许是li标签
3、li标签可以包含任意内容
type属性值
属性值 | 说明 |
1 | 默认,阿拉伯数字,1,2,3...... |
a | 小写英文字母,a,b,c...... |
A | 大写英文字母,A,B,C...... |
i | 小写罗马数字,i,ii,iii...... |
I | 大写罗马数字,I,II,III...... |
<ol>
<li>你</li>
<li>我</li>
<li>他</li>
</ol>
<dl>
<dt>名词</dt>
<dd>描述</dd>
……
</dl>
解释:
1、dl,definition lists,自定义列表;dt,definition term,自定义列表组;dd,definition description,自定义列表描述
<dl>
<dt>称呼</dt>
<dd>你</dd>
<dd>我</dd>
<dd>他</dd>
</dl>
<table>
<tr>
<td>单元格1</td>
<td>单元格2</td>
</tr>
<tr>
<td>单元格3</td>
<td>单元格4</td>
</tr>
</table>
解释:
1、tr,table row,表格行;td,table data cell,表行单元格
<table>
<tr>
<td>1</td>
</tr>
</table>
属性名 | 属性值 | 描述 |
border | 数字 | 边框宽度 |
width | 数字 | 表格宽度 |
height | 数字 | 表格高度 |
<table border="1" width="200" height="50">
<tr>
<td>1</td>
</tr>
</table>
<caption>标题内容</caption>,位于表格内第一行
<table border="1" width="200" height="50">
<caption>数字</caption>
<tr>
<td>1</td>
</tr>
</table>
<th></th>,th,table header cell,表头单元格
<table border="1" width="200" height="50">
<caption>数字</caption>
<tr>
<th>序号</th>
</tr>
<tr>
<td>1</td>
</tr>
</table>
thead、tbody、tfoot
<table border="1" width="200" height="50">
<caption>数字</caption>
<thead>
<tr>
<th>序号</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>汇总</td>
</tr>
</tfoot>
</table>
属性名 | 属性值 | 说明 |
rowspan | 合并单元格个数 | 合并行,单元格垂直合并 |
colspan | 合并单元格个数 | 合并列,单元格水平合并 |
<td rowspan="跨越的行数"></td>
<td colspan="跨越的列数"></td>
<table border="1" width="200" height="50">
<caption>数字</caption>
<thead>
<tr>
<th>序号</th>
<th>金额</th>
<th>金额</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td rowspan="2">20</td>
<td rowspan="2">20</td>
</tr>
<tr>
<td>2</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>汇总</td>
<td colspan="2">40</td>
</tr>
</tfoot>
</table>
双标签,包裹其它表单标签
<form>
// 表单
</form>
form标签的常用属性
属性 | 说明 |
name | 表单名称 |
method | 提交方式 |
action | 提交地址 |
target | 打开方式 |
enctype | 编码方式 |
name属性
一个页面中,表单可能不止一个。name属性,用来区分不同的表单
<form name="myForm"></form>
method属性
用来指定表单数据使用哪种提交方式给后端
属性值 | 说明 |
get | get方式 |
post | post方式 |
<form method="get"></form>
action属性
用来指定表单数据提交到哪个地址
<!-- 比如提交到index.php地址 -->
<form action="index.php"></form>
target属性
该属性跟a标签的target属性一样,其属性值也是四个,一般情况只用到_blank属性值,默认也是这个值
<form target="_blank"></form>
enctype属性
属性值 | 说明 |
application/x-www-form-urlencoded | 在发送前编码所有字符(默认) |
multipart/form-data | 不对字符编码,在使用包含文件上传控件的表单时,必须使用该值 |
text/plain | 空格转换为 "+" 加号,但不对特殊字符编码 |
<form enctype="multipart/form-data"></form>
input是单标签
<input type="表单类型">
属性值 | 说明 |
text | 单行文本框 |
password | 密码文本框 |
radio | 单选框 |
checkbox | 多选框 |
button | 普通按钮 |
submit | 提交按钮 |
reset | 重置按钮 |
file | 文件上传 |
单行文本框常用属性
属性 | 说明 |
value | 设置文本框的默认值 |
size | 设置文本框的长度 |
maxlength | 设置最多可输入字符 |
<form>
<input type="text" value="默认值" size="长度" maxlength="可输入字符">
</form>
<form>
<label>姓名:<input type="text" value="曹操" size="20" maxlength="10"></label>
</form>
密码文本框常用属性
密码文本框常用属性和单行文本框常用属性相同
<input type="password" value="默认值" size="长度" maxlength="可输入字符">
<form>
<label>密码:<input type="password" value="12345678" size="20" maxlength="10"></label>
</form>
单选框
属性 | 说明 |
name | 组名,同组单选框,组名要相同,必要属性 |
value | 单选框选项取值,必要属性 |
checked | 默认选中项,同组单选框,可以有一个默认选中项 |
<input type="radio" name="组名" value="取值" checked="checked">
<form>
性别:
<input type="radio" name="sex" value="男" checked="checked">男
<input type="radio" name="sex" value="女">女
</form>
复选框
复选框和单选框的属性都相同,区别复选框可以多选
<form>
爱好:
<input type="checkbox" name="hobby" value="篮球" checked>篮球
<input type="checkbox" name="hobby" value="足球" checked>足球
<input type="checkbox" name="hobby" value="台球">台球
</form>
普通按钮
<input type="button" value="取值">
<form>
<input type="button" value="普通按钮">
</form>
<button>普通按钮</button>
区别:
1、input是单标签,button是双标签
2、button标签的信息除了文本,还可以是图像、其它标签等
3、button有type属性,属性值可以是button、submit、reset等
提交按钮
<input type="submit" value="取值">
<form>
<input type="submit" value="提交">
</form>
把对应表单数据提交给后端服务器
重置按钮
<input type="reset" value="取值">
<form>
<input type="reset" value="重置">
</form>
点击重置后,所在form表单里所有内容被清空了
文件上传
<input type="file">
<form>
<input type="file">
</form>
<textarea name="文本名称" cols="列数" rows="行数"></textarea>
<form>
<textarea name="文本名称" cols="1" rows="2"></textarea>
</form>
<select>
<option>选项内容</option>
<option>选项内容</option>
</select>
下拉列表标签是为了节省页面空间
select标签属性
属性名 | 说明 |
name | 数据提交后端所需字段 |
size | 下拉选项显示个数 |
multiple | 默认只允许选一个,选多个得加这个属性 |
disabled | 所有下拉选项禁止选中 |
<form>
<select name="age" size="4" multiple="multiple" disabled="disabled">
<option>18岁以下</option>
<option>18-28岁</option>
<option>28-38岁</option>
<option>38岁以上</option>
</select>
</form>
option标签属性
属性名 | 说明 |
selected | 默认选中 |
value | 被选中,数据提交后端的值 |
disabled | 该下拉选项禁止选中 |
<form>
<select name="age" size="5">
<option value="1">18岁以下</option>
<option value="2" disabled="disabled">18-28岁</option>
<option value="3" selected="selected">28-38岁</option>
<option value="4">38岁以上</option>
</select>
</form>
<iframe src="URL" width="数值" height="数值"></iframe>
框架标签常用属性
属性名 | 说明 |
src | 嵌入的文档地址 |
width | 标签宽度 |
height | 标签高度 |
<iframe src="https://www.bilibili.com" width="300" height="200"></iframe>
有些文档禁止被嵌入
HTML标签分为三种,行内标签、块级标签和行内块级标签。
特点:
1、在页面内只占据刚好能包裹自己内容的空间
2、没有宽高,内容多大就多大,行内标签不能嵌套块级标签(a标签除外)
3、行内标签设置宽高无效,可以对行高line-height进行设置
4、可以设置外边距margin和内边距padding,但只对左右边距有效果,上下无效
5、常见行内标签span、a、strong、ins、del、br等
span标签
双标签,行内标签,本身没有固定样式
<p>我是<span>中国人</span></p>
特点:
1、独占一行
2、高度、宽度、外边距、内边距都可以设置生效
3、宽度默认是父级宽度的100%
4、是一个容器盒子,可以嵌套多层子级行内标签、块级标签,文本类块级标签除外
5、常见块级标签div、p、h1~h6、ol、ul、li等
div标签
双标签,块级标签,本身没有固定样式
<!-- 头部区域 -->
<div></div>
<!-- 内容区域 -->
<div></div>
特点
1、在页面内只占据刚好能包裹自己内容的空间
2、高度、宽度、外边距、内边距都可以设置生效
3、常见块级标签img、input、td
通过css样式display属性转换,这是css的内容
语义化是指根据内容的结构化(内容语义化),选择合适的标签(代 码语义化)。通俗来讲就是用正确的标签做正确的事情。
语义化的优点如下:
对机器友好,带有语义的文字表现力丰富,更适合搜索引擎的爬虫爬 取有效信息,有利于 SEO。除此之外,语义类还支持读屏软件,根据 文章可以自动生成目录;
对开发者友好,使用语义类标签增强了可读性,结构更加清晰,开发 者能清晰地看出网页的结构,便于团队的开发与维护。
常见的语义化标签:
DOCTYPE 是 HTML5 中一种标准通用标记语言的文档类型声明,它的目 的是告诉浏览器(解析器)应该以什么样(html 或 xhtml)的文档类 行定义来解析文档,不同的渲染模式会影响浏览器对 CSS 代码甚⾄JavaScript 脚本的解析。它必须声明在 HTML⽂档的第⼀⾏。
浏览器渲染页面的两种模式(可通过 document.compatMode 获取,比 如,语雀官网的文档类型是 CSS1Compat):
CSS1Compat:标准模式(Strick mode),默认模式,浏览器使用 W3C 的标准解析渲染页面。在标准模式中,浏览器以其支持的最高标准呈 现页面。
BackCompat:怪异模式(混杂模式)(Quick mode),浏览器使用自己的 怪异模式解析渲染页面。在怪异模式中,页面以一种比较宽松的向后 兼容的方式显示。
如果没有 defer 或 async 属性,浏览器会立即加载并执行相应的脚本。它不会等待后续加载的文档元素,读取到就会开始加载和执行,这样 就阻塞了后续文档的加载。
下图可以直观地看出三者之间的区别:
其中蓝色代表 js 脚本网络加载时间,红色代表 js 脚本执行时间,绿 色代表 html 解析。
defer 和 async 属性都是去异步加载外部的 JS 脚本文件,它们都不 会阻塞页面的解析,其区别如下:
执行顺序:多个带 async 属性的标签,不能保证加载的顺序;多个带 defer 属性的标签,按照加载顺序执行;
脚本是否并行执行:async 属性,表示后续文档的加载和执行与 js 脚本的加载和执行是并行进行的,即异步执行;defer 属性,加载后 续文档的过程和 js 脚本的加载(此时仅加载不执行)是并行进行的(异步),js 脚本需要等到文档所有元素解析完成之后才执行,DOMContentLoaded 事件触发执行之前。
行内元素有:a b span img input select strong;
块级元素有:div ul ol li dl dt dd h1 h2 h3 h4 h5 h6 p;
空元素,即没有内容的 HTML 元素。空元素是在开始标签中关闭的,也就是空元素没有闭合标签:
常见的有:<br>、<hr>、<img>、<input>、<link>、<meta>;
鲜见的有:<area>、<base>、<col>、<colgroup>、<command>、<embed>、<keygen>、<param>、<source>、<track>、<wbr>。
在线的情况下,浏览器发现 html 头部有 manifest 属性,它会请求 manifest 文件,如果是第一次访问页面 ,那么浏览器就会根据 manifest 文件的内容下载相应的资源并且进行离线存储。如果已经 访问过页面并且资源已经进行离线存储了,那么浏览器就会使用离线 的资源加载页面,然后浏览器会对比新的 manifest 文件与旧的 manifest 文件,如果文件没有发生改变,就不做任何操作,如果文 件改变了,就会重新下载文件中的资源并进行离线存储。
离线的情况下,浏览器会直接使用离线存储的资源。
(1)SVG:
SVG 可缩放矢量图形(Scalable Vector Graphics)是基于可扩展标 记语言 XML 描述的 2D 图形的语言,SVG 基于 XML 就意味着 SVG DOM 中的每个元素都是可用的,可以为某个元素附加 Javascript 事件处 理器。在 SVG 中,每个被绘制的图形均被视为对象。如果 SVG 对象 的属性发生变化,那么浏览器能够自动重现图形。
其特点如下:
不依赖分辨率
支持事件处理器
最适合带有大型渲染区域的应用程序(比如谷歌地图)
复杂度高会减慢渲染速度(任何过度使用 DOM 的应用都不快)不适合游戏应用
(2)Canvas:
Canvas 是画布,通过 Javascript 来绘制 2D 图形,是逐像素进行渲 染的。其位置发生改变,就会重新进行绘制。
其特点如下:
依赖分辨率
不支持事件处理器
弱的文本渲染能力
能够以 .png 或 .jpg 格式保存结果图像
最适合图像密集型的游戏,其中的许多对象会被频繁重绘
注:矢量图,也称为面向对象的图像或绘图图像,在数学上定义为一 系列由线连接的点。矢量文件中的图形元素称为对象。每个对象都是
一个自成一体的实体,它具有颜色、形状、轮廓、大小和屏幕位置等 属性。
dragstart:事件主体是被拖放元素,在开始拖放被拖放元素时触发。
darg:事件主体是被拖放元素,在正在拖放被拖放元素时触发。dragenter:事件主体是目标元素,在被拖放元素进入某元素时触发。dragover:事件主体是目标元素,在被拖放在某元素内移动时触发。dragleave:事件主体是目标元素,在被拖放元素移出目标元素是触 发。
drop:事件主体是目标元素,在目标元素完全接受被拖放元素时触发。
dragend:事件主体是被拖放元素,在整个拖放操作结束时触发。
译自: https://medium.freecodecamp.org/for-your-first-html-code-lets-help-batman-write-a-love-letter-64c203b9360b
作者: Kunal Sarkar
译者: MjSeven
在一个美好的夜晚,你的肚子拒绝消化你在晚餐吃的大块披萨,所以你不得不在睡梦中冲进洗手间。
在浴室里,当你在思考为什么会发生这种情况时,你听到一个来自通风口的低沉声音:“嘿,我是蝙蝠侠。”
这时,你会怎么做呢?
在你恐慌并处于关键时刻之前,蝙蝠侠说:“我需要你的帮助。我是一个超级极客,但我不懂 HTML。我需要用 HTML 写一封情书,你愿意帮助我吗?”
谁会拒绝蝙蝠侠的请求呢,对吧?所以让我们用 HTML 来写一封蝙蝠侠的情书。
HTML 网页与你电脑上的其它文件一样。就同一个 .doc 文件以 MS Word 打开,.jpg 文件在图像查看器中打开一样,一个 .html 文件在浏览器中打开。
那么,让我们来创建一个 .html 文件。你可以在 Notepad 或其它任何编辑器中完成此任务,但我建议使用 VS Code。 在这里下载并安装 VS Code 。它是免费的,也是我唯一喜欢的微软产品。
在系统中创建一个目录,将其命名为 “HTML Practice”(不带引号)。在这个目录中,再创建一个名为 “Batman’s Love Letter”(不带引号)的目录,这将是我们的项目根目录。这意味着我们所有与这个项目相关的文件都会在这里。
打开 VS Code,按下 ctrl+n 创建一个新文件,按下 ctrl+s 保存文件。切换到 “Batman’s Love Letter” 文件夹并将其命名为 “loveletter.html”,然后单击保存。
现在,如果你在文件资源管理器中双击它,它将在你的默认浏览器中打开。我建议使用 Firefox 来进行 web 开发,但 Chrome 也可以。
让我们将这个过程与我们已经熟悉的东西联系起来。还记得你第一次拿到电脑吗?我做的第一件事是打开 MS Paint 并绘制一些东西。你在 Paint 中绘制一些东西并将其另存为图像,然后你可以在图像查看器中查看该图像。之后,如果要再次编辑该图像,你在 Paint 中重新打开它,编辑并保存它。
我们目前的流程非常相似。正如我们使用 Paint 创建和编辑图像一样,我们使用 VS Code 来创建和编辑 HTML 文件。就像我们使用图像查看器查看图像一样,我们使用浏览器来查看我们的 HTML 页面。
我们有一个空的 HTML 文件,以下是蝙蝠侠想在他的情书中写的第一段。
“After all the battles we fought together, after all the difficult times we saw together, and after all the good and bad moments we’ve been through, I think it’s time I let you know how I feel about you.”
复制这些到 VS Code 中的 loveletter.html。单击 “View -> Toggle Word Wrap (alt+z)” 自动换行。
保存并在浏览器中打开它。如果它已经打开,单击浏览器中的刷新按钮。
瞧!那是你的第一个网页!
我们的第一段已准备就绪,但这不是在 HTML 中编写段落的推荐方法。我们有一种特定的方法让浏览器知道一个文本是一个段落。
如果你用 <p> 和 </p> 来包裹文本,那么浏览器将识别 <p> 和 </p> 中的文本是一个段落。我们这样做:
<p>After all the battles we fought together, after all the difficult times we saw together, after all the good and bad moments we've been through, I think it's time I let you know how I feel about you.</p>
通过在 <p> 和 </p>中编写段落,你创建了一个 HTML 元素。一个网页就是 HTML 元素的集合。
让我们首先来认识一些术语:<p> 是开始标签,</p> 是结束标签,“p” 是标签名称。元素开始和结束标签之间的文本是元素的内容。
在上面,你将看到文本覆盖屏幕的整个宽度。
我们不希望这样。没有人想要阅读这么长的行。让我们设定段落宽度为 550px。
我们可以通过使用元素的 style 属性来实现。你可以在其 style 属性中定义元素的样式(例如,在我们的示例中为宽度)。以下行将在 p 元素上创建一个空样式属性:
<p style="">...</p>
你看到那个空的 "" 了吗?这就是我们定义元素外观的地方。现在我们要将宽度设置为 550px。我们这样做:
<p style="width:550px;">
After all the battles we fought together, after all the difficult times we saw together, after all the good and bad moments we've been through, I think it's time I let you know how I feel about you.
</p>
我们将 width 属性设置为 550px,用冒号 : 分隔,以分号 ; 结束。
另外,注意我们如何将 <p> 和 </p> 放在单独的行中,文本内容用一个制表符缩进。像这样设置代码使其更具可读性。
接下来,蝙蝠侠希望列出他所钦佩的人的一些优点,例如:
You complete my darkness with your light. I love:
- the way you see good in the worst things
- the way you handle emotionally difficult situations
- the way you look at Justice
I have learned a lot from you. You have occupied a special place in my heart over time.
这看起来很简单。
让我们继续,在 </p> 下面复制所需的文本:
<p style="width:550px;">
After all the battles we faught together, after all the difficult times we saw together, after all the good and bad moments we've been through, I think it's time I let you know how I feel about you.
</p>
<p style="width:550px;">
You complete my darkness with your light. I love:
- the way you see good in the worse
- the way you handle emotionally difficult situations
- the way you look at Justice
I have learned a lot from you. You have occupied a special place in my heart over the time.
</p>
保存并刷新浏览器。
哇!这里发生了什么,我们的列表在哪里?
如果你仔细观察,你会发现没有显示换行符。在代码中我们在新的一行中编写列表项,但这些项在浏览器中显示在一行中。
如果你想在 HTML(新行)中插入换行符,你必须使用 <br>。让我们来使用 <br>,看看它长什么样:
<p style="width:550px;">
After all the battles we faught together, after all the difficult times we saw together, after all the good and bad moments we've been through, I think it's time I let you know how I feel about you.
</p>
<p style="width:550px;">
You complete my darkness with your light. I love: <br>
- the way you see good in the worse <br>
- the way you handle emotionally difficult situations <br>
- the way you look at Justice <br>
I have learned a lot from you. You have occupied a special place in my heart over the time.
</p>
保存并刷新:
好的,现在它看起来就像我们想要的那样!
另外,注意我们没有写一个 </br>。有些标签不需要结束标签(它们被称为自闭合标签)。
还有一件事:我们没有在两个段落之间使用 <br>,但第二个段落仍然是从一个新行开始,这是因为 <p> 元素会自动插入换行符。
我们使用纯文本编写列表,但是有两个标签可以供我们使用来达到相同的目的:<ul> and <li>。
让我们解释一下名字的意思:ul 代表 无序列表(Unordered List),li 代表 列表项目(List Item)。让我们使用它们来展示我们的列表:
<p style="width:550px;">
After all the battles we faught together, after all the difficult times we saw together, after all the good and bad moments we've been through, I think it's time I let you know how I feel about you.
</p>
<p style="width:550px;">
You complete my darkness with your light. I love:
<ul>
<li>the way you see good in the worse</li>
<li>the way you handle emotionally difficult situations</li>
<li>the way you look at Justice</li>
</ul>
I have learned a lot from you. You have occupied a special place in my heart over the time.
</p>
在复制代码之前,注意差异部分:
让我们保存文件并刷新浏览器以查看结果:
你会立即注意到在每个列表项之前显示了重点标志。我们现在不需要在每个列表项之前写 “-”。
经过仔细检查,你会注意到最后一行超出 550px 宽度。这是为什么?因为 HTML 不允许 <ul> 元素出现在 <p> 元素中。让我们将第一行和最后一行放在单独的 <p> 元素中:
<p style="width:550px;">
After all the battles we faught together, after all the difficult times we saw together, after all the good and bad moments we've been through, I think it's time I let you know how I feel about you.
</p>
<p style="width:550px;">
You complete my darkness with your light. I love:
</p>
<ul style="width:550px;">
<li>the way you see good in the worse</li>
<li>the way you handle emotionally difficult situations</li>
<li>the way you look at Justice</li>
</ul>
<p style="width:550px;">
I have learned a lot from you. You have occupied a special place in my heart over the time.
</p>
保存并刷新。
注意,这次我们还定义了 <ul> 元素的宽度。那是因为我们现在已经将 <ul> 元素放在了 <p> 元素之外。
定义情书中所有元素的宽度会变得很麻烦。我们有一个特定的元素用于此目的:<div> 元素。一个 <div> 元素就是一个通用容器,用于对内容进行分组,以便轻松设置样式。
让我们用 <div> 元素包装整个情书,并为其赋予宽度:550px 。
<div style="width:550px;">
<p>
After all the battles we faught together, after all the difficult times we saw together, after all the good and bad moments we've been through, I think it's time I let you know how I feel about you.
</p>
<p>
You complete my darkness with your light. I love:
</p>
<ul>
<li>the way you see good in the worse</li>
<li>the way you handle emotionally difficult situations</li>
<li>the way you look at Justice</li>
</ul>
<p>
I have learned a lot from you. You have occupied a special place in my heart over the time.
</p>
</div>
棒极了,我们的代码现在看起来简洁多了。
到目前为止,蝙蝠侠对结果很高兴,他希望在情书上标题。他想写一个标题: “Bat Letter”。当然,你已经看到这个名字了,不是吗?:D
你可以使用 <h1>、<h2>、<h3>、<h4>、<h5> 和 <h6> 标签来添加标题,<h1> 是最大的标题和最主要的标题,<h6> 是最小的标题。
让我们在第二段之前使用 <h1> 做主标题和一个副标题:
<div style="width:550px;">
<h1>Bat Letter</h1>
<p>
After all the battles we faught together, after all the difficult times we saw together, after all the good and bad moments we've been through, I think it's time I let you know how I feel about you.
</p>
<h2>You are the light of my life</h2>
<p>
You complete my darkness with your light. I love:
</p>
<ul>
<li>the way you see good in the worse</li>
<li>the way you handle emotionally difficult situations</li>
<li>the way you look at Justice</li>
</ul>
<p>
I have learned a lot from you. You have occupied a special place in my heart over the time.
</p>
</div>
保存,刷新。
我们的情书尚未完成,但在继续之前,缺少一件大事:蝙蝠侠标志。你见过是蝙蝠侠的东西但没有蝙蝠侠的标志吗?
并没有。
所以,让我们在情书中添加一个蝙蝠侠标志。
在 HTML 中包含图像就像在一个 Word 文件中包含图像一样。在 MS Word 中,你到 “菜单 -> 插入 -> 图像 -> 然后导航到图像位置为止 -> 选择图像 -> 单击插入”。
在 HTML 中,我们使用 <img> 标签让浏览器知道我们需要加载的图像,而不是单击菜单。我们在 src 属性中写入文件的位置和名称。如果图像在项目根目录中,我们可以简单地在 src 属性中写入图像文件的名称。
在我们深入编码之前,从 这里 下载蝙蝠侠标志。你可能希望裁剪图像中的额外空白区域。复制项目根目录中的图像并将其重命名为 “bat-logo.jpeg”。
<div style="width:550px;">
<h1>Bat Letter</h1>
<img src="bat-logo.jpeg">
<p>
After all the battles we faught together, after all the difficult times we saw together, after all the good and bad moments we've been through, I think it's time I let you know how I feel about you.
</p>
<h2>You are the light of my life</h2>
<p>
You complete my darkness with your light. I love:
</p>
<ul>
<li>the way you see good in the worse</li>
<li>the way you handle emotionally difficult situations</li>
<li>the way you look at Justice</li>
</ul>
<p>
I have learned a lot from you. You have occupied a special place in my heart over the time.
</p>
</div>
我们在第 3 行包含了 <img> 标签。这个标签也是一个自闭合的标签,所以我们不需要写 </img>。在 src 属性中,我们给出了图像文件的名称。这个名称应与图像名称完全相同,包括扩展名(.jpeg)及其大小写。
保存并刷新,查看结果。
该死的!刚刚发生了什么?
当使用 <img> 标签包含图像时,默认情况下,图像将以其原始分辨率显示。在我们的例子中,图像比 550px 宽得多。让我们使用 style 属性定义它的宽度:
<div style="width:550px;">
<h1>Bat Letter</h1>
<img src="bat-logo.jpeg" style="width:100%">
<p>
After all the battles we faught together, after all the difficult times we saw together, after all the good and bad moments we've been through, I think it's time I let you know how I feel about you.
</p>
<h2>You are the light of my life</h2>
<p>
You complete my darkness with your light. I love:
</p>
<ul>
<li>the way you see good in the worse</li>
<li>the way you handle emotionally difficult situations</li>
<li>the way you look at Justice</li>
</ul>
<p>
I have learned a lot from you. You have occupied a special place in my heart over the time.
</p>
</div>
你会注意到,这次我们定义宽度使用了 “%” 而不是 “px”。当我们在 “%” 中定义宽度时,它将占据父元素宽度的百分比。因此,100% 的 550px 将为我们提供 550px。
保存并刷新,查看结果。
太棒了!这让蝙蝠侠的脸露出了羞涩的微笑 :)。
现在蝙蝠侠想在最后几段中承认他的爱。他有以下文本供你用 HTML 编写:
“I have a confession to make
It feels like my chest does have a heart. You make my heart beat. Your smile brings a smile to my face, your pain brings pain to my heart.
I don’t show my emotions, but I think this man behind the mask is falling for you.”
当阅读到这里时,你会问蝙蝠侠:“等等,这是给谁的?”蝙蝠侠说:
“这是给超人的。”
你说:哦!我还以为是给神奇女侠的呢。
蝙蝠侠说:不,这是给超人的,请在最后写上 “I love you Superman.”。
好的,我们来写:
<div style="width:550px;">
<h1>Bat Letter</h1>
<img src="bat-logo.jpeg" style="width:100%">
<p>
After all the battles we faught together, after all the difficult times we saw together, after all the good and bad moments we've been through, I think it's time I let you know how I feel about you.
</p>
<h2>You are the light of my life</h2>
<p>
You complete my darkness with your light. I love:
</p>
<ul>
<li>the way you see good in the worse</li>
<li>the way you handle emotionally difficult situations</li>
<li>the way you look at Justice</li>
</ul>
<p>
I have learned a lot from you. You have occupied a special place in my heart over the time.
</p>
<h2>I have a confession to make</h2>
<p>
It feels like my chest does have a heart. You make my heart beat. Your smile brings smile on my face, your pain brings pain to my heart.
</p>
<p>
I don't show my emotions, but I think this man behind the mask is falling for you.
</p>
<p>I love you Superman.</p>
<p>
Your not-so-secret-lover, <br>
Batman
</p>
</div>
这封信差不多完成了,蝙蝠侠另外想再做两次改变。蝙蝠侠希望在最后段落的第一句中的 “does” 一词是斜体,而 “I love you Superman” 这句话是粗体的。
我们使用 <em> 和 <strong> 以斜体和粗体显示文本。让我们来更新这些更改:
<div style="width:550px;">
<h1>Bat Letter</h1>
<img src="bat-logo.jpeg" style="width:100%">
<p>
After all the battles we faught together, after all the difficult times we saw together, after all the good and bad moments we've been through, I think it's time I let you know how I feel about you.
</p>
<h2>You are the light of my life</h2>
<p>
You complete my darkness with your light. I love:
</p>
<ul>
<li>the way you see good in the worse</li>
<li>the way you handle emotionally difficult situations</li>
<li>the way you look at Justice</li>
</ul>
<p>
I have learned a lot from you. You have occupied a special place in my heart over the time.
</p>
<h2>I have a confession to make</h2>
<p>
It feels like my chest <em>does</em> have a heart. You make my heart beat. Your smile brings smile on my face, your pain brings pain to my heart.
</p>
<p>
I don't show my emotions, but I think this man behind the mask is falling for you.
</p>
<p><strong>I love you Superman.</strong></p>
<p>
Your not-so-secret-lover, <br>
Batman
</p>
</div>
你可以通过三种方式设置样式或定义 HTML 元素的外观:
让我们来看看如何定义 <div> 的内联样式:
<div style="width:550px;">
我们可以在 <style> 和 </style> 里面写同样的样式:
div{
width:550px;
}
在嵌入式样式中,我们编写的样式是与元素分开的。所以我们需要一种方法来关联元素及其样式。第一个单词 “div” 就做了这样的活。它让浏览器知道花括号 {...} 里面的所有样式都属于 “div” 元素。由于这种语法确定要应用样式的元素,因此它称为一个选择器。
我们编写样式的方式保持不变:属性(width)和值(550px)用冒号(:)分隔,以分号(;)结束。
让我们从 <div> 和 <img> 元素中删除内联样式,将其写入 <style> 元素:
<style>
div{
width:550px;
}
img{
width:100%;
}
</style>
<div>
<h1>Bat Letter</h1>
<img src="bat-logo.jpeg">
<p>
After all the battles we faught together, after all the difficult times we saw together, after all the good and bad moments we've been through, I think it's time I let you know how I feel about you.
</p>
<h2>You are the light of my life</h2>
<p>
You complete my darkness with your light. I love:
</p>
<ul>
<li>the way you see good in the worse</li>
<li>the way you handle emotionally difficult situations</li>
<li>the way you look at Justice</li>
</ul>
<p>
I have learned a lot from you. You have occupied a special place in my heart over the time.
</p>
<h2>I have a confession to make</h2>
<p>
It feels like my chest <em>does</em> have a heart. You make my heart beat. Your smile brings smile on my face, your pain brings pain to my heart.
</p>
<p>
I don't show my emotions, but I think this man behind the mask is falling for you.
</p>
<p><strong>I love you Superman.</strong></p>
<p>
Your not-so-secret-lover, <br>
Batman
</p>
</div>
保存并刷新,结果应保持不变。
但是有一个大问题,如果我们的 HTML 文件中有多个 <div> 和 <img> 元素该怎么办?这样我们在 <style> 元素中为 div 和 img 定义的样式就会应用于页面上的每个 div 和 img。
如果你在以后的代码中添加另一个 div,那么该 div 也将变为 550px 宽。我们并不希望这样。
我们想要将我们的样式应用于现在正在使用的特定 div 和 img。为此,我们需要为 div 和 img 元素提供唯一的 id。以下是使用 id 属性为元素赋予 id 的方法:
<div id="letter-container">
以下是如何在嵌入式样式中将此 id 用作选择器:
#letter-container{
...
}
注意 # 符号。它表示它是一个 id,{...} 中的样式应该只应用于具有该特定 id 的元素。
让我们来应用它:
<style>
#letter-container{
width:550px;
}
#header-bat-logo{
width:100%;
}
</style>
<div id="letter-container">
<h1>Bat Letter</h1>
<img id="header-bat-logo" src="bat-logo.jpeg">
<p>
After all the battles we faught together, after all the difficult times we saw together, after all the good and bad moments we've been through, I think it's time I let you know how I feel about you.
</p>
<h2>You are the light of my life</h2>
<p>
You complete my darkness with your light. I love:
</p>
<ul>
<li>the way you see good in the worse</li>
<li>the way you handle emotionally difficult situations</li>
<li>the way you look at Justice</li>
</ul>
<p>
I have learned a lot from you. You have occupied a special place in my heart over the time.
</p>
<h2>I have a confession to make</h2>
<p>
It feels like my chest <em>does</em> have a heart. You make my heart beat. Your smile brings smile on my face, your pain brings pain to my heart.
</p>
<p>
I don't show my emotions, but I think this man behind the mask is falling for you.
</p>
<p><strong>I love you Superman.</strong></p>
<p>
Your not-so-secret-lover, <br>
Batman
</p>
</div>
HTML 已经准备好了嵌入式样式。
但是,你可以看到,随着我们包含越来越多的样式,<style></style> 将变得很大。这可能很快会混乱我们的主 HTML 文件。
因此,让我们更进一步,通过将 <style> 标签内的内容复制到一个新文件来使用链接样式。
在项目根目录中创建一个新文件,将其另存为 “style.css”:
#letter-container{
width:550px;
}
#header-bat-logo{
width:100%;
}
我们不需要在 CSS 文件中写 <style> 和 </style>。
我们需要使用 HTML 文件中的 <link> 标签来将新创建的 CSS 文件链接到 HTML 文件。以下是我们如何做到这一点:
<link rel="stylesheet" type="text/css" href="style.css">
我们使用 <link> 元素在 HTML 文档中包含外部资源,它主要用于链接样式表。我们使用的三个属性是:
link 元素的结尾没有 </link>。因此,<link> 也是一个自闭合的标签。
<link rel="gf" type="cute" href="girl.next.door">
如果只是得到一个女朋友,那么很容易:D
可惜没有那么简单,让我们继续前进。
这是我们 “loveletter.html” 的内容:
<link rel="stylesheet" type="text/css" href="style.css">
<div id="letter-container">
<h1>Bat Letter</h1>
<img id="header-bat-logo" src="bat-logo.jpeg">
<p>
After all the battles we faught together, after all the difficult times we saw together, after all the good and bad moments we've been through, I think it's time I let you know how I feel about you.
</p>
<h2>You are the light of my life</h2>
<p>
You complete my darkness with your light. I love:
</p>
<ul>
<li>the way you see good in the worse</li>
<li>the way you handle emotionally difficult situations</li>
<li>the way you look at Justice</li>
</ul>
<p>
I have learned a lot from you. You have occupied a special place in my heart over the time.
</p>
<h2>I have a confession to make</h2>
<p>
It feels like my chest <em>does</em> have a heart. You make my heart beat. Your smile brings smile on my face, your pain brings pain to my heart.
</p>
<p>
I don't show my emotions, but I think this man behind the mask is falling for you.
</p>
<p><strong>I love you Superman.</strong></p>
<p>
Your not-so-secret-lover, <br>
Batman
</p>
</div>
“style.css” 内容:
#letter-container{
width:550px;
}
#header-bat-logo{
width:100%;
}
保存文件并刷新,浏览器中的输出应保持不变。
我们的情书已经准备好给蝙蝠侠,但还有一些正式的片段。
与其他任何编程语言一样,HTML 自出生以来(1990 年)经历过许多版本,当前版本是 HTML5。
那么,浏览器如何知道你使用哪个版本的 HTML 来编写页面呢?要告诉浏览器你正在使用 HTML5,你需要在页面顶部包含 <!DOCTYPE html>。对于旧版本的 HTML,这行不同,但你不需要了解它们,因为我们不再使用它们了。
此外,在之前的 HTML 版本中,我们曾经将整个文档封装在 <html></html> 标签内。整个文件分为两个主要部分:头部在 <head></head> 里面,主体在 <body></body> 里面。这在 HTML5 中不是必须的,但由于兼容性原因,我们仍然这样做。让我们用 <Doctype>, <html>、 <head> 和 <body> 更新我们的代码:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="letter-container">
<h1>Bat Letter</h1>
<img id="header-bat-logo" src="bat-logo.jpeg">
<p>
After all the battles we faught together, after all the difficult times we saw together, after all the good and bad moments we've been through, I think it's time I let you know how I feel about you.
</p>
<h2>You are the light of my life</h2>
<p>
You complete my darkness with your light. I love:
</p>
<ul>
<li>the way you see good in the worse</li>
<li>the way you handle emotionally difficult situations</li>
<li>the way you look at Justice</li>
</ul>
<p>
I have learned a lot from you. You have occupied a special place in my heart over the time.
</p>
<h2>I have a confession to make</h2>
<p>
It feels like my chest <em>does</em> have a heart. You make my heart beat. Your smile brings smile on my face, your pain brings pain to my heart.
</p>
<p>
I don't show my emotions, but I think this man behind the mask is falling for you.
</p>
<p><strong>I love you Superman.</strong></p>
<p>
Your not-so-secret-lover, <br>
Batman
</p>
</div>
</body>
</html>
主要内容在 <body> 里面,元信息在 <head> 里面。所以我们把 <div> 保存在 <body> 里面并加载 <head> 里面的样式表。
保存并刷新,你的 HTML 页面应显示与之前相同的内容。
我发誓,这是最后一次改变。
你可能已经注意到选项卡的标题正在显示 HTML 文件的路径:
我们可以使用 <title> 标签来定义 HTML 文件的标题。标题标签也像链接标签一样在 <head> 内部。让我们我们在标题中加上 “Bat Letter”:
<!DOCTYPE html>
<html>
<head>
<title>Bat Letter</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="letter-container">
<h1>Bat Letter</h1>
<img id="header-bat-logo" src="bat-logo.jpeg">
<p>
After all the battles we faught together, after all the difficult times we saw together, after all the good and bad moments we've been through, I think it's time I let you know how I feel about you.
</p>
<h2>You are the light of my life</h2>
<p>
You complete my darkness with your light. I love:
</p>
<ul>
<li>the way you see good in the worse</li>
<li>the way you handle emotionally difficult situations</li>
<li>the way you look at Justice</li>
</ul>
<p>
I have learned a lot from you. You have occupied a special place in my heart over the time.
</p>
<h2>I have a confession to make</h2>
<p>
It feels like my chest <em>does</em> have a heart. You make my heart beat. Your smile brings smile on my face, your pain brings pain to my heart.
</p>
<p>
I don't show my emotions, but I think this man behind the mask is falling for you.
</p>
<p><strong>I love you Superman.</strong></p>
<p>
Your not-so-secret-lover, <br>
Batman
</p>
</div>
</body>
</html>
保存并刷新,你将看到在选项卡上显示的是 “Bat Letter” 而不是文件路径。
蝙蝠侠的情书现在已经完成。
恭喜!你用 HTML 制作了蝙蝠侠的情书。
我们学习了以下新概念:
我们学习了以下 HTML 标签:
我们学习了以下 CSS 属性:
朋友们,这就是今天的全部了,下一个教程中见。
作者简介:开发者 + 作者 | supersarkar.com | twitter.com/supersarkar
via: https://medium.freecodecamp.org/for-your-first-html-code-lets-help-batman-write-a-love-letter-64c203b9360b
作者: Kunal Sarkar 译者: MjSeven 校对: wxy
本文由 LCTT 原创编译, Linux中国 荣誉推出
*请认真填写需求信息,我们会在24小时内与您取得联系。