Skip to content

1.emmet语法

1.1 简介

Emmet语法的前身是Zen coding,它使用缩写,来提高html/css的编写速度, Vscode内部已经集成该语法。
快速生成HTML结构语法
快速生成CSS样式语法

1.2 快速生成HTML结构语法

  1. 生成标签直接输入标签名按tab键即可。比如div然后tab键,就可以生成<div></div>
  2. 如果想要生成多个相同标签加上*就可以了。比如div*3就可以快速生成3个div
  3. 如果有父子级关系的标签,可以用>。比如ul > li就可以了
  4. 如果有兄弟关系的标签,用+就可以了。比如 div+p
  5. 如果生成带有类名或者id名字的,直接写.demo或者#twotab 键就可以了。默认生成的是div标签,如果想生成其他标签,比如p标签就写p.demo,就生成了一个p标签类为demo的标签
  6. 如果生成的div类名是有顺序的,可以用自增符号 $。示例 p.demo$*5
  7. 如果想要在生成的标签内部写内容可以用 { }表示。示例 div{5}div{$}*5

1.3 快速生成CSS样式语法

CSS 基本采取简写形式即可

  1. 比如 w200tab 可以生成 width: 200px;
  2. 比如 lh26pxtab 可以生成 line-height: 26px;

1.4 快速格式化代码

Vscode快速格式化代码: shift+alt+f
也可以设置当我们保存页面的时候自动格式化代码:

  1. 文件 ------>【首选项】---------->【设置】;
  2. 搜索emmet.include;
  3. 在settings.json下的【工作区设置】中添加以下语句:
    "editor.formatOnType": true,
    "editor.formatOnSave": true

2.CSS的复合选择器

2.1 什么是复合选择器

  1. 在CSS中,可以根据选择器的类型把选择器分为基础选择器复合选择器,复合选择器是建立在基础选择器之上,对基本选择器进行组合形成的。
  2. 复合选择器是由两个或多个基础选择器,通过不同的方式组合而成的,可以更准确、更高效的选择目标元素(标签)
  3. 常用的复合选择器包括:后代选择器、子选择器、并集选择器、伪类选择器等等

2.2 后代选择器

后代选择器又称为包含选择器,可以选择父元素里面子元素。其写法就是把外层标签写在前面,内层标签写在后面,中间用空格分隔。当标签发生嵌套时,内层标签就成为外层标签的后代。

语法:

html
元素1 元素2 { 样式声明 }

上述语法表示选择元素 1 里面的所有元素 2 (后代元素)。

示例:

html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        ul li {
            color: pink;
        }
        .nav li {
            color: #6b1212;
        }
        .nav li a {
            color: #df16aa;
        }
    </style>
</head>
<body>
    <ol>
        <li>后代选择器ol li</li>
        <li>后代选择器ol li</li>
        <li>后代选择器ol li</li>
        <li>后代选择器ol li</li>
    </ol>
    <ul>
        <li>后代选择器ul li</li>
        <li>后代选择器ul li</li>
        <li>后代选择器ul li</li>
        <li>后代选择器ul li</li>
    </ul>
    <ul class="nav">
        <li>后代选择器ul li</li>
        <li>后代选择器ul li</li>
        <li>后代选择器ul li</li>
        <li><a href="#">a标签</a></li>
    </ul>
</body>
</html>

img_21.png

语法说明:

  1. 元素1 和 元素2 中间用空格隔开
  2. 元素1 是父级,元素2 是子级,最终选择的是元素2
  3. 元素2 可以是儿子,也可以是孙子等,只要是元素1 的后代即可
  4. 元素1 和 元素2 可以是任意基础选择器

2.3 子选择器

子元素选择器(子选择器)只能选择作为某元素的最近一级子元素。
(简单理解就是选亲儿子元素)

语法:

html
元素1>元素2 {样式声明}

上述语法表示选择元素1 里面的所有直接后代(子元素) 元素2。

示例:

html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div>a {
            color: pink;
        }
    </style>
</head>
<body>
    <div>
        <a href="#">我是儿子</a>
        <p>
            <a href="#">我是孙子</a>
        </p>
    </div>
</body>
</html>

img_22.png

语法说明:

  1. 元素1和元素2中间用>隔开
  2. 元素1是父级,元素2是子级,最终选择的是元素2
  3. 元素2 必须是亲儿子,其孙子、重孙之类都不归他管。你也可以叫他亲儿子选择器

2.4 并集选择器

并集选择器可以选择多组标签, 同时为他们定义相同的样式,通常用于集体声明。并集选择器是各选择器通过英文逗号,连接而成,任何形式的选择器都可以作为并集选择器的一部分。

语法:

html
元素1,元素2 {样式声明}

上述语法表示选择元素1 和 元素2。

html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div,p,ul>ol {
            color: pink;
        }
    </style>
</head>
<body>
    <div>
        <div>熊大</div>
        <p>熊二</p>
        <span>光头强</span><br>
        <a href="#">孙悟空</a>
        <ul>
            <ol>小猪佩奇</ol>
            <ol>猪妈妈</ol>
            <ol>猪爸爸</ol>
        </ul>
    </div>
</body>
</html>

img_23.png

语法说明:

  1. 元素1 和 元素2 中间用逗号隔开
  2. 逗号可以理解为和的意思
  3. 并集选择器通常用于集体声明

2.5 伪类选择器

伪类选择器用于向某些选择器添加特殊的效果,比如给链接添加特殊效果,或选择第1个,第n个元素。
伪类选择器书写最大的特点是用冒号(:)表示,比如 :hover 、 :first-child 。

2.5.1 链接伪类选择器

作用是给链接添加特殊效果。

语法:

伪类选择器说明示例
:link选择所有未被访问的链接a:link
:visited选择所有已被访问的链接a:visited
:hover选择鼠标指针位于其上的链接a:hover
:active选择活动链接(鼠标按下未弹起时)a:active
html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        /* 选择所有未被访问的链接 */
        a:link {
            color: red;
        }
        /* 选择所有已被访问的链接 */
        a:visited {
            color: yellow;
        }
        /* 选择鼠标指针位于其上的链接 */
        a:hover {
            color: blue;
        }
        /* 选择活动链接(鼠标按下未弹起时) */
        a:active {
            color: green;
        }

    </style>
</head>
<body>
    <a href="#7">链接伪类选择器示例1</a><br>
    <a href="#8">链接伪类选择器示例2</a><br>
    <a href="#9">链接伪类选择器示例3</a><br>
    <a href="#10">链接伪类选择器示例4</a>
</body>
</html>

img_24.png
img_25.png

注意事项:

  1. 为了确保生效,请按照LVHA的循顺序声明 :link-:visited-:hover-:active。
  2. 记忆法:love hate 或者 lv 包包 hao 。
  3. 因为 a 链接在浏览器中具有默认样式,所以我们实际工作中都需要给链接单独指定样式。
html
<!--a是标签选择器 选择所有a标签加上灰色样式 并且将下划线去掉-->
a {
   color: gray;
    text-decoration: none;
}
<!--使用链接伪类选择器 鼠标经过的时候变成蓝色-->
a:hover {
   color: blue;
}

2.5.2 焦点伪类选择器

:focus伪类选择器用于选取获得焦点的表单元素。
焦点就是光标,一般情况<input> 类表单元素才能获取

html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        input:focus {
            background-color: pink;
            color: red;
        }
    </style>
</head>
<body>
    <input type="text"></input>
    <input type="text"></input>
    <input type="text"></input>
</body>
</html>

img_26.png

2.6 复合选择器总结

选择器作用特征使用情况隔开符号及用法
后代选择器选择后代元素可以是子孙后代较多空格 .nav a
子选择器选择最近一级子元素只选亲儿子较少> div>p
并集选择器选择多组标签集体声明相同样式较多, div,p,ul>li
链接伪类选择器给链接添加特殊效果按LVHA顺序较多: a:hover
:focus选择器选择获得光标的表单主要针对input表单较少: input:focus

3.CSS的显示模式

3.1 什么是元素的显示模式

元素显示模式就是元素(标签)以什么方式进行显示,比如<div>自己占一行,比如一行可以放多个<span>
网页的标签非常多,在不同地方会用到不同类型的标签,了解他们的特点可以更好的布局我们的网页。
HTML元素一般分为块元素和行内元素两种类型。

3.2 元素显示模式的分类

3.2.1 块元素

常见的块元素<h1>-<h6><p><div><ul><ol><li>

块级元素特点:

  1. 比较霸道,自己独占一行
  2. 高度、宽度、外边距以及内边距都可以控制
  3. 宽度默认是容器父级宽度的100%
  4. 是一个容器及盒子,里面可以放行内或者块级元素
html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            width: 100px;
            height: 100px;
            background-color: pink;
        }
    </style>
</head>
<body>
    <div>我是div</div> 我是div之外的元素
</body>
</html>

img_27.png

注意:
文字类的元素内不能放块级元素
<p> 标签主要用于存放文字,因此 <p> 里面不能放块级元素,特别是不能放<div>
同理,<h1>~<h6>等都是文字类块级标签,里面也不能放其他块级元素

html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            width: 100px;
            height: 100px;
            background-color: pink;
        }
    </style>
</head>
<body>
    <p>我是p标签<div>我是div</div></p>

</body>
</html>

img_28.png

3.2.2 行内元素

<a><strong><b><em><i><del><s><ins><u><span>
<span> 标签是最典型的行内元素。有的地方也将行内元素称为内联元素。

行内元素的特点:

  1. 相邻行内元素在一行上,一行可以显示多个。
  2. 高、宽直接设置是无效的。
  3. 默认宽度就是它本身内容的宽度。
  4. 行内元素只能容纳文本或其他行内元素。
html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        span {
            width: 100px;
            height: 100px;
            background-color: pink;
        }
    
    </style>
</head>
<body>
    <span>第一个行内元素1111111</span><strong>第二个行内元素</strong><span>第三个行内元素</span><strong>第四个行内元素</strong>
</body>
</html>

img_29.png

注意事项:

  1. 链接里面不能再放链接
  2. 特殊情况链接<a>里面可以放块级元素,但是给<a>转换一下块级模式最安全

3.2.3 行内块元素

常见的行内块标签:<img /><input /><td>
它们同时具有块元素和行内元素的特点。有些资料称它们为行内块元素。

行内块元素的特点:

  1. 和相邻行内元素(行内块)在一行上,但是他们之间会有空白缝隙。
  2. 一行可以显示多个(行内元素特点)。
  3. 默认宽度就是它本身内容的宽度(行内元素特点)。
  4. 高度、行高、外边距以及内边距都可以控制(块级元素特点)。
html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        input {
            width: 250px;
            height: 50px;
            background-color: pink;
        }
    
    </style>
</head>
<body>
    <input type="text">
    <input type="text">
</body>
</html>

img_30.png

3.2.4 元素显示模式总结

元素模式特点常见标签
块级元素独占一行,可设置宽高,默认宽度100%,可包含行内或块级元素<div>,<p>,<h1>-<h6>,<ul>
行内元素一行显示多个,不可设置宽高,默认宽度为内容宽度,只能容纳文本或行内元素<span>,<a>,<strong>
行内块元素一行显示多个但有空隙,可设置宽高,默认宽度为内容宽度<img>,<input>,<td>

3.3 元素显示模式的转换

一个模式的元素需要另外一种模式的特性
比如想要增加链接 <a> 的触发范围。

转换方式:

转换方式描述示例代码
display: block将元素转换为块级元素a { display: block; }
display: inline将元素转换为行内元素div { display: inline; }
display: inline-block将元素转换为行内块元素span { display: inline-block; }
html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>
        a {
            width: 100px;
            height: 100px;
            background-color: pink;
            display:block;
        }

        div {
            width: 100px;
            height: 100px;
            background-color: rgb(219, 8, 43);
            display:inline;
        }
        span {
           width: 100px;
            height: 100px;
            background-color: rgb(23, 79, 209);
            display: inline-block;
        }

    </style>

</head>
<body>
    <a href="#">a标签行内元素转换成块元素</a>
    <a href="#">a标签行内元素转换成块元素</a>

    <div>div块元素转换成行内元素</div>
    <div>div块元素转换成行内元素</div>

    <span>span行内元素转换成行内块元素</span>
    <span>span行内元素转换成行内块元素</span>
</body>
</html>

img_31.png

3.4 Snipaste工具使用

Snipaste是一个截图工具,可以让你的截图贴在你的屏幕上。

下载地址:
下载直接解压运行即可

aiignore
通过网盘分享的文件:Snipaste-2.2.1-Beta2-x64.zip
链接: https://pan.baidu.com/s/1URrgvTlF5MoYMpQUBMgXyw 提取码: d9jd
--来自百度网盘超级会员v10的分享

常用快捷键:

  1. F1可以截图,同时测量大小,设置箭头,书写文字等。
  2. F3在桌面置顶显示
  3. 点击图片,alt可以取色(按下shift键可以切换取色模式)
  4. 按下esc取消图片显示

3.5 单行文字垂直居中的代码

让文字的行高等于盒子的高度 就可以让文字在当前盒子内垂直居中 img3.pngimg4.png

案例演示:

html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .span1 {
            display:block;
            width: 230px;
            height: 40px;
            line-height: 40px;
            background-color: orange;
        }
        .span2 {
            display:block;
            width: 230px;
            height: 40px;
            line-height: 20px;
            background-color: orange;
        }
        .span3 {
            display:block;
            width: 230px;
            height: 40px;
            line-height: 60px;
            background-color: orange;
        }
    </style>
</head>
<body>
    <span class="span1">单行文字居中示例</span>
    <hr>
    <span class="span2">单行文字偏上示例</span>
    <hr>
    <span class="span3">单行文字偏下示例</span>
</body>
</html>

img_34.png
行高的上空隙和下空隙把文字挤到中间了,如果行高小于盒子高度,文字会偏上,如果行高大于盒子高度,则文字偏下。

3.6 实战案例-简洁版小米侧边栏

img_32.png

html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        a {
            display:block;
            width: 230px;
            height: 40px;
            background-color: #55585a;
            font-size: 14px;
            color: #fff;
            text-decoration: none;
            text-indent: 2em;
            line-height: 40px;
        }
        a:hover {
            background-color: #ff6700;
        }
    </style>
</head>
<body>
    <a href="#">手机 电话卡</a>
    <a href="#">电视 盒子</a>
    <a href="#">笔记本 平板</a>
    <a href="#">家电 插线板</a>
    <a href="#">出行 穿戴</a>
    <a href="#">智能 路由器</a>
    <a href="#">电源 配件</a>
    <a href="#">健康 儿童</a>
    <a href="#">耳机 音响</a>
    <a href="#">生活 箱包</a>
</body>
</html>

img_33.png

4.CSS的背景

通过 CSS 背景属性,可以给页面元素添加背景样式。
背景属性可以设置背景颜色、背景图片、背景平铺、背景图片位置、背景图像固定等。

4.1 背景颜色

background-color 定义元素的背景颜色

使用方式:

html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            width: 100px;
            height: 100px;
            background-color: pink;
        }
    </style>
</head>
<body>
    <div></div>
</body>
</html>

img_35.png

其他说明:
元素背景颜色默认值是transparent(透明)

aiignore
background-color: transparent;

4.2 背景图片

background-image 定义元素的背景图片

使用方式:

属性值描述示例
none无背景图 (默认值)background-image: none
url("image.jpg")使用绝对或相对路径指定背景图片background-image: url("bg.png")
html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            height: 1000px;
            width: 1000px;
            background-image: url(背景.jpg);
        }
    </style>
</head>
<body>
    <div>背景图片</div>
</body>
</html>

img_36.png

其他说明:

  1. 实际开发常见于 logo 或者一些装饰性的小图片或者是超大的背景图片, 优点是非常便于控制位置. (精灵图也是一种运用场景)
  2. 背景图片后面的地址,千万不要忘记加 URL,同时里面的路径不要加引号
  3. div设置背景图片的同时还要设置宽高,不然背景图片不显示

4.3 背景平铺

background-repeat 设置元素背景图像的平铺

使用方式:

属性值描述示例
repeat背景图像在纵向和横向上平铺 (默认值)background-repeat: repeat
no-repeat背景图像不平铺background-repeat: no-repeat
repeat-x背景图像在横向上平铺background-repeat: repeat-x
repeat-y背景图像在纵向上平铺background-repeat: repeat-y
html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            height: 500px;
            width: 500px;
            background-color: aquamarine;
            background-image: url(爱心=focused.png);
            background-repeat: repeat;
        }
    </style>
</head>
<body>
    <div></div>
</body>
</html>

img_37.png

html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            height: 500px;
            width: 500px;
            background-color: aquamarine;
            background-image: url(爱心=focused.png);
            background-repeat: no-repeat;
        }
    </style>
</head>
<body>
    <div></div>
</body>
</html>

img_38.png

html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            height: 500px;
            width: 500px;
            background-color: aquamarine;
            background-image: url(爱心=focused.png);
            background-repeat: repeat-x;
        }
    </style>
</head>
<body>
    <div></div>
</body>
</html>

img_39.png

html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            height: 500px;
            width: 500px;
            background-color: aquamarine;
            background-image: url(爱心=focused.png);
            background-repeat: repeat-y;
        }
    </style>
</head>
<body>
    <div></div>
</body>
</html>

img_40.png

4.4 背景图片位置

background-position 属性可以改变图片在背景中的位置

4.4.1 参数-方位名词

可以使用topcenterbottomleftright等方位名词

使用方式:

html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            height: 500px;
            width: 500px;
            background-color: aquamarine;
            background-image: url(爱心=focused.png);
            background-repeat: no-repeat;
            background-position: top center;
        }
    </style>
</head>
<body>
    <div></div>
</body>
</html>

img_41.png

其他说明:

  1. 如果指定的两个值都是方位名词,则两个值前后顺序无关,比如 left top 和 top left 效果一致
  2. 如果只指定了一个方位名词,另一个值省略,则第二个值默认居中对齐

使用案例:

  1. 使用所学知识实现一个用户登录功能
html
<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>Document</title>
   <style>
      h3 {
         width: 120px;
         height: 50px;
         background-color: rgb(192, 255, 201);
         font-size: 14px;
         font-weight: 400;
         line-height: 51px;
         background-image: url(image.png);
         background-repeat: no-repeat;
         background-position: left;
         text-indent: 3em;
      }
      a {
         text-decoration: none;
      }
   </style>
</head>
<body>
<h3><a href="#">用户登录</a></h3>
</body>
</html>

img_42.png

  1. 完成一个超大背景图片展示
html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            background-image: url(bg.png);
            background-position: top center;
            background-repeat: no-repeat;
        }
    </style>
</head>
<body>
    
</body>
</html>

img_43.png

4.4.2 参数-精确位置

如果参数值是精确坐标,那么第一个肯定是 x 坐标,第二个一定是 y 坐标
如果只指定一个数值,那该数值一定是 x 坐标,另一个默认垂直居中

html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
.div1 {
            height: 300px;
            width: 300px;
            background-color: aquamarine;
            background-image: url(爱心=focused.png);
            background-repeat: no-repeat;
            background-position: 20px 50px;
        }
        .div2 {
            height: 300px;
            width: 300px;
            background-color: aquamarine;
            background-image: url(爱心=focused.png);
            background-repeat: no-repeat;
            background-position: 50px 20px;
        }
    </style> 
</head>
<body>
    <div class="div1"></div>
    <hr>
    <div class="div2"></div>
</body>
</html>

img_44.png

4.4.3 参数-混合单位

如果指定的两个值是精确单位和方位名词混合使用,则第一个值是 x 坐标,第二个值是 y 坐标

html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
.div1 {
            height: 300px;
            width: 300px;
            background-color: aquamarine;
            background-image: url(爱心=focused.png);
            background-repeat: no-repeat;
            background-position: 10px center;
        }
        .div2 {
            height: 300px;
            width: 300px;
            background-color: aquamarine;
            background-image: url(爱心=focused.png);
            background-repeat: no-repeat;
            background-position: center 10px;
        }
    </style> 
</head>
<body>
    <div class="div1"></div>
    <hr>
    <div class="div2"></div>
</body>
</html>

img_45.png

4.5 背景图片固定

background-attachment 属性设置背景图像是否固定或者随着页面的其余部分滚动。

使用方式:

属性值描述示例
scroll背景图像随页面内容滚动 (默认值)background-attachment: scroll
fixed背景图像固定在视口中background-attachment: fixed
local背景图像随元素内容滚动background-attachment: local
html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>
        body {
            background-image: url(bg.png);
            background-position: top center;
            background-repeat: no-repeat;
            background-attachment: fixed;
        }
    </style>
</head>
<body>

    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    <p>背景图片固定演示</p>
    
</body>
</html>

img_46.png

其他说明:
background-attachment 后期可以制作视差滚动的效果。

4.6 背景样式合写

background: 背景颜色 背景图片地址 背景平铺 背景图像滚动 背景图片位置;

使用方式:

html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>
        body {
            background: url(bg.png) top center no-repeat fixed;
        }
    </style>
</head>
<body>
    <p>背景合写演示</p>  
</body>
</html>

4.7 背景色半透明

CSS3 提供了背景颜色半透明的效果。

使用方式:

  1. 最后一个参数是 alpha 透明度,取值范围在 0~1之间
  2. 我们习惯把 0.3 的 0 省略掉,写为 background: rgba(0, 0, 0, .3);
html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            height: 300px;
            width: 300px;
            background: rgba(0, 0, 0, 0.3);
        }
    </style>
</head>
<body>
    <div></div>
</body>
</html>

img_47.png

注意事项:

  1. 背景半透明是指盒子背景半透明,盒子里面的内容不受影响
  2. CSS3 新增属性,是 IE9+ 版本浏览器才支持的,但是现在实际开发,我们不太关注兼容性写法了,可以放心使用

4.8 背景总结

属性作用
background-color背景颜色颜色值/transparent
background-image背景图片none/url()
background-repeat背景平铺repeat/no-repeat/repeat-x/repeat-y
background-position背景位置方位名词(top/center/bottom/left/right) 或 精确坐标(x y)
background-attachment背景附着scroll(滚动)/fixed(固定)
background背景简写颜色 图片 平铺 滚动 位置 (顺序无强制要求)
background:rgba()背景色半透明rgba(红,绿,蓝,透明度) 透明度0-1

5.综合案例

根据所学知识实现一个鼠标放上去会变样式的导航栏

html
<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>Document</title>
   <style>
      .nav a {
         display: inline-block;
         height: 58px;
         width: 120px;
         background-color: pink;
         text-align: center;
         line-height: 48px;
         color: azure;
         text-decoration: none;
      }
      .nav .a1 {
         background: url(images/bg1.png) no-repeat;
      }
      .nav .a1:hover {
         background-image: url(images/bg2.png);
      }
      .nav .a2 {
         background: url(images/bg3.jpg) no-repeat;
      }
      .nav .a2:hover {
         background-image: url(images/bg3.png);
      }
      .nav .a3 {
         background: url(images/bg4.png) no-repeat;
      }
      .nav .a3:hover {
         background-image: url(images/bg5.png);
      }
      .nav .a4 {
         background: url(images/bg22.png) no-repeat;
      }
      .nav .a4:hover {
         background-image: url(images/bg11.png);
      }
   </style>
</head>
<body>
<div class="nav">
   <a href="#" class="a1">五彩导航</a>
   <a href="#" class="a2">五彩导航</a>
   <a href="#" class="a3">五彩导航</a>
   <a href="#" class="a4">五彩导航</a>
</div>
</body>
</html>

img_48.png

Released under the MIT License.