层叠样式表 (Cascading Style Sheets).
CSS 能够对网页中元素位置的排版进行像素级精确控制, 实现美化页面的效果. 能够做到页面的样式和结构分离.
CSS 就是 “东方四大邪术” 之化妆术.
选择器 + {一条/N条声明}
<style>
p {
/* 设置字体颜色 */
color: red;
/* 设置字体大小 */
font-size: 30px;
}
style>
<p>hellop>

注意:
- CSS 要写到 style 标签中(后面还会介绍其他写法)
- style 标签可以放到页面任意位置. 一般放到 head 标签内.
- CSS 使用 /* */ 作为注释. (使用 ctrl + / 快速切换) .
写在 style 标签中. 嵌入到 html 内部.
理论上来说 style 放到 html 的哪里都行. 但是一般都是放到 head 标签中.
优点: 这样做能够让样式和页面结构分离.
缺点: 分离的还不够彻底. 尤其是 css 内容多的时候
前面写的代码主要都是使用了这种方式. 实际开发中不常用.
PS: 搜狗搜索中仍然保留着这种写法.
行内表达式优先级比内部表达式优先级高
通过 style 属性, 来指定某个标签的样式.
只适合于写简单样式. 只针对某个标签生效.
缺点: 不能写太复杂的样式.
这种写法优先级较高, 会覆盖其他的样式.
(可以理解为就近原则)
<style>
div{
color: red;
}
style>
<div style="color: green">
hello world
div>


可以看到, red 颜色被覆盖了.
实际开发中最常用的方式.
<link rel="stylesheet" href="[CSS文件路径]">
创建 html文件
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<link rel="stylesheet" href="./style.css">
head>
<body>
<div>hello worlddiv>
body>
html>
创建 style.css
div {
color: red;
}
注意: 不要忘记 link 标签调用 CSS, 否则不生效.
优点: 样式和结构彻底分离了.
缺点: 受到浏览器缓存影响, 修改之后 不一定 立刻生效.
关于缓存:
这是计算机中一种常见的提升性能的技术手段.
网页依赖的资源(图片/CSS/JS等)通常是从服务器上获取的. 如果频繁访问该网站, 那么这些外部资源就没必要反复从服务器获取. 就可以使用缓存先存起来(就是存在本地磁盘上了). 从而提高访问效率.
可以通过 ctrl + F5 强制刷新页面, 强制浏览器重新获取 css 文件.
p { color: red; font-size: 30px;}
p {
color: red;
font-size: 30px;
}
虽然 CSS 不区分大小写, 我们开发时统一使用小写字母
选中页面中指定的标签元素.
要先选中元素, 才能设置元素的属性.
就好比 SC2, War3 这样的游戏, 需要先选中单位, 再指挥该单位行动.
以下内容只是 CSS2 标准中支持的选择器, 在 CSS3 中还做出了一些补充(在这里就不做介绍啦!)
特点:
<style>
p{
color: red;
}
div{
color: green;
}
style>
<p>哈哈哈p>
<p>哈哈哈p>
<p>哈哈哈p>
<p>哈哈哈p>
<div>呵呵呵div>
<div>呵呵呵div>
<div>呵呵呵div>
<div>呵呵呵div>

特点:
<style>
.blue {
color: blue;
}
style>
<div class="blue">哈哈div>
<div>哈哈div>

语法细节:
代码示例: 使用多个类名
注意: 一个标签可以同时使用多个类名
这样做可以把相同的属性提取出来, 达到简化代码的效果
<style>
.box {
width: 200px;
height: 150px;
}
.red {
background-color: red;
}
.green {
background-color: green;
}
style>
<div class="box red">div>
<div class="box green">div>
<div class="box red">div>

和类选择器类似.
<style>
#ha {
color: red;
}
style>
<div id="ha">蛤蛤蛤div>

类比:
姓名是类选择器, 可以重复.
身份证号码是 id 选择器, 是唯一的.
使用 * 的定义, 选取所有的标签.
- {
color: red;
}
页面的所有内容都会被改成 红色 .
不需要被页面结构调用.
通配符选择器在实际开发当中用来针对页面中的所有元素默认样式进行消除,主要用来消除边距
基础选择器小结
+----------------------------------------------------------------------+
| | 作用 | 特点 |
+----------------------------------------------------------------------+
| 标签选择器 | 能选出所有相同标签 | 不能差异化选择 |
| 类选择器 | 能选出一个或多个标签 | 根据需求选择, 最灵活, 最常用. |
| id 选择器 | 能选出一个标签 | 同一个id 在一个 HTML 中只能出现一次 |
| 通配符选择器 | 选择所有标签 | 特殊情况下使用 |
+----------------------------------------------------------------------+
又叫包含选择器. 选择某个父元素中的某个子元素.
元素1 元素2 {样式声明}
代码示例: 把 ol 中的 li 修改颜色, 不影响 ul
<ul>
<li>aaali>
<li>bbbli>
<li>cccli>
ul>
<ol>
<li>dddli>
<li>eeeli>
<li>fffli>
ol>
ol li {
color: red;
}
代码示例: 元素 2 不一定非是 儿子, 也可以是孙子.
<ul>
<li>aaali>
<li>bbbli>
<li><a href="#">ccca>li>
ul>
ul li a {
color: yellow;
}
或者
ul a {
color: yellow;
}
代码示例: 可以是任意基础选择器的组合. (包括类选择器, id 选择器)
<ol class="one">
<li>dddli>
<li>eeeli>
<li><a href="#">fffa>li>
<li><a href="#">fffa>li>
<li><a href="#">fffa>li>
ol>
.one li a {
color: green;
}
和后代选择器类似, 但是只能选择子标签.
元素1>元素2 { 样式声明 }
<div class="two">
<a href="#">链接1a>
<p><a href="#">链接2a>p>
div>
后代选择器的写法, 会把链接1 和 2 都选中.
.two a {
color: red;
}
子选择器的写法, 只选链接 1
.two>a {
color: red;
}
练习
<div class="cat">
<ul>
<li><a href="#">小猫a>li>
<li><a href="#">小猫a>li>
<li><a href="#">小猫a>li>
ul>
div>
CSS 结果:
.cat ul li a {
color: red;
}
<div class="cat">
<a href="#">小猫a>
<ul>
<li><a href="#">小狗a>li>
<li><a href="#">小狗a>li>
ul>
div>
使用 Emmet 快捷键生成上面的 html 代码:
.cat>a+ul>li*2>a
CSS 结果:
.cat>a {
color: red;
}
用于选择多组标签. (集体声明)
元素1, 元素2 { 样式声明 }
代码示例:
<div>苹果div>
<h3>香蕉h3>
<ul>
<li>鸭梨li>
<li>橙子li>
ul>
div, h3 {
color: red;
}
div,
h3,
ul>li {
color: red;
}
a:link 选择未被访问过的链接
a:visited 选择已经被访问过的链接
a:hover 选择鼠标指针悬停上的链接
a:active 选择活动链接(鼠标按下了但是未弹起)
示例:
<a href="#">小猫a>
a:link {
color: black;
/* 去掉 a 标签的下划线 */
text-decoration: none;
}
a:visited {
color: green;
}
a:hover {
color: red;
}
a:active {
color: blue;
}
如何让一个已经被访问过的链接恢复成未访问的状态?
清空浏览器历史记录即可. ctrl + shift + delete
注意事项
a {
color: black;
}
a:hover {
color: red;
}
<div class="three">
<input type="text">
<input type="text">
<input type="text">
<input type="text">
div>
.three>input:focus {
color: red;
}
此时被选中的表单的字体就会变成红色.

复合选择器小结
+------------------------------------------------------------+
| 选择器 | 作用 | 注意事项 |
+------------------------------------------------------------+
| 后代选择器 | 选择后代元素 | 可以是孙子元素 |
| 子选择器 | 选择子元素 | 只能选亲儿子, 不能选孙子 |
| 并集选择器 | 选择相同样式的元素 | 更好的做到代码重用 |
| 链接伪类选择器 | 选择不同状态的链接 | 重点掌握 a:hover 的写法|
| :focuse 伪类选择器 | 选择被选中的元素 | 重点掌握 input:focus |
+------------------------------------------------------------+
不需要全都背下来, 而是在使用中学习.

body {
font-family: '微软雅黑';
font-family: 'Microsoft YaHei';
}
<style>
.font-family .one {
font-family: 'Microsoft YaHei';
}
.font-family .two {
font-family: '宋体';
}
style>
<div class="font-family">
<div class="one">
这是微软雅黑
div>
<div class="two">
这是宋体
div>
div>
p {
font-size: 20px;
}
注意: 实际上它设置的是字体中字符框的高度;实际的字符字形可能比这些框高或矮
<style>
.font-size .one {
font-size: 40px;
}
.font-size .two {
font-size: 20px;
}
style>
<div class="font-size">
<div class="one">
大大大
div>
<div class="two">
小小小
div>
div>
p {
font-weight: bold;
font-weight: 700;
}
700 == bold, 400 是不变粗, == normal<style>
.font-weight .one {
font-weight: 900;
}
.font-weight .two {
font-weight: 100;
}
style>
<div class="font-weight">
<div class="one">
粗粗粗
div>
<div class="two">
细细细
div>
div>
/* 设置倾斜 */
font-style: italic;
/* 取消倾斜 */
font-style: normal;
很少把某个文字变倾斜.
但是经常要把 em / i 改成不倾斜.

认识 RGB
我们的显示器是由很多很多的 “像素” 构成的. 每个像素视为一个点, 这个点就能反映出一个具体的颜色.
我们使用 R (red), G (green), B (blue) 的方式表示颜色(色光三原色). 三种颜色按照不同的比例搭配, 就能
混合出各种五彩斑斓的效果.
计算机中针对 R, G, B 三个分量, 分别使用一个字节表示(8个比特位, 表示的范围是 0-255, 十六进制表示
为 00-FF).
数值越大, 表示该分量的颜色就越浓. 255, 255, 255 就表示白色; 0, 0, 0 就表示黑色.
设置文本颜色
color: red;
color: #ff0000; /*其中ff表示红色像素点,00表示绿色像素点,00表示蓝色像素点*/
color: rgb(255, 0, 0);
鼠标悬停在 vscode 的颜色上, 会出现颜色选择器, 可以手动调整颜色.
color 属性值的写法:
十六进制形式表示颜色, 如果两两相同, 就可以用一个来表示.
#ff00ff => #f0f
<style>
.color {
color: red;
/* color: rgb(255, 0, 0); */
/* color: #ff0000; */
}
style>
<div class="color">这是一段话div>
控制文字水平方向的对齐.
不光能控制文本对齐, 也能控制图片等元素居中或者靠右
text-align: [值];
<style>
.text-align .one {
text-align: left;
}
.text-align .two {
text-align: right;
}
.text-align .three {
text-align: center;
}
style>
<div class="text-align">
<div class="one">左对齐div>
<div class="two">右对齐div>
<div class="three">居中对齐div>
div>
text-decoration: [值];
常用取值:
<style>
.text-decorate .one {
text-decoration: none;
}
.text-decorate .two {
text-decoration: underline;
}
.text-decorate .three {
text-decoration: overline;
}
.text-decorate .four {
text-decoration: line-through;
}
style>
<div class="text-decorate">
<div class="one">啥都没有div>
<div class="two">下划线div>
<div class="three">上划线div>
<div class="four">删除线div>
div>
控制段落的 首行 缩进 (其他行不影响)
text-indent: [值];
<style>
.text-indent .one {
text-indent: 2em;
}
.text-indent .two {
text-indent: -2em;
}
style>
<div class="text-indent">
<div class="one">正常缩进div>
<div class="two">反向缩进div>
div>
行高指的是上下文本行之间的基线距离.
HTML 中展示文字涉及到这几个基准线:
内容区:底线和顶线包裹的区域,即下图深灰色背景区域

其实基线之间的距离 = 顶线间距离 = 底线间距离 = 中线间距离
line-height: [值];
注意1: 行高 = 上边距 + 下边距 + 字体大小
上下边距是相等的, 此处字体大小是 16px, 行高 40px, 上下边距就分别是 12px
<style>
.line-height .one {
line-height: 40px;
font-size: 16px;
}
style>
<div class="line-height">
<div>
上一行
div>
<div class="one">
中间行
div>
<div>
下一行
div>
div>

注意2: 行高也可以取 normal 等值.
这个取决于浏览器的实现. chrome 上 normal 为 21 px
注意3: 行高等与元素高度, 就可以实现文字居中对齐.
<style>
.line-height .two {
height: 100px;
line-height: 100px;
}
style>
<div class="line-height">
<div class="two">
文本垂直居中
div>
div>

background-color: [指定颜色]
默认是 transparent (透明) 的. 可以通过设置颜色的方式修改.
<style>
body {
background-color: #f3f3f3;
}
.bgc .one {
background-color: red;
}
.bgc .two {
background-color: #0f0;
}
.bgc .three {
/* 背景透明 */
background-color: transparent;
}
style>
<div class="bgc">
<div class="one">红色背景div>
<div class="two">绿色背景div>
<div class="three">透明背景div>
div>
background-image: url(...);
比 image 更方便控制位置(图片在盒子中的位置)
注意:
- url 不要遗漏.
- url 可以是绝对路径, 也可以是相对路径
- url 上可以加引号, 也可以不加.
<style>
.bgi .one {
background-image: url(rose.jpg);
height: 300px;
}
style>
<div class="bgi">
<div class="one">背景图片div>
div>
background-repeat: [平铺方式]
重要取值:
默认是 repeat.
背景颜色和背景图片可以同时存在. 背景图片在背景颜色的上方.
<style>
.bgr .one {
background-image: url(rose.jpg);
height: 300px;
background-repeat: no-repeat;
}
.bgr .two {
background-image: url(rose.jpg);
height: 300px;
background-repeat: repeat-x;
}
.bgr .three {
background-image: url(rose.jpg);
height: 300px;
background-repeat: repeat-y;
}
style>
<div class="bgr">
<div class="one">不平铺div>
<div class="two">水平平铺div>
<div class="three">垂直平铺div>
div>
background-position: x y;
修改图片的位置.
参数有三种风格:
<style>
.bgp .one {
background-image: url(rose.jpg);
height: 500px;
background-repeat: no-repeat;
background-color: purple;
background-position: center;
}
style>
<div class="bgp">
<div class="one">背景居中div>
div>
注意
关于坐标系:
计算机中的平面坐标系, 一般是左手坐标系(和高中数学上常用的右手系不一样. y轴是往下指的).
background-size: length|percentage|cover|contain;
<style>
.bgs .one {
width: 500px;
height: 300px;
background-image: url(rose.jpg);
background-repeat: no-repeat;
background-position: center;
background-size: contain;
}
style>
<div class="bgs">
<div class="one">背景尺寸div>
div>
注意体会 contain 和 cover 的区别. 当元素为矩形(不是正方形) 时, 区别是很明显的.
contain:

cover:

通过 border-radius 使边框带圆角效果.
基本用法
border-radius: length;
length 是内切圆的半径. 数值越大, 弧线越强烈

<div>蛤蛤div>
div {
width: 200px;
height: 100px;
border: 2px solid green;
border-radius: 10px;
}
让 border-radius 的值为正方形宽度的一半即可.
div {
width: 200px;
height: 200px;
border: 2px solid green;
border-radius: 100px;
/* 或者用 50% 表示宽度的一半 */
border-radius: 50%;
}
让 border-radius 的值为矩形高度的一半即可
div {
width: 200px;
height: 100px;
border: 2px solid green;
border-radius: 50px;
}

展开写法
border-radius 是一个复合写法. 实际上可以针对四个角分别设置.
border-radius:2em;
等价于
border-top-left-radius:2em;
border-top-right-radius:2em;
border-bottom-right-radius:2em;
border-bottom-left-radius:2em;
border-radius: 10px 20px 30px 40px;
等价于(按照顺时针排列)
border-top-left-radius:10px;
border-top-right-radius:20px;
border-bottom-right-radius:30px;
border-bottom-left-radius:40px;
打开浏览器
有两种方式可以打开 Chrome 调试工具
标签页含义
elements 标签页使用

在 CSS 中, HTML 的标签的显示模式有很多.
此处只重点介绍两个:
常见的元素:
h1 - h6
p
div
ul
ol
li
…
特点:
<style>
.demo1 .parent {
width: 500px;
height: 500px;
background-color: green;
}
.demo1 .child {
/* 不写 width, 默认和父元素一样宽 */
/* 不写 height, 默认为 0 (看不到了) */
height: 200px;
background-color: red;
}
style>
<div class="demo1">
<div class="parent">
<div class="child">
child1
div>
<div class="child">
child2
div>
div>
div>
注意:
<body>
<p>
<div>蛤蛤div>
p>
body>
常见的元素:
a
strong
b
em
i
del
s
ins
u
span
…
特点:
不独占一行, 一行可以显示多个
设置高度, 宽度, 行高无效
左右外边距有效(上下无效). 内边距有效.
默认宽度就是本身的内容
行内元素只能容纳文本和其他行内元素, 不能放块级元素
<style>
.demo2 span {
width: 200px;
height: 200px;
background-color: red;
}
style>
<div class="demo2">
<span>child1span>
<span>child2span>
<span>child3span>
div>

注意:
使用 display 属性可以修改元素的显示模式.
可以把 div 等变成行内元素, 也可以把 a , span 等变成块级元素.
每一个 HTML 元素就相当于是一个矩形的 “盒子”
这个盒子由这几个部分构成

基础属性
<div>testdiv>
div {
width: 500px;
height: 250px;
border-width: 10px;
border-style: solid;
border-color: green;
}
支持简写, 没有顺序要求
border: 1px solid red;
可以改四个方向的任意边框.
border-top/bottom/left/right
以下的代码只给上边框设为红色, 其余为蓝色.
利用到的层叠性, 就近原则的生效.
border: 1px solid blue;
border-top: red;
边框会撑大盒子
可以看到, width, height 是 500 * 250, 而最终整个盒子大小是 520 * 270. 边框10个像素相当于扩大了大小.

买房子时:
建筑面积 = 套内面试 + 公摊面试(电梯间)
套内面积 = 使用面积 + 墙占据的空间
蓝色区域就是 “使用面积”, 绿色区域就是 “套内面积”
通过 box-sizing 属性可以修改浏览器的行为, 使边框不再撑大盒子.
*为通配符选择器.
* {
box-sizing: border-box;
}
padding 设置内容和边框之间的距离.
基础写法
默认内容是顶着边框来放置的. 用 padding 来控制这个距离
可以给四个方向都加上边距
<div>
test
div>
div {
height: 200px;
width: 300px;
}

加上 padding 之后
div {
height: 200px;
width: 300px;
padding-top: 5px;
padding-left: 10px;
}

此时可以看到带有了一个绿色的内边距.
注意:
300 * 200 => 310 * 205. 说明内边距也会影响到盒子大小(撑大盒 子).使用 box-sizing: border-box 属性也可以使内边距不再撑大盒子. (和上面 border 类似)
复合写法
可以把多个方向的 padding 合并到一起. [四种情况都要记住, 都很常见]
padding: 5px; 表示四个方向都是 5px
padding: 5px 10px; 表示上下内边距 5px, 左右内边距为 10px
padding: 5px 10px 20px; 表示上边距 5px, 左右内边距为 10px, 下内边距为 20px
padding: 5px 10px 20px 30px; 表示 上5px, 右10px, 下20px, 左30px (顺时针)
控制台中选中元素, 查看右下角, 是很清楚的
基础写法
控制盒子和盒子之间的距离.
可以给四个方向都加上边距
<div class="first">蛤蛤div>
<div>呵呵div>
div {
background-color: red;
width: 200px;
height: 200px;
}
.first {
margin-bottom: 20px;
}

复合写法
规则同 padding
margin: 10px; // 四个方向都设置
margin: 10px 20px; // 上下为 10, 左右 20
margin: 10px 20px 30px; // 上 10, 左右 20, 下 30
margin: 10px 20px 30px 40px; // 上 10, 右 20, 下 30, 左 40
块级元素水平居中
前提:
三种写法均可.
margin-left: auto; margin-right: auto;
margin: auto;
margin: 0 auto;
示例:
<div>蛤蛤div>
div {
width: 500px;
height: 200px;
background-color: red;
margin: 0 auto;
}
注意:
这个水平居中的方式和 text-align 不一样.
margin: auto 是给块级元素用得到.
text-align: center 是让行内元素或者行内块元素居中的.
另外, 对于垂直居中, 不能使用 "上下 margin 为 auto " 的方式.
浏览器会给元素加上一些默认的样式, 尤其是内外边距. 不同浏览器的默认样式存在差别.
为了保证代码在不同的浏览器上都能按照统一的样式显示, 往往我们会去除浏览器默认样式.
使用通配符选择器即可完成这件事情.
* {
marign: 0;
padding: 0;
}
创建一个 div, 内部包含三个 span
<div>
<span>1span>
<span>2span>
<span>3span>
div>
<style>
div {
width: 100%;
height: 150px;
background-color: red;
}
div>span {
background-color: green;
width: 100px;
}
style>
此时看到的效果为

当我们给 div 加上 display:flex 之后, 效果为

此时看到, span 有了高度, 不再是 “行内元素了”
再给 div 加上 justify-content: space-around; 此时效果为

此时可以看到这些 span 已经能够水平隔开了.
把 justify-content: space-around; 改为 justify-content: flex-end; 可以看到此时三个元素在
右侧显示了.

flex 是 flexible box 的缩写. 意思为 “弹性盒子”.
任何一个 html 元素, 都可以指定为 display:flex 完成弹性布局.
flex 布局的本质是给父盒子添加 display:flex 属性, 来控制子盒子的位置和排列方式.
基础概念:

display: flex 之后, 子元素的 float, clear, vertical-align 都会失效.justify-content
设置主轴上的子元素排列方式.
使用之前一定要确定好主轴是哪个方向
属性取值

代码示例
<div>
<span>1span>
<span>2span>
<span>3span>
<span>4span>
<span>5span>
div>
<style>
div {
width: 100%;
height: 150px;
background-color: red;
display: flex;
}
div span {
width: 100px;
height: 100px;
background-color: green;
}
style>
未指定 justify-content 时, 默认按照从左到右的方向布局.

设置 justify-content: flex-end , 此时元素都排列到右侧了.

设置 jutify-content: center , 此时元素居中排列

设置 justify-content: space-around; 平分了剩余空间.

设置 justify-content: space-between; 先两边元素贴近边缘, 再平分剩余空间.

align-items
设置侧轴上的元素排列方式
在上面的代码中, 我们是让元素按照主轴的方向排列, 同理我们也可以指定元素按照侧轴方向排列.

取值和 justify-content 差不多.
理解 stretch(拉伸):
这个是 align-content 的默认值. 意思是如果子元素没有被显式指定高度, 那么就会填充满父元素的高度.
形如:
<div>
<span>1span>
<span>2span>
<span>3span>
div>
<style>
div {
width: 500px;
height: 500px;
background-color: green;
display: flex;
justify-content: space-around;
}
div span {
width: 150px;
background-color: red;
}
style>

可以使用 align-items 实现垂直居中
<div>
<span>1span>
<span>2span>
<span>3span>
div>
<style>
div {
width: 500px;
height: 500px;
background-color: green;
display: flex;
justify-content: space-around;
align-items: center;
}
div span {
width: 150px;
height: 100px;
background-color: red;
}
style>

注意:
align-items 只能针对单行元素来实现. 如果有多行元素, 就需要使用 item-contents