昨日内容回顾

浮动:是css中布局最多的一个属性

有浮动,一定要清除浮动

浮动不是一个元素单独浮动,要浮动一起浮动

清除浮动四种方式:

1.给父盒子添加高度,一般导航栏

2.给浮动元素后面加一个空的块标签,

并且设置clear:both

3. 伪元素清除法

  1. 给父元素设置
  1. .clearfix:after{
  2. content:'.',
  3. display:block;
  4. clear:both;
  5. height:0;
  6. visibiable:hidden;
  7. }

4.给父元素overflow:hidden

块元素与行内元素的转换

块—->行内 display:inline

行内—->块 display:block

行内—->行内块 display: inline-block; input

标准流下的盒子居中:

  1. 一定要有明确的width
  2. margin: 0 auto;
  3. 文本居中 text-align:center;

如果盒子浮动了,margin: 0 auto;就失效了

标准下的盒子,不浮动的盒子,一定要善于使用父padding,而不是margin

margin塌陷问题:

标准流的盒,垂直方向会出现塌陷问题,这个问题没法解决,这种叫‘奇技淫巧’

盒子浮动了,垂直方向上不出现任何问题

水平方向,不管是标准流下的盒子还是浮动的盒子,都不会出现任何问题

文本属性:

text-align:设置文本的对齐方式

text-decoration: 设置文本的装饰

text-indent: 设置首字母缩进 em

line-height: 一行的高度

font-size字体大小一定不要大于行高

font-size:16px;

line-height:60px;

如果让单行文本垂直水平居中:

line-height=盒子的height,text-align:center;

文本属性和字体属性

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <style>
  7. div{
  8. width: 20em;
  9. height: 5em;
  10. border: 1px solid green;
  11. color: red;
  12. /*文本首行缩进*/
  13. text-indent: 1em;
  14. /*字体属性*/
  15. font-size: 20px;
  16. font-weight: bolder;
  17. /*字体要加多个,用逗号隔开。防止别人的电脑上,没有对应的字体。
  18. 首先加载的字体为微软雅黑,如果没有,就从后面找。*/
  19. font-family: "Microsoft Yahei", "微软雅黑", "Arial", sans-serif;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <div>韩雪,赵丽颖,宋茜</div>
  25. </body>
  26. <html>

网页效果:

  1. ![](https://cdn.nlark.com/yuque/0/2020/png/1484428/1598489114521-d3df9e7a-86b5-4933-80d9-bdd6ea48da68.png)

多行文本垂直居中

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <style>
  7. div{
  8. width: 350px;
  9. height: 200px;
  10. background-color: pink;
  11. margin: 100 auto;
  12. color: red;
  13. line-height: 40px;
  14. font-weight: bolder;
  15. font-size: 20px;
  16. /*先计算字体大小,再调整padding的参数*/
  17. padding-top: 40px;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div>赵丽颖,1987年10月16日出生于河北省廊坊市,中国内地影视女演员。2006年,因获得雅虎搜星比赛冯小刚组冠军而进入演艺圈</div>
  23. </body>
  24. <html>

网页效果:

  1. ![](https://cdn.nlark.com/yuque/0/2020/png/1484428/1598489114492-6bac09d6-7b8f-4e0f-82e1-fdbbc625da74.png)

分析,由于总高度为 200。文本有 4 行,每行高度为 40。那么 4 行的高度为 160,所以 padding-top 的 40。

正好等于 200。实现了多行居中效果!

一、超链接导航栏案例

直接上代码了

html 结构

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style>
  7. *{
  8. padding: 0;
  9. margin: 0;
  10. }
  11. ul{
  12. list-style: none;
  13. }
  14. .nav{
  15. width: 960px;
  16. overflow: hidden;
  17. margin: 50px auto;
  18. background-color: purple;
  19. /*设置圆角*/
  20. border-radius: 5px;
  21. }
  22. .nav ul li{
  23. float: left;
  24. width: 160px;
  25. height: 40px;
  26. line-height: 40px;
  27. text-align: center;
  28. }
  29. .nav ul li a{
  30. width: 160px;
  31. height: 40px;
  32. display: block;
  33. color: white;
  34. font-size: 14px;
  35. text-decoration: none;
  36. }
  37. /*a标签除外,不继承父元素的color*/
  38. .nav ul li a:hover{
  39. background: yellow;
  40. color: green;
  41. /*添加下划线效果*/
  42. text-decoration: underline;
  43. }
  44. </style>
  45. </head>
  46. <body>
  47. <div class="nav">
  48. <ul>
  49. <li>
  50. <a href="#">网站导航</a>
  51. </li>
  52. <li>
  53. <a href="#">网站导航</a>
  54. </li>
  55. <li>
  56. <a href="#">网站导航</a>
  57. </li>
  58. <li>
  59. <a href="#">网站导航</a>
  60. </li>
  61. <li>
  62. <a href="#">网站导航</a>
  63. </li>
  64. <li>
  65. <a href="#">网站导航</a>
  66. </li>
  67. </ul>
  68. </div>
  69. </body>
  70. </html>

写好上面的结构代码之后,也就是将我们页面展示的内容显示了,但是我们此时要利用我们学过的知识点来布局页面

首先我们要做导航栏,并排显示元素,第一想 浮动,想到使用浮动之后,一定记得清除浮动元素。

网页效果:

  1. ![](https://cdn.nlark.com/yuque/0/2020/png/1484428/1598489115353-1379c3d5-0bd8-4d89-a0f6-bbc072ddc37d.png)

二、background

先来讲讲颜色表示法

一共有三种:单词、rgb 表示法、十六进制表示法

rgb:红色 绿色 蓝色 三原色

光学显示器,每个像素都是由三原色的发光原件组成的,靠明亮度不同调成不同的颜色的。

用逗号隔开,r、g、b 的值,每个值的取值范围 0~255,一共 256 个值。

如果此项的值,是 255,那么就说明是纯色:

黑色:

光学显示器,每个元件都不发光,黑色的。

白色:

颜色可以叠加,比如黄色就是红色和绿色的叠加:

再比如:

就是红、绿、蓝三种颜色的不同比例叠加。

16 进制表示法

红色:

所有用#开头的值,都是 16 进制的。

ff0000:红色

16 进制表示法,也是两位两位看,看 r、g、b,但是没有逗号隔开。

ff 就是 10 进制的 255,00 就是 10 进制的 0,00 就是 10 进制的 0。所以等价于 rgb(255,0,0);

怎么换算的?我们介绍一下

我们现在看一下 10 进制中的基本数字(一共 10 个):

0、1、2、3、4、5、6、7、8、9

16 进制中的基本数字(一共 16 个):

0、1、2、3、4、5、6、7、8、9、a、b、c、d、e、f

16 进制对应表:

十进制数 十六进制数

0 0

1 1

2 2

3 3

……

10 a

11 b

12 c

13 d

14 e

15 f

16 10

17 11

18 12

19 13

……

43 2b

……

255 ff

十六进制中,13 这个数字表示什么?

表示 1 个 16 和 3 个 1。 那就是 19。 这就是位权的概念,开头这位表示多少个 16,末尾这位表示多少个 1。

小练习:

16 进制中 28 等于 10 进制多少?

答:2*16+8 = 40。

16 进制中的 2b 等于 10 进制多少?

答:2*16+11 = 43。

16 进制中的 af 等于 10 进制多少?

答:10 * 16 + 15 = 175

16 进制中的 ff 等于 10 进制多少?

答:15*16 + 15 = 255

所以,#ff0000 就等于 rgb(255,0,0)

等价于:

所以,任何一种十六进制表示法,都能够换算成为 rgb 表示法。也就是说,两个表示法的颜色数量,一样。

十六进制可以简化为 3 位,所有#aabbcc 的形式,能够简化为#abc;

比如:

等价于

比如:

等价于

只能上面的方法简化,比如

无法简化!

再比如

无法简化!

要记住:

000 黑

fff 白

f00 红

333 灰

222 深灰

ccc 浅灰

background-color 属性表示背景颜色

举例:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <style>
  7. div{
  8. width: 200px;
  9. height: 200px;
  10. font-weight: bolder;
  11. /*单词 十六进制 rgb*/
  12. background-color: rgb(255,0,0);
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <div></div>
  18. </body>
  19. </html>

网页效果:

  1. ![](https://cdn.nlark.com/yuque/0/2020/png/1484428/1598489114592-2d0fb942-fb6d-4b44-9dd5-b5415ea4176c.png)

background-img:表示设置该元素的背景图片

举例:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <style>
  7. div{
  8. width: 600px;
  9. height: 300px;
  10. /*背景图片*/
  11. background-image: url("images/zly.jpg");
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div></div>
  17. </body>
  18. </html>
  1. ![](https://cdn.nlark.com/yuque/0/2020/png/1484428/1598489114513-13cbf44d-274e-4899-a345-1527b1522f3c.png)

那么发现默认的背景图片,水平方向和垂直方向都平铺

background-repeat:表示设置该元素平铺的方式

属性值:

描述
repeat 默认。背景图像将在垂直方向和水平方向重复。
repeat-x 背景图像将在水平方向重复。
repeat-y 背景图像将在垂直方向重复。
no-repeat 背景图像将仅显示一次。
inherit 规定应该从父元素继承 background-repeat 属性的设置。

背景图片仅显示一次

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <style>
  7. div{
  8. width: 600px;
  9. height: 300px;
  10. /*边框*/
  11. border: 1px solid red;
  12. /*背景图片*/
  13. background-image: url("images/zly.jpg");
  14. background-repeat: no-repeat;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div></div>
  20. </body>
  21. </html>

网页效果:

  1. ![](https://cdn.nlark.com/yuque/0/2020/png/1484428/1598489115476-39305f57-4db1-4a88-91cf-127b5c6bd405.png)

给元素设置 padding 之后,发现 padding 的区域也会平铺背景图片。

举例:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <style>
  7. div{
  8. width: 600px;
  9. height: 300px;
  10. /*边框*/
  11. border: 1px solid red;
  12. /*背景图片*/
  13. background-image: url("images/zly.jpg");
  14. padding: 100px;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div></div>
  20. </body>
  21. </html>

网页效果:

  1. ![](https://cdn.nlark.com/yuque/0/2020/png/1484428/1598489114878-64b7b194-b858-42e9-8606-083716ef8a6d.png)

repeat 应用案例

还是上面那个超链接导航栏的案例,我们给 body 设置平铺的图片,注意:一定找左右对称的平铺图片,才能实现我们要的效果

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style>
  7. *{
  8. padding: 0;
  9. margin: 0;
  10. }
  11. body{
  12. background-image: url("images/backdrop.jpg");
  13. }
  14. ul{
  15. list-style: none;
  16. }
  17. .nav{
  18. width: 960px;
  19. overflow: hidden;
  20. margin: 50px auto;
  21. background-color: purple;
  22. /*设置圆角*/
  23. border-radius: 5px;
  24. }
  25. .nav ul li{
  26. float: left;
  27. width: 160px;
  28. height: 40px;
  29. line-height: 40px;
  30. text-align: center;
  31. }
  32. .nav ul li a{
  33. width: 160px;
  34. height: 40px;
  35. display: block;
  36. color: white;
  37. font-size: 14px;
  38. text-decoration: none;
  39. }
  40. /*a标签除外,不继承父元素的color*/
  41. .nav ul li a:hover{
  42. background: yellow;
  43. color: green;
  44. /*添加下划线效果*/
  45. text-decoration: underline;
  46. }
  47. </style>
  48. </head>
  49. <body>
  50. <div class="nav">
  51. <ul>
  52. <li>
  53. <a href="#">网站导航</a>
  54. </li>
  55. <li>
  56. <a href="#">网站导航</a>
  57. </li>
  58. <li>
  59. <a href="#">网站导航</a>
  60. </li>
  61. <li>
  62. <a href="#">网站导航</a>
  63. </li>
  64. <li>
  65. <a href="#">网站导航</a>
  66. </li>
  67. <li>
  68. <a href="#">网站导航</a>
  69. </li>
  70. </ul>
  71. </div>
  72. </body>
  73. </html>

网页效果:

  1. ![](https://cdn.nlark.com/yuque/0/2020/png/1484428/1598489114566-3e02a884-7c7e-45b8-9b19-29a87c790622.png)

background-position: 属性设置背景图像的起始位置。这个属性设置背景原图像(由 background-image 定义)的位置

属性值:

描述
top left
top center
top right
center left
center center
center right
bottom left
bottom center
bottom right
如果您仅规定了一个关键词,那么第二个值将是”center”。
默认值:0 0;
这两个值必须挨在一起。

举例:

看上面那个不重复的例子,如果需要设置图片居中,需要使用 background-position 方法。

使用 x 和 y 坐标来定位图片。

背景图片居中

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <style>
  7. div{
  8. width: 600px;
  9. height: 300px;
  10. /*边框*/
  11. border: 1px solid red;
  12. /*背景图片*/
  13. background-image: url("images/zly.jpg");
  14. background-repeat: no-repeat;
  15. /*表示x坐标为100,y坐标为0*/
  16. background-position: 200px 0px;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <div></div>
  22. </body>
  23. </html>

网页效果:

  1. ![](https://cdn.nlark.com/yuque/0/2020/png/1484428/1598489114592-3c2d9f86-3719-42b2-a07b-448edb5a4bac.png)

雪碧图技术(精灵图技术)

CSS 雪碧 即 CSS Sprite ,也有人叫它 CSS 精灵,是一种 CSS 图像合并技术,该方法是将小图标和背景图像合并到一张图片上,然后利用 css 的背景定位来显示需要显示的图片部分

CSS 雪碧图应用原理:

只有一张大的合并图, 每个小图标节点如何显示单独的小图标呢?

其实就是 截取 大图一部分显示,而这部分就是一个小图标。

比如淘宝网

  1. ![](https://cdn.nlark.com/yuque/0/2020/png/1484428/1598489114539-19faf36f-065e-4c1f-9fb0-c25a65cda19b.png)

使用雪碧图好处:

1、利用 CSS Sprites 能很好地减少网页的 http 请求,从而大大的提高页面的性能,这也是 CSS Sprites 最大的优点,也是其被广泛传播和应用的主要原因;

2、CSS Sprites 能减少图片的字节,曾经比较过多次 3 张图片合并成 1 张图片的字节总是小于这 3 张图片的字节总和。

3、解决了网页设计师在图片命名上的困扰,只需对一张集合的图片上命名就可以了,不需要对每一个小元素进行命名,从而提高了网页的制作效率。

4、更换风格方便,只需要在一张或少张图片上修改图片的颜色或样式,整个网页的风格就可以改变。维护起来更加方便

不足 :

1)CSS 雪碧的最大问题是内存使用

2)拼图维护比较麻烦

3)使 CSS 的编写变得困难

4)CSS 雪碧调用的图片不能被打印

切图

必须使用谷歌浏览器打开淘宝主页,搜索华为 P20,鼠标放到搜索框位置,右键检查

  1. ![](https://cdn.nlark.com/yuque/0/2020/png/1484428/1598489114524-346d7c4d-bea4-4989-b413-d0d9635f7543.png)

滑动精度条,找到右边的图片,右键 Open new in tab

  1. ![](https://cdn.nlark.com/yuque/0/2020/png/1484428/1598489114604-7305817a-9966-423d-84be-4c5951e27456.png)

就会打开图片连接

https://img.alicdn.com/tps/i2/TB1Z4WGMpXXXXbyaXXX6sGuHVXX-458-458.png

下面开始演示如何切一个搜索,如下图红包边框的搜索

  1. ![](https://cdn.nlark.com/yuque/0/2020/png/1484428/1598489114533-18f736fd-d02f-480c-a9dd-b2e70410478a.png)

先下载完整的图片,使用 ps 打开,获取搜索图片的坐标(注意,是下图箭头的坐标)

从坐标中可以看出 x 和 y 的坐标为 6,124。图片的宽度和高度分别为 80,34

  1. ![](https://cdn.nlark.com/yuque/0/2020/png/1484428/1598489114613-c2eafda2-5fa9-44bf-8677-d5cfb0e2374a.png)

注意:css 中的 background 设置的坐标必须为负数

因为 background 的坐标和平常见到的坐标不一样,如下图

  1. ![](https://cdn.nlark.com/yuque/0/2020/png/1484428/1598489114532-b9697b16-d16c-424b-bfaa-338f15ae4629.png)
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <style>
  7. div{
  8. width: 80px;
  9. height: 34px;
  10. /*坐标必须为负数*/
  11. background: url("images/taobao.png") no-repeat -5px -124px;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div></div>
  17. </body>
  18. </html>

网页效果:

  1. ![](https://cdn.nlark.com/yuque/0/2020/png/1484428/1598489114568-d000f605-a036-49aa-bc13-7b9b4897a356.png)

通天 banner

我们可以使用 background 综合属性制作通天 banner,什么是通天 banner 呢,就是一般我们电脑的屏幕都是 1439.但是设计师给我们的 banner 图都会比这个大,

那么我们可以此属性来制作通天 banner。

  1. background: red url('./images/banner.jpg') no-repeat center top;

举例-小米金融:

打开小米金融页面

https://jr.mi.com/index.html

下载首页最大的图片,链接为:

https://fundres.mipay.com/res/pc/images/index_banner-744e8a4eb7.jpg

发现它的尺寸为 2560x640,但是一般笔记本的分辨率,最大为 1366。

那么如何保证图片居中,而不失真呢?

使用 background,关键性参数 center top,表示水平顶部居中

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <style>
  7. div{
  8. width: 100%;
  9. height: 640px;
  10. /*center top表示 水平顶部居中*/
  11. background: url("images/banne.jpg") no-repeat center top;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div></div>
  17. </body>
  18. </html>

网页效果:

  1. ![](https://cdn.nlark.com/yuque/0/2020/png/1484428/1598489115371-c78dc910-abec-4603-8608-95206dcce773.png)

background-attachment

设置 fixed 之后,该属性固定背景图片不随浏览器的滚动而滚动

举例:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <style>
  7. div{
  8. width: 1000px;
  9. height: 1000px;
  10. background-image: url("images/taobao_logo.png");
  11. border: 1px solid red;
  12. background-repeat: no-repeat;
  13. background-position: right left;
  14. /*固定背景图片*/
  15. background-attachment: fixed;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <div></div>
  21. </body>
  22. </html>

网页效果:

拖动右边的进度条,logo 始终展示。就好像右边烦人的小广告一样。

  1. ![](https://cdn.nlark.com/yuque/0/2020/png/1484428/1598489114583-e370aa5a-8d36-49c0-a121-026f4623ce9d.png)

三、定位

定位有三种:

1.相对定位 2.绝对定位 3.固定定位

这三种定位,每一种都暗藏玄机,所以我们要一一单讲。

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <style>
  7. body{
  8. border: 1px solid green;
  9. }
  10. div{
  11. width: 200px;
  12. height: 200px;
  13. background-color: red;
  14. /*margin-top: 50px;*/
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div class="wrap">
  20. </div>
  21. </body>
  22. </html>

网页效果:

  1. ![](https://cdn.nlark.com/yuque/0/2020/png/1484428/1598489114634-ccb4ba21-6899-4834-b47c-20df35d581b4.png)

如果想让红色方块下移 50px,我们首先想到的是使用 margin-top 50px

打开代码注释的部分,刷新页面。会发现,body 被撑开了。这不是我们想要的,这个时候,需要用到相对定位。

它不会撑开 body

相对定位

相对定位:相对于自己原来的位置定位

现象和使用:

1.如果对当前元素仅仅设置了相对定位,那么与标准流的盒子没有什么区别。

2.设置相对定位之后,我们才可以使用四个方向的属性:top、bottom、left、right

特性:

1.不脱标

2.形影分离

3.老家留坑(占着茅房不拉屎,恶心人)

所以说相对定位 在页面中没有什么太大的作用。影响我们页面的布局。我们不要使用相对定位来做压盖效果

用途:

1.微调元素位置

2.做绝对定位的参考(父相子绝)绝对定位会说到此内容。

参考点:

自己原来的位置做参考点。

使用相对定位,移动红色方块 50 像素

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <style>
  7. body{
  8. border: 1px solid green;
  9. }
  10. div{
  11. width: 200px;
  12. height: 200px;
  13. background-color: red;
  14. /*相对定位,相对自己原来的位置,跟父级没有任何关系*/
  15. position: relative;
  16. /*移动50px*/
  17. top: 50px;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div class="wrap">
  23. </div>
  24. </body>
  25. </html>

网页效果:

  1. ![](https://cdn.nlark.com/yuque/0/2020/png/1484428/1598489115294-9908de25-0bd1-491f-af14-dc397a80069e.png)

它没有撑开 body,只是超出范围了。

老家留坑现象:

先看 3 个方块

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <style>
  7. .box1{
  8. width: 100px;
  9. height: 100px;
  10. background-color: red;
  11. }
  12. .box2{
  13. width: 100px;
  14. height: 100px;
  15. background-color: green;
  16. }
  17. .box3{
  18. width: 100px;
  19. height: 100px;
  20. background-color: yellow;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <div class="box1">
  26. </div>
  27. <div class="box2">
  28. </div>
  29. <div class="box3">
  30. </div>
  31. </body>
  32. </html>

网页效果:

  1. ![](https://cdn.nlark.com/yuque/0/2020/png/1484428/1598489115454-b8fe8cfa-346e-4ad2-ae46-405dd89238e2.png)

移动绿色方块

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <style>
  7. .box1{
  8. width: 100px;
  9. height: 100px;
  10. background-color: red;
  11. }
  12. .box2{
  13. width: 100px;
  14. height: 100px;
  15. background-color: green;
  16. position: relative;
  17. left: 100px;
  18. }
  19. .box3{
  20. width: 100px;
  21. height: 100px;
  22. background-color: yellow;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <div class="box1">
  28. </div>
  29. <div class="box2">
  30. </div>
  31. <div class="box3">
  32. </div>
  33. </body>
  34. </html>

网页效果:

  1. ![](https://cdn.nlark.com/yuque/0/2020/png/1484428/1598489114588-c6d29167-18c4-4a28-a50a-322f5866558c.png)

可以看到,原来的位置占用着,黄色方块挤不上去。这就是老家留坑现象,它影响我们页面的布局

不要用相对定位做遮盖现象

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <style>
  7. .box1{
  8. width: 100px;
  9. height: 100px;
  10. background-color: red;
  11. }
  12. .box2{
  13. width: 100px;
  14. height: 100px;
  15. background-color: green;
  16. position: relative;
  17. left: 50px;
  18. top: 50px;
  19. }
  20. .box3{
  21. width: 100px;
  22. height: 100px;
  23. background-color: yellow;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <div class="box1">
  29. </div>
  30. <div class="box2">
  31. </div>
  32. <div class="box3">
  33. </div>
  34. </body>
  35. </html>

网页效果:

  1. ![](https://cdn.nlark.com/yuque/0/2020/png/1484428/1598489114587-2303a9d2-4c52-4609-aa39-5f9cb031a098.png)

相对定位的用途:

让搜索框和提交按钮在一条水平线显示

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <style>
  7. .where{
  8. font-size: 30px;
  9. }
  10. .search{
  11. width: 100px;
  12. height: 40px;
  13. position: relative;
  14. top: -4px;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div>
  20. <form action="" method="post">
  21. <input type="text" class="where">
  22. <input type="submit" class="search" value="搜索">
  23. </form>
  24. </div>
  25. </body>
  26. </html>

网页效果:

  1. ![](https://cdn.nlark.com/yuque/0/2020/png/1484428/1598489114634-ef0fc826-f1eb-42f2-b49c-72be1b14310a.png)

绝对定位

特性:

1.脱标 2.做遮盖效果,提成了层级。设置绝对定位之后,不区分行内元素和块级元素,都能设置宽高。

参考点(重点):

一、单独一个绝对定位的盒子

1.当我使用 top 属性描述的时候 是以页面的左上角(跟浏览器的左上角区分)为参考点来调整位置

2.当我使用 bottom 属性描述的时候。是以首屏页面左下角为参考点来调整位置。

举例,使用 top

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <style>
  7. .wrap{
  8. width: 400px;
  9. height: 400px;
  10. padding: 100px;
  11. background-color: yellow;
  12. /*绝对定位*/
  13. position: relative;
  14. }
  15. .container{
  16. width: 500px;
  17. height: 500px;
  18. background-color: green;
  19. position: relative;
  20. padding: 20px;
  21. }
  22. .box1{
  23. width: 200px;
  24. height: 200px;
  25. background-color: red;
  26. position: absolute;
  27. top: 100px;
  28. }
  29. </style>
  30. </head>
  31. <body style='height: 2000px;'>
  32. <div class="wrap">
  33. <div class="container">
  34. <div class="box1">
  35. </div>
  36. </div>
  37. </div>
  38. </body>
  39. </html>

网页效果:

  1. ![](https://cdn.nlark.com/yuque/0/2020/png/1484428/1598489114610-220a8678-5141-465e-a370-6be1609df1e3.png)

二、以父辈盒子作为参考点

1.父辈元素设置相对定位,子元素设置绝对定位,那么会以父辈元素左上角为参考点,这个父辈元素不一定是爸爸,它也可以是爷爷,曾爷爷。

2.如果父亲设置了定位,那么以父亲为参考点。那么如果父亲没有设置定位,那么以父辈元素设置定位的为参考点

3.不仅仅是父相子绝,父绝子绝 ,父固子绝,都是以父辈元素为参考点

注意了:父绝子绝,没有实战意义,做站的时候不会出现父绝子绝。因为绝对定位脱离标准流,影响页面的布局。相反‘父相子绝’在我们页面布局中,是常用的布局方案。因为父亲设置相对定位,不脱离标准流,子元素设置绝对定位,仅仅的是在当前父辈元素内调整该元素的位置。

还要注意,绝对定位的盒子无视父辈的 padding

作用:页面布局常见的“父相子绝”,一定要会!!!!

绝对定位的盒子居中

设置绝对定位之后,margin:0 auto;不起任何作用,如果想让绝对定位的盒子居中。当做公式记下来 设置子元素绝对定位,然后 left:50%; margin-left 等于元素宽度的一半,实现绝对定位盒子居中

当做公式记下来吧!

  1. *{
  2. padding: 0;
  3. margin: 0;
  4. }
  5. .box{
  6. width: 100%;
  7. height: 69px;
  8. background: #000;
  9. }
  10. .box .c{
  11. width: 960px;
  12. height: 69px;
  13. background-color: pink;
  14. /*margin: 0 auto;*/
  15. position: relative;
  16. left: 50%;
  17. margin-left: -480px;
  18. /*设置绝对定位之后,margin:0 auto;不起任何作用,如果想让绝对定位的盒子居中。当做公式记下来 设置子元素绝对定位,然后left:50%; margin-left等于元素宽度的一半,实现绝对定位盒子居中*/
  19. }

固定定位

固定当前的元素不会随着页面滚动而滚动

特性:

1.脱标 2.遮盖,提升层级 3.固定不变

参考点:

设置固定定位,用 top 描述。那么是以浏览器的左上角为参考点

如果用 bottom 描述,那么是以浏览器的左下角为参考点

作用:1.返回顶部栏 2.固定导航栏 3.小广告

使用简单的 js 完成回到顶部功能

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <style>
  7. p{
  8. width: 140px;
  9. height: 100px;
  10. background-color: pink;
  11. position: fixed;
  12. bottom: 0;
  13. right: 50px;
  14. line-height: 100px;
  15. text-align: center;
  16. color: white;
  17. border-radius: 10px;
  18. font-weight: bolder;
  19. font-size: 25px;
  20. }
  21. p a {
  22. text-decoration: none;
  23. }
  24. .wrap img {
  25. display: block;
  26. }
  27. </style>
  28. <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
  29. </head>
  30. <body>
  31. <div class="wrap">
  32. <p>
  33. <a href="#">回到顶部</a>
  34. </p>
  35. <img src="images/zly.jpg" alt="">
  36. <img src="images/zly.jpg" alt="">
  37. <img src="images/zly.jpg" alt="">
  38. </div>
  39. <script>
  40. $(function(){
  41. $('p').click(function(){
  42. $('html').animate({
  43. "scrollTop":0
  44. },1000)
  45. // scrollTop表示回到顶部
  46. // 1000表示1秒,单位为毫秒
  47. })
  48. })
  49. </script>
  50. </body>
  51. </html>

网页效果:

  1. ![](https://cdn.nlark.com/yuque/0/2020/png/1484428/1598489114645-8b957c31-dbed-4509-abe8-96df88cc8df8.png)

固定导航栏

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style>
  7. *{
  8. padding: 0;
  9. margin: 0;
  10. }
  11. ul{
  12. list-style: none;
  13. }
  14. .nav{
  15. width: 960px;
  16. overflow: hidden;
  17. /*margin: 0px auto;*/
  18. background-color: purple;
  19. border-radius: 5px;
  20. position: fixed;
  21. left: 50%;
  22. margin-left: -480px;
  23. }
  24. .nav ul li{
  25. float: left;
  26. width: 160px;
  27. height: 40px;
  28. line-height: 40px;
  29. text-align: center;
  30. }
  31. .nav ul li a{
  32. width: 160px;
  33. height: 40px;
  34. display: block;
  35. color: white;
  36. font-size: 14px;
  37. text-decoration: none;
  38. }
  39. .nav ul li a:hover{
  40. background: yellow;
  41. color: green;
  42. text-decoration: underline;
  43. }
  44. .wrap{
  45. width: 100%;
  46. height: 400px;
  47. background-color: #666;
  48. }
  49. </style>
  50. </head>
  51. <body style="height: 3000px">
  52. <div class="nav">
  53. <ul>
  54. <li>
  55. <a href="#">网站导航</a>
  56. </li>
  57. <li>
  58. <a href="#">网站导航</a>
  59. </li>
  60. <li>
  61. <a href="#">网站导航</a>
  62. </li>
  63. <li>
  64. <a href="#">网站导航</a>
  65. </li>
  66. <li>
  67. <a href="#">网站导航</a>
  68. </li>
  69. <li>
  70. <a href="#">网站导航</a>
  71. </li>
  72. </ul>
  73. </div>
  74. <div class="wrap"></div>
  75. </body>
  76. </html>

网页效果:

  1. ![](https://cdn.nlark.com/yuque/0/2020/png/1484428/1598489114645-092c681c-c4ec-42fe-be51-1f0053354067.png)

四、z-index

这个东西非常简单,它有四大特性,每个特性你记住了,页面布局就不会出现找不到盒子的情况。

  • z-index 值表示谁压着谁,数值大的压盖住数值小的,
  • 只有定位了的元素,才能有 z-index,也就是说,不管相对定位,绝对定位,固定定位,都可以使用 z-index,而浮动元素不能使用 z-index
  • z-index 值没有单位,就是一个正整数,默认的 z-index 值为 0 如果大家都没有 z-index 值,或者 z-index 值一样,那么谁写在 HTML 后面,谁在上面压着别人,定位了元素,永远压住没有定位的元素。
  • 从父现象:父亲怂了,儿子再牛逼也没用

只要有定位的盒子,一定大于标准流的盒子

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <style>
  7. .box1{
  8. width: 200px;
  9. height: 200px;
  10. background-color: red;
  11. position: relative;
  12. top: 50px;
  13. }
  14. .box2{
  15. width: 200px;
  16. height: 200px;
  17. background-color: green;
  18. }
  19. .box3{
  20. width: 200px;
  21. height: 200px;
  22. background-color: yellow;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <div class="box1">
  28. </div>
  29. <div class="box2">
  30. </div>
  31. <div class="box3">
  32. </div>
  33. </body>
  34. </html>

网页效果:

  1. ![](https://cdn.nlark.com/yuque/0/2020/png/1484428/1598489114620-dcf55bf7-09e4-492f-a439-cd10356eadf0.png)

从父现象

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <style>
  7. .lzy{
  8. width: 300px;
  9. height: 300px;
  10. background-color: black;
  11. position: absolute;
  12. z-index: 14;
  13. }
  14. .tl{
  15. width: 300px;
  16. height: 300px;
  17. background-color: yellow;
  18. position: absolute;
  19. z-index: 11;
  20. }
  21. .kimi{
  22. width: 100px;
  23. height: 100px;
  24. background-color: green;
  25. position: absolute;
  26. top: 400px;
  27. left: 400px;
  28. }
  29. .sd{
  30. width: 100px;
  31. height: 100px;
  32. background-color: pink;
  33. position: absolute;
  34. top: 450px;
  35. left: 350px;
  36. z-index: 1000;
  37. }
  38. </style>
  39. </head>
  40. <body>
  41. <div class="lzy">
  42. <div class="kimi"></div>
  43. </div>
  44. <div class="tl">
  45. <div class="sd"></div>
  46. </div>
  47. </body>
  48. </html>

网页效果

  1. ![](https://cdn.nlark.com/yuque/0/2020/png/1484428/1598489114633-735550ad-23bc-41fd-9c30-bb69881c9472.png)

一般父元素,会设置相对定位

子元素设置相对定位或者绝对定位,以父元素为参考点

z-link 应用—固定导航栏

要求网页的图片和文字内容,不能覆盖导航栏。

html 代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style>
  7. * {
  8. padding: 0;
  9. margin: 0;
  10. }
  11. ul {
  12. list-style: none;
  13. }
  14. body {
  15. padding-top: 40px;
  16. }
  17. .nav {
  18. width: 100%;
  19. height: 40px;
  20. background-color: black;
  21. position: fixed;
  22. top: 0;
  23. left: 0;
  24. z-index: 99999;
  25. }
  26. .wrap {
  27. width: 960px;
  28. overflow: hidden;
  29. margin: 0px auto;
  30. background-color: purple;
  31. border-radius: 5px;
  32. }
  33. .wrap ul li {
  34. float: left;
  35. width: 160px;
  36. height: 40px;
  37. line-height: 40px;
  38. text-align: center;
  39. }
  40. .wrap ul li a {
  41. width: 160px;
  42. height: 40px;
  43. display: block;
  44. color: white;
  45. font-size: 14px;
  46. text-decoration: none;
  47. }
  48. .wrap ul li a:hover {
  49. background: yellow;
  50. color: green;
  51. text-decoration: underline;
  52. }
  53. p {
  54. position: relative;
  55. }
  56. </style>
  57. </head>
  58. <body style="height: 3000px">
  59. <div class="nav">
  60. <div class="wrap">
  61. <ul>
  62. <li>
  63. <a href="#">网站导航</a>
  64. </li>
  65. <li>
  66. <a href="#">网站导航</a>
  67. </li>
  68. <li>
  69. <a href="#">网站导航</a>
  70. </li>
  71. <li>
  72. <a href="#">网站导航</a>
  73. </li>
  74. <li>
  75. <a href="#">网站导航</a>
  76. </li>
  77. <li>
  78. <a href="#">网站导航</a>
  79. <li>
  80. </ul>
  81. </div>
  82. </div>
  83. <!-- <div class="wrap">内容</div> -->
  84. <img src="images/zly.jpg" alt="">
  85. <img src="images/zly.jpg" alt="">
  86. <img src="images/zly.jpg" alt="">
  87. <img src="images/zly.jpg" alt="">
  88. <p>哈哈哈哈哈哈哈哈</p>
  89. <img src="images/zly.jpg" alt="">
  90. <img src="images/zly.jpg" alt="">
  91. <img src="images/zly.jpg" alt="">
  92. </body>
  93. </html>

网页效果:

  1. ![](https://cdn.nlark.com/yuque/0/2020/png/1484428/1598489114681-afb1a13d-7f9b-41f0-a369-4e2e45d24892.png)

五、iconfont 使用

Iconfont-国内功能很强大且图标内容很丰富的矢量图标库,提供矢量图标下载、在线存储、格式转换等功能。

官方链接:

http://iconfont.cn/home/index

登录之后,点击官方图标库 ,搜索图标,关键字为 搜索

选择对应的图标—>添加入库

  1. ![](https://cdn.nlark.com/yuque/0/2020/png/1484428/1598489114616-af69748f-59d5-4e26-aa01-1296e1307a1b.png)

点击右上角的购物车,点击添加至项目

  1. ![](https://cdn.nlark.com/yuque/0/2020/png/1484428/1598489114653-09c818e3-6205-4064-bf28-c3f76b21a1cf.png)

新建一个项目

  1. ![](https://cdn.nlark.com/yuque/0/2020/png/1484428/1598489114569-53523c82-176f-4c9f-af55-0920f20d90bc.png)

点击右侧的下载至本地

点击图标复制代码,待会就能用到了。

  1. ![](https://cdn.nlark.com/yuque/0/2020/png/1484428/1598489114568-7b3f5d04-eec2-4afa-8894-0ac5e2ca4e02.png)

解压压缩包,我们需要的是 unicode

使用浏览器打开 demo_unicode.html

页面是一个帮助文档,在线文档请参考

http://iconfont.cn/help/detail?spm=a313x.7781069.1998910419.15&helptype=code

第一步:拷贝项目下面生成的 font-face

将解压的文件夹整体复制到网站目录

将目录名重命名为 font

使用外联样式,连接目录里面的 iconfont.css

  1. <link rel="stylesheet" href="./font/iconfont.css">

第二步:定义使用 iconfont 的样式

  1. <style>
  2. @font-face {font-family: 'iconfont';
  3. src: url('./font/iconfont.eot');
  4. src: url('./font/iconfont.eot?#iefix') format('embedded-opentype'),
  5. url('./font/iconfont.woff') format('woff'),
  6. url('./font/iconfont.ttf') format('truetype'),
  7. url('./font/iconfont.svg#iconfont') format('svg');
  8. }
  9. .iconfont{
  10. font-family:"iconfont" !important;
  11. font-size:16px;font-style:normal;
  12. -webkit-font-smoothing: antialiased;
  13. -webkit-text-stroke-width: 0.2px;
  14. -moz-osx-font-smoothing: grayscale;
  15. }
  16. </style>

第三步:挑选相应图标并获取字体编码,应用于页面。粘贴复制的代码

  1. ![](https://cdn.nlark.com/yuque/0/2020/png/1484428/1598489114712-c8e5cc7a-dee6-4a98-8936-e20087c66344.png)
  1. <i class="iconfont">&#xe652;</i>

完整代码如下:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <link rel="stylesheet" href="./font/iconfont.css">
  7. <style>
  8. @font-face {font-family: 'iconfont';
  9. src: url('./font/iconfont.eot');
  10. src: url('./font/iconfont.eot?#iefix') format('embedded-opentype'),
  11. url('./font/iconfont.woff') format('woff'),
  12. url('./font/iconfont.ttf') format('truetype'),
  13. url('./font/iconfont.svg#iconfont') format('svg');
  14. }
  15. .iconfont{
  16. font-family:"iconfont" !important;
  17. font-size:16px;font-style:normal;
  18. -webkit-font-smoothing: antialiased;
  19. -webkit-text-stroke-width: 0.2px;
  20. -moz-osx-font-smoothing: grayscale;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <i class="iconfont">&#xe652;</i>
  26. </body>
  27. </html>

访问网页,就可以看到图标了

  1. ![](https://cdn.nlark.com/yuque/0/2020/png/1484428/1598489114590-467f1194-749f-4919-b2e8-1c9446190ec0.png)

如果本地不行,可以尝试使用在线链接

  1. ![](https://cdn.nlark.com/yuque/0/2020/png/1484428/1598489114661-56484c39-5113-4f3f-aa53-8f976053dc31.png)

生成在线链接

  1. ![](https://cdn.nlark.com/yuque/0/2020/png/1484428/1598489115328-796b9842-6e69-418d-a07e-ee7a859e7ac2.png)

替换掉上面的代码,再次访问。就可以了。

周末作业:

小米首页,仅仅是首页的内容