• 前端--CSS



    文章目录

    • CSS的介绍
    • 引入方式
    • 代码风格
    • 选择器
    • 复合选择器 (选学)
    • 常用元素属性
    • 背景属性
    • 圆角矩形
    • Chrome 调试工具 -- 查看 CSS 属性
    • 元素的显示模式
    • 盒模型
    • 弹性布局

    一、CSS的介绍

    层叠样式表 (Cascading Style Sheets).
    CSS 能够对网页中元素位置的排版进行像素级精确控制, 实现美化页面的效果. 能够做到页面的样式和结构分离.

    基础语法规范

    • 选择器 + {一条/N条声明}
    • 选择器决定针对谁修改 (找谁)
    • 声明决定修改啥. (干啥)
    • 声明的属性是键值对. 使用 ; 区分键值对, 使用 : 区分键和值.
    1. <style>
    2.    p {
    3.        /* 设置字体颜色 */
    4.        color: red;
    5.        /* 设置字体大小 */
    6.        font-size: 30px;
    7.   }
    8. style>
    9. <p>hellop>
    注意:
    • CSS 要写到 style 标签中(还有其他写法) .
    • style 标签可以放到页面任意位置. 一般放到 head 标签内.
    • CSS 使用 /* */ 作为注释. (使用 ctrl + / 快速切换) .

    二、引入方式

    1.内部样式表

    写在 style 标签中. 嵌入到 html 内部. 理论上来说 style 放到 html 的哪里都行. 但是一般都是放到 head 标签中.
    优点: 这样做能够让样式和页面结构分离.
    缺点: 分离的还不够彻底. 尤其是 css 内容多的时候.

    2.行内样式表

    通过 style 属性, 来指定某个标签的样式. 只适合于写简单样式. 只针对某个标签生效.
    缺点: 不能写太复杂的样式. 这种写法优先级较高, 会覆盖其他的样式.
    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. <style>
    8. div {
    9. color: red;
    10. }
    11. style>
    12. head>
    13. <body>
    14. <div style="color:green">想要生活过的去, 头上总得带点绿div>
    15. body>
    16. html>

    此时我们可以发现此时的红色被覆盖了。

    3.外部样式

    实际开发中最常用的方式.
    1. 创建一个 css 文件.
    2. 使用 link 标签引入 css

    rel="stylesheet" href="[CSS文件路径]">

    创建 demo.html  

    1. <head>
    2.    <meta charset="UTF-8">
    3.    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    4.    <title>外部样式title>
    5.    <link rel="stylesheet" href="style.css">
    6. head>
    7. <body>
    8.    <div>上帝为你关上一扇门, 然后就去睡觉了div>
    9. body>

    创建 style.css

    1. div {
    2.    color: red;
    3. }

    注意: 不要忘记 link 标签调用 CSS, 否则不生效.

    优点: 样式和结构彻底分离了.
    缺点: 受到浏览器缓存影响, 修改之后 不一定 立刻生效.
    • 关于缓存:
    • 这是计算机中一种常见的提升性能的技术手段.
    • 网页依赖的资源(图片/CSS/JS等)通常是从服务器上获取的. 如果频繁访问该网站, 那么这些外部资源就没必要反复从服务器获取. 就可以使用缓存先存起来(就是存在本地磁盘上了). 从而提高访问效率.
    • 可以通过 ctrl + F5 强制刷新页面, 强制浏览器重新获取 css 文件.

    三、代码风格

    1.样式格式

    1.紧凑风格

    p { color : red ; font-size : 30px ;}

    2.展开风格(推荐)

    p {
        color : red ;
        font-size : 30px ;
    }

    注意:虽然 CSS 不区分大小写, 我们开发时统一使用小写字母 。

     

    空格规范
    • 冒号后面带空格
    • 选择器和 { 之间也有一个空格.

    四、选择器

    1.选择器的功能

    选中页面中指定的标签元素. 要先选中元素, 才能设置元素的属性.

    1.选择器的种类

    以下内容只是 CSS2 标准中支持的选择器, 在 CSS3 中还做出了一些补充.
    1. 基础选择器: 单个选择器构成的
    • 标签选择器
    • 类选择器
    • id 选择器
    • 通配符选择器
    2. 复合选择器: 把多种基础选择器综合运用起来.
    • 后代选择器
    • 子选择器
    • 并集选择器
    • 伪类选择器

    2.基础选择器        

    特点: 能快速为同一类型的标签都选择出来. 但是不能差异化选择 。
    测试代码:
    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. <style>
    8. p {
    9. color: red;
    10. }
    11. div {
    12. color: green;
    13. }
    14. style>
    15. head>
    16. <body>
    17. <p>咬人猫p>
    18. <p>咬人猫p>
    19. <p>咬人猫p>
    20. <div>阿叶君div>
    21. <div>阿叶君div>
    22. <div>阿叶君div>
    23. body>
    24. html>

     

    3.类选择器

    特点:
    • 差异化表示不同的标签
    • 可以让多个标签的都使用同一个标签.
    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. <style>
    8. .blue {
    9. color: blue;
    10. }
    11. style>
    12. head>
    13. <body>
    14. <div class="blue">咬人猫div>
    15. <div>咬人猫div>
    16. body>
    17. html>
    语法细节:
    • 类名用 . 开头的
    • 下方的标签使用 class 属性来调用.
    • 一个类可以被多个标签使用, 一个标签也能使用多个类(多个类名要使用空格分割, 这种做法可以让代码更好复用)
    • 如果是长的类名, 可以使用 - 分割.
    • 不要使用纯数字, 或者中文, 以及标签名来命名类名.
    代码示例: 使用多个类名
    注意: 一个标签可以同时使用多个类名
    这样做可以把相同的属性提取出来, 达到简化代码的效果.
    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. <style>
    8. .box {
    9. width: 200px;
    10. height: 150px;
    11. }
    12. .red {
    13. background-color: red;
    14. }
    15. .green {
    16. background-color: green;
    17. }
    18. style>
    19. head>
    20. <body>
    21. <div class="box red">div>
    22. <div class="box green">div>
    23. <div class="box red">div>
    24. body>
    25. html>

    4.id 选择器

    和类选择器类似.
    • CSS 中使用 # 开头表示 id 选择器
    • id 选择器的值和 html 中某个元素的 id 值相同
    • html 的元素 id 不必带 #
    • id 是唯一的, 不能被多个标签使用 (是和 类选择器 最大的区别)
    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. <style>
    8. #ha {
    9. color: red;
    10. }
    11. style>
    12. head>
    13. <body>
    14. <div id="ha">蛤蛤蛤div>
    15. body>
    16. html>

     

    5.通配符选择器

    使用 * 的定义, 选取所有的标签.
    1. * {
    2. color: red;
    3. }
    页面的所有内容都会被改成 红色 .
    不需要被页面结构调用.

    6.基础选择器小结

    五、复合选择器 (选学)

    后代选择器
    又叫包含选择器. 选择某个父元素中的某个子元素.
    元素1 元素2 {样式声明}
    • 元素 1 和 元素 2 要使用空格分割
    • 元素 1 是父级, 元素 2 是子级, 只选元素 2 , 不影响元素 1
    代码示例: 把 ol 中的 li 修改颜色, 不影响 ul
    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. <style>
    8. ol li {
    9. color: red;
    10. }
    11. style>
    12. head>
    13. <body>
    14. <ul>
    15. <li>aaali>
    16. <li>bbbli>
    17. <li>cccli>
    18. ul>
    19. <ol>
    20. <li>dddli>
    21. <li>eeeli>
    22. <li>fffli>
    23. ol>
    24. body>
    25. html>
    代码示例: 元素 2 不一定非是 儿子, 也可以是孙子.
    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. <style>
    8. /* ul li a {
    9.    color: yellow;
    10. } */
    11. ul a {
    12. color: yellow;
    13. }
    14. style>
    15. head>
    16. <body>
    17. <ul>
    18.    <li>aaali>
    19.    <li>bbbli>
    20.    <li><a href="#">ccca>li>
    21. ul>
    22. body>
    23. html>

    代码示例: 可以是任意基础选择器的组合. (包括类选择器, id 选择器)
    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. <style>
    8. .one li a {
    9.     color: green;
    10. }
    11. style>
    12. head>
    13. <body>
    14. <ol class="one">
    15.    <li>dddli>
    16.    <li>eeeli>
    17.    <li><a href="#">fffa>li>
    18.    <li><a href="#">fffa>li>
    19.    <li><a href="#">fffa>li>
    20. ol>
    21. body>
    22. html>

    1.子选择器

    和后代选择器类似, 但是只能选择子标签.
    元素1>元素2 { 样式声明 }
    • 使用大于号分割
    • 只选亲儿子, 不选孙子元素
    后代选择器的写法, 会把链接1 和 2 都选中.
    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. <style>
    8. .two a {
    9. color: red;
    10. }
    11. style>
    12. head>
    13. <body>
    14. <div class="two">
    15. <a href="#">链接1a>
    16. <p><a href="#">链接2a>p>
    17. div>
    18. body>
    19. html>
    子选择器的写法, 只选链接 1
    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. <style>
    8. .two>a {
    9. color: red;
    10. }
    11. style>
    12. head>
    13. <body>
    14. <div class="two">
    15. <a href="#">链接1a>
    16. <p><a href="#">链接2a>p>
    17. div>
    18. body>
    19. html>

     

    2.并集选择器

    用于选择多组标签. (集体声明)
    元素1, 元素2 { 样式声明 }
    • 通过 逗号 分割等多个元素.
    • 表示同时选中元素 1 和 元素 2
    • 任何基础选择器都可以使用并集选择器.
    • 并集选择器建议竖着写. 每个选择器占一行. (最后一个选择器不能加逗号)
    代码示例 :
    1. 把苹果和香蕉颜色改成红色
    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. <style>
    8. div, h3 {
    9. color: red;
    10. }
    11. style>
    12. head>
    13. <body>
    14. <div>苹果div>
    15. <h3>香蕉h3>
    16. <ul>
    17. <li>鸭梨li>
    18. <li>橙子li>
    19. ul>
    20. body>
    21. html>

    2. 把鸭梨和橙子也都一起改成红色
    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. <style>
    8. div,
    9. h3,
    10. ul>li {
    11. color: red;
    12. }
    13. style>
    14. head>
    15. <body>
    16. <div>苹果div>
    17. <h3>香蕉h3>
    18. <ul>
    19. <li>鸭梨li>
    20. <li>橙子li>
    21. ul>
    22. body>
    23. html>

     

    3.伪类选择器

    1) 链接伪类选择器

    • a:link 选择未被访问过的链接
    • a:visited 选择已经被访问过的链接
    • a:hover 选择鼠标指针悬停上的链接
    • a:active 选择活动链接(鼠标按下了但是未弹起)
    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. <style>
    8. a:link {
    9. color: black;
    10. /* 去掉 a 标签的下划线 */
    11. text-decoration: none;
    12. }
    13. a:visited {
    14. color: green;
    15. }
    16. a:hover {
    17. color: red;
    18. }
    19. a:active {
    20. color: blue;
    21. }
    22. style>
    23. head>
    24. <body>
    25. <a href="#">小猫a>
    26. body>
    27. html>

    示例:
    如何让一个已经被访问过的链接恢复成未访问的状态?
    清空浏览器历史记录即可. ctrl + shift + delete
    注意事项
    • 按照 LVHA 的顺序书写, 例如把 active 拿到前面去, 就会导致 active 失效. 记忆规则 "绿化"
    • 浏览器的 a 标签都有默认样式, 一般实际开发都需要单独制定样式.
    • 实际开发主要给链接做一个样式, 然后给 hover 做一个样式即可. link, visited, active 用的不多.
    a {
        color : black ;
    }
    a : hover {
        color : red ;
    }

    2) :force 伪类选择器

    选取获取焦点的 input 表单元素.
    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. <style>
    8. .three>input:focus {
    9. color: red;
    10. }
    11. style>
    12. head>
    13. <body>
    14. <div class="three">
    15. <input type="text">
    16. <input type="text">
    17. <input type="text">
    18. <input type="text">
    19. div>
    20. body>
    21. html>

    六、常用元素属性

    1.字体属性

    1.设置字体

    1. body {
    2.    font-family: '微软雅黑';
    3.    font-family: 'Microsoft YaHei';
    4. }
    • 字体名称可以用中文, 但是不建议.
    • 多个字体之间使用逗号分隔. (从左到右查找字体, 如果都找不到, 会使用默认字体. )
    • 如果字体名有空格, 使用引号包裹.
    • 建议使用常见字体, 否则兼容性不好.
    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. <style>
    8. .font-family .one {
    9. font-family: 'Microsoft YaHei';
    10. }
    11. .font-family .two {
    12. font-family: '宋体';
    13. }
    14. style>
    15. head>
    16. <body>
    17. <div class="font-family">
    18. <div class="one">
    19. 这是微软雅黑
    20. div>
    21. <div class="two">
    22. 这是宋体
    23. div>
    24. div>
    25. body>
    26. html>

    2.大小

    1. p {
    2.    font-size: 20px;
    3. }
    • 不同的浏览器默认字号不一样, 最好给一个明确值. (chrome 默认是 16px)
    • 可以给 body 标签使用 font-size
    • 要注意单位 px 不要忘记.
    • 标题标签需要单独指定大小
    注意: 实际上它设置的是字体中字符框的高度;实际的字符字形可能比这些框高或矮
    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. <style>
    8. .font-size .one {
    9. font-size: 40px;
    10. }
    11. .font-size .two {
    12. font-size: 20px;
    13. }
    14. style>
    15. head>
    16. <body>
    17. <div class="font-size">
    18. <div class="one">
    19. 大大大
    20. div>
    21. <div class="two">
    22. 小小小
    23. div>
    24. div>
    25. body>
    26. html>

    3.粗细

    1. p {
    2. font-weight: bold;
    3.    font-weight: 700;
    4. }
    • 可以使用数字表示粗细.
    • 700 == bold, 400 是不变粗, == normal
    • 取值范围是 100 -> 900
    4.文字样式
    1. /* 设置倾斜 */
    2. font-style: italic;
    3. /* 取消倾斜 */
    4. font-style: normal;

      很少把某个文字变倾斜.

      但是经常要把 em / i 改成不倾斜.

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. <style>
    8. .font-style em {
    9. font-style: normal;
    10. }
    11. .font-style div {
    12. font-style: italic;
    13. }
    14. style>
    15. head>
    16. <body>
    17. <div class="font-style">
    18. <em>
    19. 放假啦
    20. em>
    21. <div class="one">
    22. 听说要加班
    23. div>
    24. div>
    25. body>
    26. html>

    2.文本属性

    1.文本颜色

    认识 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 ;
    color : rgb ( 255 , 0 , 0 );
    鼠标悬停在 vscode 的颜色上, 会出现颜色选择器, 可以手动调整颜色.
    color 属性值的写法:
    • 预定义的颜色值(直接是单词)
    • [最常用] 十六进制形式
    • RGB 方式
    十六进制形式表示颜色, 如果两两相同, 就可以用一个来表示.
    #ff00ff => #f0f
    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. <style>
    8. .color {
    9. color: red;
    10. /* color: rgb(255, 0, 0); */
    11. /* color: #ff0000; */
    12. }
    13. style>
    14. head>
    15. <body>
    16. <div class="color">这是一段话div>
    17. body>
    18. html>

     

    2.文本对齐

    控制文字水平方向的对齐.

            不光能控制文本对齐, 也能控制图片等元素居中或者靠右

    text-align : [ ];
    • center: 居中对齐
    • left: 左对齐
    • right: 右对齐
    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. <style>
    8. .text-align .one {
    9. text-align: left;
    10. }
    11. .text-align .two {
    12. text-align: right;
    13. }
    14. .text-align .three {
    15. text-align: center;
    16. }
    17. style>
    18. head>
    19. <body>
    20. <div class="text-align">
    21. <div class="one">左对齐div>
    22. <div class="two">右对齐div>
    23. <div class="three">居中对齐div>
    24. div>
    25. body>
    26. html>

     

    3.文本装饰

    text-decoration : [ ];
    常用取值:
    • underline 下划线. [常用]
    • none 啥都没有. 可以给 a 标签去掉下划线.
    • overline 上划线. [不常用]
    • line-through 删除线 [不常用]
    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. <style>
    8. .text-decorate .one {
    9. text-decoration: none;
    10. }
    11. .text-decorate .two {
    12. text-decoration: underline;
    13. }
    14. .text-decorate .three {
    15. text-decoration: overline;
    16. }
    17. .text-decorate .four {
    18. text-decoration: line-through;
    19. }
    20. style>
    21. head>
    22. <body>
    23. <div class="text-decorate">
    24. <div class="one">啥都没有div>
    25. <div class="two">下划线div>
    26. <div class="three">上划线div>
    27. <div class="four">删除线div>
    28. div>
    29. body>
    30. html>

    4.文本缩进

    控制段落的 首行 缩进 (其他行不影响)

    text-indent : [ ];
    • 单位可以使用 px 或者 em.
    • 使用 em 作为单位更好. 1 个 em 就是当前元素的文字大小.
    • 缩进可以是负的, 表示往左缩进. (会导致文字就冒出去了)
    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. <style>
    8. .text-indent .one {
    9. text-indent: 2em;
    10. }
    11. .text-indent .two {
    12. text-indent: -2em;
    13. }
    14. style>
    15. head>
    16. <body>
    17. <div class="text-indent">
    18. <div class="one">正常缩进div>
    19. <div class="two">反向缩进div>
    20. div>
    21. body>
    22. html>

            

    5.行高

    行高指的是上下文本行之间的基线距离.
    HTML 中展示文字涉及到这几个基准线:
    • 顶线
    • 中线
    • 基线 (相当于英语四线格的倒数第二条线)
    • 底线
    内容区:底线和顶线包裹的区域,即下图深灰色背景区域
    其实基线之间的距离 = 顶线间距离 = 底线间距离 = 中线间距离
    text-indent : [ ];

     

    注意 1: 行高 = 上边距 + 下边距 + 字体大小
    上下边距是相等的, 此处字体大小是 16px, 行高 40px, 上下边距就分别是 12px
    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. <style>
    8. .line-height .one {
    9. line-height: 40px;
    10. font-size: 16px;
    11. }
    12. style>
    13. head>
    14. <body>
    15. <div class="line-height">
    16. <div>
    17. 上一行
    18. div>
    19. <div class="one">
    20. 中间行
    21. div>
    22. <div>
    23. 下一行
    24. div>
    25. div>
    26. body>
    27. html>
    注意 2: 行高也可以取 normal 等值 .
    这个取决于浏览器的实现. chrome 上 normal 为 21 px
    注意 3: 行高等与元素高度 , 就可以实现文字居中对齐 .
    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. <style>
    8. .line-height .two {
    9. height: 100px;
    10. line-height: 100px;
    11. }
    12. style>
    13. head>
    14. <body>
    15. <div class="line-height">
    16. <div class="two">
    17. 文本垂直居中
    18. div>
    19. div>
    20. body>
    21. html>

    七、背景属性

    1.背景颜色

    默认是 transparent (透明) 的. 可以通过设置颜色的方式修改.
    background-color: [ 指定颜色 ]
    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. <style>
    8. body {
    9. background-color: #f3f3f3;
    10. }
    11. .bgc .one {
    12. background-color: red;
    13. }
    14. .bgc .two {
    15. background-color: #0f0;
    16. }
    17. .bgc .three {
    18. /* 背景透明 */
    19. background-color: transparent;
    20. }
    21. style>
    22. head>
    23. <body>
    24. <div class="bgc">
    25. <div class="one">红色背景div>
    26. <div class="two">绿色背景div>
    27. <div class="three">透明背景div>
    28. div>
    29. body>
    30. html>

     

    2.背景图片

    background-image: url(...);

    image 更方便控制位置 ( 图片在盒子中的位置 )
    注意:
    • 1. url 不要遗漏.
    • 2. url 可以是绝对路径, 也可以是相对路径
    • 3. url 上可以加引号, 也可以不加.
    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. <style>
    8. .bgi .one {
    9. background-image: url(前端.jpg);
    10. height: 300px;
    11. }
    12. style>
    13. head>
    14. <body>
    15. <div class="bgi">
    16. <div class="one">背景图片div>
    17. div>
    18. body>
    19. html>

     

    3.背景平铺

    background-repeat: [平铺方式]

    重要取值:
    • repeat: 平铺
    • no-repeat: 不平铺
    • repeat-x: 水平平铺
    • repeat-y: 垂直平铺
    默认是 repeat.
            背景颜色和背景图片可以同时存在. 背景图片在背景颜色的上方.
    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. <style>
    8. .bgr .one {
    9. background-image: url(前端.jpg);
    10. height: 300px;
    11. background-repeat: no-repeat;
    12. }
    13. .bgr .two {
    14. background-image: url(前端.jpg);
    15. height: 300px;
    16. background-repeat: repeat-x;
    17. }
    18. .bgr .three {
    19. background-image: url(前端.jpg);
    20. height: 300px;
    21. background-repeat: repeat-y;
    22. }
    23. style>
    24. head>
    25. <body>
    26. <div class="bgr">
    27. <div class="one">不平铺div>
    28. <div class="two">水平平铺div>
    29. <div class="three">垂直平铺div>
    30. div>
    31. body>
    32. html>

    4.背景位置

    background-position: x y;

    修改图片的位置.

    参数有三种风格:
    • 1. 方位名词: (top, left, right, bottom)
    • 2. 精确单位: 坐标或者百分比(以左上角为原点)
    • 3. 混合单位: 同时包含方位名词和精确单位
    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. <style>
    8. .bgp .one {
    9. background-image: url(前端.jpg);
    10. height: 500px;
    11. background-repeat: no-repeat;
    12. background-color: purple;
    13. background-position: center;
    14. }
    15. style>
    16. head>
    17. <body>
    18. <div class="bgp">
    19. <div class="one">背景居中div>
    20. div>
    21. body>
    22. html>
    注意
    • 如果参数的两个值都是方位名词, 则前后顺序无关. (top left 和 left top 等效)
    • 如果只指定了一个方位名词, 则第二个默认居中. (left 则意味着水平居中, top 意味着垂直居中. )
    • 如果参数是精确值, 则的的第一个肯定是 x , 第二个肯定是 y. (100 200 意味着 x 为 100, y 为 200)
    • 如果参数是精确值, 且只给了一个数值, 则该数值一定是 x 坐标, 另一个默认垂直居中.
    • 如果参数是混合单位, 则第一个值一定为 x, 第二个值为 y 坐标. (100 center 表示横坐标为 100, 垂
    • 直居中)
    关于坐标系 :
    计算机中的平面坐标系 , 一般是左手坐标系 ( 和高中数学上常用的右手系不一样 . y 轴是往下指的 ).
            

    5.背景尺寸

    background-size : length | percentage | cover | contain ;
    • 可以填具体的数值: 如 40px 60px 表示宽度为 40px, 高度为 60px
    • 也可以填百分比: 按照父元素的尺寸设置.
    • cover: 把背景图像扩展至足够大,以使背景图像完全覆盖背景区域。背景图像的某些部分也许无
    • 法显示在背景定位区域中。
    • 把图像图像扩展至最大尺寸,以使其宽度和高度完全适应内容区域.
    注意体会 contain cover 的区别 . 当元素为矩形 ( 不是正方形 ) , 区别是很明显的 .
    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. <style>
    8. .bgs .one {
    9. width: 500px;
    10. height: 300px;
    11. background-image: url(前端.jpg);
    12. background-repeat: no-repeat;
    13. background-position: center;
    14. background-size: contain;
    15. }
    16. style>
    17. head>
    18. <body>
    19. <div class="bgs">
    20. <div class="one">背景尺寸div>
    21. div>
    22. body>
    23. html>
    contain:

    cover:

    八、圆角矩形

    通过 border-radius 使边框带圆角效果.

    1.基本用法

    border-radius: length;

    length 是内切圆的半径. 数值越大, 弧线越强烈
    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. <style>
    8. div {
    9. width: 200px;
    10. height: 100px;
    11. border: 2px solid green;
    12. border-radius: 10px;
    13. }
    14. style>
    15. head>
    16. <body>
    17. <div>蛤蛤div>
    18. body>
    19. html>

     

    2.生成圆形

    让 border-radius 的值为正方形宽度的一半即可.
    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. <style>
    8. div {
    9. width: 200px;
    10. height: 200px;
    11. border: 2px solid green;
    12. border-radius: 100px;
    13. /* 或者用 50% 表示宽度的一半 */
    14. border-radius: 50%;
    15. }
    16. style>
    17. head>
    18. <body>
    19. <div>蛤蛤div>
    20. body>
    21. html>

    3.生成圆角矩形

    让 border-radius 的值为矩形高度的一半即可
    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. <style>
    8. div {
    9. width: 200px;
    10. height: 100px;
    11. border: 2px solid green;
    12. border-radius: 50px;
    13. }
    14. style>
    15. head>
    16. <body>
    17. <div>蛤蛤div>
    18. body>
    19. html>

    4.展开写法

    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 调试工具 -- 查看 CSS 属性

    1.打开浏览器

    有两种方式可以打开 Chrome 调试工具
    • 直接按 F12 键
    • 鼠标右键页面 => 检查元素

    2.标签页含义

    • elements 查看标签结构
    • console 查看控制台
    • source 查看源码+断点调试
    • network 查看前后端交互过程
    • application 查看浏览器提供的一些扩展功能(本地存储等)
    • Performance, Memory, Security, Lighthouse

    3.elements 标签页使用

    • ctrl + 滚轮进行缩放, ctrl + 0 恢复原始大小.
    • 使用 左上角 箭头选中元素
    • 右侧可以查看当前元素的属性, 包括引入的类.
    • 右侧可以修改选中元素的 css 属性. 例如颜色, 可以点击颜色图标, 弹出颜色选择器, 修改颜色. 例如
    • 字体大小, 可以使用方向键来微调数值.
    • 此处的修改不会影响代码, 刷新就还原了~
    • 如果 CSS 样式写错了, 也会在这里有提示. (黄色感叹号)

    十、元素的显示模式

    在 CSS 中, HTML 的标签的显示模式有很多.
    此处只重点介绍两个:
    • 块级元素
    • 行内元素

    1.块级元素

    常见的元素:
    • h1 - h6
    • p
    • div
    • ul
    • ol
    • li
    • ....
    特点:
    • 独占一行
    • 高度, 宽度, 内外边距, 行高都可以控制.
    • 宽度默认是父级元素宽度的 100% (和父元素一样宽)
    • 是一个容器(盒子), 里面可以放行内和块级元素.
    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. <style>
    8. .demo1 .parent {
    9. width: 500px;
    10. height: 500px;
    11. background-color: green;
    12. }
    13. .demo1 .child {
    14. /* 不写 width, 默认和父元素一样宽 */
    15. /* 不写 height, 默认为 0 (看不到了) */
    16. height: 200px;
    17. background-color: red;
    18. }
    19. style>
    20. head>
    21. <body>
    22. <div class="demo1">
    23. <div class="parent">
    24. <div class="child">
    25. child1
    26. div>
    27. <div class="child">
    28. child2
    29. div>
    30. div>
    31. div>
    32. body>
    33. html>

     

    注意:

    • 文字类的元素内不能使用块级元素
    • p 标签主要用于存放文字, 内部不能放块级元素, 尤其是 div
       

     
    蛤蛤
       

    2.行内元素/内联元素

    常见的元素:
    • a
    • strong
    • b
    • em
    • i
    • del
    • s
    • ins
    • u
    • span
    特点:
    • 不独占一行, 一行可以显示多个
    • 设置高度, 宽度, 行高无效
    • 左右外边距有效(上下无效). 内边距有效.
    • 默认宽度就是本身的内容
    • 行内元素只能容纳文本和其他行内元素, 不能放块级元素
    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. <style>
    8. .demo2 span {
    9. width: 200px;
    10. height: 200px;
    11. background-color: red;
    12. }
    13. style>
    14. head>
    15. <body>
    16. <div class="demo2">
    17. <span>child1span>
    18. <span>child2span>
    19. <span>child3span>
    20. div>
    21. body>
    22. html>

    注意:

    • a 标签中不能再放 a 标签 (虽然 chrome 不报错, 但是最好不要这么做).
    • a 标签里可以放块级元素, 但是更建议先把 a 转换成块级元素.

    3.行内元素和块级元素的区别

    • 块级元素独占一行, 行内元素不独占一行
    • 块级元素可以设置宽高, 行内元素不能设置宽高.
    • 块级元素四个方向都能设置内外边距, 行内元素垂直方向不能设置.

    4.改变显示模式

    使用 display 属性可以修改元素的显示模式.
    • 可以把 div 等变成行内元素, 也可以把 a , span 等变成块级元素.
    • display: block 改成块级元素 [常用]
    • display: inline 改成行内元素 [很少用]
    • display: inline-block 改成行内块元素

    十一、盒模型

    每一个 HTML 元素就相当于是一个矩形的 "盒子"
    这个盒子由这几个部分构成
    • 边框 border
    • 内容 content
    • 内边距 padding
    • 外边距 margin