
- 选择器 + {一条/N条声明}
- 选择器决定针对谁修改 (找谁)
- 声明决定修改啥. (干啥)
- 声明的属性是键值对. 使用 ; 区分键值对, 使用 : 区分键和值.
- <style>
- p {
- /* 设置字体颜色 */
- color: red;
- /* 设置字体大小 */
- font-size: 30px;
- }
- style>
- <p>hellop>
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- div {
- color: red;
- }
- style>
- head>
- <body>
- <div style="color:green">想要生活过的去, 头上总得带点绿div>
- body>
- html>

此时我们可以发现此时的红色被覆盖了。
rel="stylesheet" href="[CSS文件路径]">
创建 demo.html
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>外部样式title>
- <link rel="stylesheet" href="style.css">
- head>
- <body>
- <div>上帝为你关上一扇门, 然后就去睡觉了div>
- body>
创建 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 不区分大小写, 我们开发时统一使用小写字母 。
空格规范
- 冒号后面带空格
- 选择器和 { 之间也有一个空格.
- 标签选择器
- 类选择器
- id 选择器
- 通配符选择器
- 后代选择器
- 子选择器
- 并集选择器
- 伪类选择器
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- p {
- color: red;
- }
- div {
- color: green;
- }
- style>
- head>
- <body>
-
- <p>咬人猫p>
- <p>咬人猫p>
- <p>咬人猫p>
- <div>阿叶君div>
- <div>阿叶君div>
- <div>阿叶君div>
- body>
- html>

- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
-
- <style>
- .blue {
- color: blue;
- }
- style>
- head>
- <body>
-
- <div class="blue">咬人猫div>
- <div>咬人猫div>
- body>
- html>
- 类名用 . 开头的
- 下方的标签使用 class 属性来调用.
- 一个类可以被多个标签使用, 一个标签也能使用多个类(多个类名要使用空格分割, 这种做法可以让代码更好复用)
- 如果是长的类名, 可以使用 - 分割.
- 不要使用纯数字, 或者中文, 以及标签名来命名类名.
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
-
- <style>
- .box {
- width: 200px;
- height: 150px;
- }
-
- .red {
- background-color: red;
- }
-
- .green {
- background-color: green;
- }
- style>
- head>
- <body>
- <div class="box red">div>
- <div class="box green">div>
- <div class="box red">div>
- body>
- html>

- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
-
- <style>
- #ha {
- color: red;
- }
- style>
- head>
- <body>
- <div id="ha">蛤蛤蛤div>
- body>
- html>

- * {
- color: red;
- }
元素1 元素2 {样式声明}
- 元素 1 和 元素 2 要使用空格分割
- 元素 1 是父级, 元素 2 是子级, 只选元素 2 , 不影响元素 1
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- ol li {
- color: red;
- }
- style>
-
- head>
- <body>
- <ul>
- <li>aaali>
- <li>bbbli>
- <li>cccli>
- ul>
- <ol>
- <li>dddli>
- <li>eeeli>
- <li>fffli>
- ol>
- body>
- html>
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- /* ul li a {
- color: yellow;
- } */
- ul a {
- color: yellow;
- }
- style>
-
- head>
- <body>
- <ul>
- <li>aaali>
- <li>bbbli>
- <li><a href="#">ccca>li>
- ul>
- body>
- html>
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- .one li a {
- color: green;
- }
- style>
-
- head>
- <body>
- <ol class="one">
- <li>dddli>
- <li>eeeli>
- <li><a href="#">fffa>li>
- <li><a href="#">fffa>li>
- <li><a href="#">fffa>li>
- ol>
- body>
- html>

元素1>元素2 { 样式声明 }
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- .two a {
- color: red;
- }
- style>
-
- head>
- <body>
- <div class="two">
- <a href="#">链接1a>
- <p><a href="#">链接2a>p>
- div>
- body>
- html>
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- .two>a {
- color: red;
- }
- style>
-
- head>
- <body>
- <div class="two">
- <a href="#">链接1a>
- <p><a href="#">链接2a>p>
- div>
- body>
- html>
元素1, 元素2 { 样式声明 }
- 通过 逗号 分割等多个元素.
- 表示同时选中元素 1 和 元素 2
- 任何基础选择器都可以使用并集选择器.
- 并集选择器建议竖着写. 每个选择器占一行. (最后一个选择器不能加逗号)
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- div, h3 {
- color: red;
- }
- style>
-
- head>
- <body>
- <div>苹果div>
- <h3>香蕉h3>
- <ul>
- <li>鸭梨li>
- <li>橙子li>
- ul>
- body>
- html>
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- div,
- h3,
- ul>li {
- color: red;
- }
- style>
-
- head>
- <body>
- <div>苹果div>
- <h3>香蕉h3>
- <ul>
- <li>鸭梨li>
- <li>橙子li>
- ul>
- body>
- html>
- a:link 选择未被访问过的链接
- a:visited 选择已经被访问过的链接
- a:hover 选择鼠标指针悬停上的链接
- a:active 选择活动链接(鼠标按下了但是未弹起)
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- a:link {
- color: black;
- /* 去掉 a 标签的下划线 */
- text-decoration: none;
- }
-
- a:visited {
- color: green;
- }
-
- a:hover {
- color: red;
- }
-
- a:active {
- color: blue;
- }
- style>
-
- head>
- <body>
- <a href="#">小猫a>
- body>
- html>
a {color : black ;}a : hover {color : red ;}
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- .three>input:focus {
- color: red;
- }
- style>
-
- head>
- <body>
- <div class="three">
- <input type="text">
- <input type="text">
- <input type="text">
- <input type="text">
- div>
- body>
- html>
- body {
- font-family: '微软雅黑';
- font-family: 'Microsoft YaHei';
- }
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- .font-family .one {
- font-family: 'Microsoft YaHei';
- }
- .font-family .two {
- font-family: '宋体';
- }
- style>
-
- head>
- <body>
- <div class="font-family">
- <div class="one">
- 这是微软雅黑
- div>
- <div class="two">
- 这是宋体
- div>
- div>
- body>
- html>

- p {
- font-size: 20px;
- }
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- .font-size .one {
- font-size: 40px;
- }
- .font-size .two {
- font-size: 20px;
- }
- style>
-
- head>
- <body>
- <div class="font-size">
- <div class="one">
- 大大大
- div>
- <div class="two">
- 小小小
- div>
- div>
- body>
- html>
- p {
- font-weight: bold;
- font-weight: 700;
- }
- /* 设置倾斜 */
- font-style: italic;
- /* 取消倾斜 */
- font-style: normal;
很少把某个文字变倾斜.
但是经常要把 em / i 改成不倾斜.
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- .font-style em {
- font-style: normal;
- }
- .font-style div {
- font-style: italic;
- }
- style>
-
- head>
- <body>
- <div class="font-style">
- <em>
- 放假啦
- em>
- <div class="one">
- 听说要加班
- div>
- div>
- body>
- html>

color : red ;color : #ff0000 ;color : rgb ( 255 , 0 , 0 );
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- .color {
- color: red;
- /* color: rgb(255, 0, 0); */
- /* color: #ff0000; */
- }
- style>
-
- head>
- <body>
- <div class="color">这是一段话div>
- body>
- html>
不光能控制文本对齐, 也能控制图片等元素居中或者靠右
text-align : [ 值 ];
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- .text-align .one {
- text-align: left;
- }
-
- .text-align .two {
- text-align: right;
- }
-
- .text-align .three {
- text-align: center;
- }
- style>
- head>
-
- <body>
- <div class="text-align">
- <div class="one">左对齐div>
- <div class="two">右对齐div>
- <div class="three">居中对齐div>
- div>
- body>
-
- html>

text-decoration : [ 值 ];
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <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>
- head>
-
- <body>
- <div class="text-decorate">
- <div class="one">啥都没有div>
- <div class="two">下划线div>
- <div class="three">上划线div>
- <div class="four">删除线div>
- div>
- body>
-
- html>
控制段落的 首行 缩进 (其他行不影响)
text-indent : [ 值 ];
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- .text-indent .one {
- text-indent: 2em;
- }
- .text-indent .two {
- text-indent: -2em;
- }
- style>
- head>
-
- <body>
- <div class="text-indent">
- <div class="one">正常缩进div>
- <div class="two">反向缩进div>
- div>
- body>
-
- html>

text-indent : [ 值 ];
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- .line-height .one {
- line-height: 40px;
- font-size: 16px;
- }
- style>
- head>
-
- <body>
- <div class="line-height">
- <div>
- 上一行
- div>
- <div class="one">
- 中间行
- div>
- <div>
- 下一行
- div>
- div>
- body>
-
- html>
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- .line-height .two {
- height: 100px;
- line-height: 100px;
- }
- style>
- head>
-
- <body>
- <div class="line-height">
- <div class="two">
- 文本垂直居中
- div>
- div>
- body>
-
- html>

background-color: [ 指定颜色 ]
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- body {
- background-color: #f3f3f3;
- }
- .bgc .one {
- background-color: red;
- }
- .bgc .two {
- background-color: #0f0;
- }
- .bgc .three {
- /* 背景透明 */
- background-color: transparent;
- }
- style>
- head>
-
- <body>
- <div class="bgc">
- <div class="one">红色背景div>
- <div class="two">绿色背景div>
- <div class="three">透明背景div>
- div>
- body>
-
- html>

background-image: url(...);
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- .bgi .one {
- background-image: url(前端.jpg);
- height: 300px;
- }
- style>
- head>
-
- <body>
- <div class="bgi">
- <div class="one">背景图片div>
- div>
- body>
-
- html>
background-repeat: [平铺方式]
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- .bgr .one {
- background-image: url(前端.jpg);
- height: 300px;
- background-repeat: no-repeat;
- }
- .bgr .two {
- background-image: url(前端.jpg);
- height: 300px;
- background-repeat: repeat-x;
- }
- .bgr .three {
- background-image: url(前端.jpg);
- height: 300px;
- background-repeat: repeat-y;
- }
- style>
- head>
-
- <body>
- <div class="bgr">
- <div class="one">不平铺div>
- <div class="two">水平平铺div>
- <div class="three">垂直平铺div>
- div>
- body>
-
- html>
background-position: x y;
修改图片的位置.
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- .bgp .one {
- background-image: url(前端.jpg);
- height: 500px;
- background-repeat: no-repeat;
- background-color: purple;
- background-position: center;
- }
- style>
- head>
-
- <body>
- <div class="bgp">
- <div class="one">背景居中div>
- div>
- body>
-
- html>
background-size : length | percentage | cover | contain ;
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- .bgs .one {
- width: 500px;
- height: 300px;
- background-image: url(前端.jpg);
- background-repeat: no-repeat;
- background-position: center;
- background-size: contain;
- }
- style>
- head>
-
- <body>
- <div class="bgs">
- <div class="one">背景尺寸div>
- div>
- body>
-
- html>
cover:

border-radius: length;
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- div {
- width: 200px;
- height: 100px;
- border: 2px solid green;
- border-radius: 10px;
- }
- style>
- head>
-
- <body>
- <div>蛤蛤div>
- body>
-
- html>
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- div {
- width: 200px;
- height: 200px;
- border: 2px solid green;
- border-radius: 100px;
- /* 或者用 50% 表示宽度的一半 */
- border-radius: 50%;
- }
- style>
- head>
-
- <body>
- <div>蛤蛤div>
- body>
-
- html>
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- div {
- width: 200px;
- height: 100px;
- border: 2px solid green;
- border-radius: 50px;
- }
- style>
- head>
-
- <body>
- <div>蛤蛤div>
- body>
-
- html>

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 ;
- elements 查看标签结构
- console 查看控制台
- source 查看源码+断点调试
- network 查看前后端交互过程
- application 查看浏览器提供的一些扩展功能(本地存储等)
- Performance, Memory, Security, Lighthouse
- ctrl + 滚轮进行缩放, ctrl + 0 恢复原始大小.
- 使用 左上角 箭头选中元素
- 右侧可以查看当前元素的属性, 包括引入的类.
- 右侧可以修改选中元素的 css 属性. 例如颜色, 可以点击颜色图标, 弹出颜色选择器, 修改颜色. 例如
- 字体大小, 可以使用方向键来微调数值.
- 此处的修改不会影响代码, 刷新就还原了~
- 如果 CSS 样式写错了, 也会在这里有提示. (黄色感叹号)
在 CSS 中, HTML 的标签的显示模式有很多.此处只重点介绍两个:
- 块级元素
- 行内元素
- h1 - h6
- p
- div
- ul
- ol
- li
- ....
- 独占一行
- 高度, 宽度, 内外边距, 行高都可以控制.
- 宽度默认是父级元素宽度的 100% (和父元素一样宽)
- 是一个容器(盒子), 里面可以放行内和块级元素.
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- .demo1 .parent {
- width: 500px;
- height: 500px;
- background-color: green;
- }
- .demo1 .child {
- /* 不写 width, 默认和父元素一样宽 */
- /* 不写 height, 默认为 0 (看不到了) */
- height: 200px;
- background-color: red;
- }
- style>
- head>
-
- <body>
- <div class="demo1">
- <div class="parent">
- <div class="child">
- child1
- div>
- <div class="child">
- child2
- div>
- div>
- div>
- body>
-
- html>

注意:
蛤蛤
- a
- strong
- b
- em
- i
- del
- s
- ins
- u
- span
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- .demo2 span {
- width: 200px;
- height: 200px;
- background-color: red;
- }
- style>
- head>
-
- <body>
- <div class="demo2">
- <span>child1span>
- <span>child2span>
- <span>child3span>
- div>
- body>
-
- html>

注意:
- display: block 改成块级元素 [常用]
- display: inline 改成行内元素 [很少用]
- display: inline-block 改成行内块元素