而固定定位不会
标签:
1、绝对定位position:absolute;
绝对定位指的是通过规定HTML元素在水安然安祥垂直标的目的上的位置来固定元素,基于绝对定位的元素不会占据空间。绝对定位的位置声明是相对付已定位的并且包罗关系比来的祖先元素。如果当前需要被定为的元素没有已定位的祖先元素作为参考值,则相对付整个网页。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>定位和构造</title> </head> <style> .big { width: 900px; height: 600px; background-color: black; position: relative; } .box4 { width: 150px; height: 100px; background-color: blue; position: absolute; top: 150px; left: 200px; } </style> <body> <div> <div></div> </div> </body> </html>如图所示,蓝色的盒子是相对付整个大盒子而言的,但是,当蓝色盒子外层没有设置有定位的大盒子包裹,则蓝色盒子会的绝对定位会相对与整个屏幕。
2、相对定位position:relative;
相对定位与绝对定位的区别在于它的参照点不是左上角的原点,而是该元素自己原先的起点位置。并且即使该元素偏移到了新的位置,,也仍然从原始的起点处占据空间。
专门成立的学习Q-q-u-n: 784-783-012 ,分享学习的要领和需要注意的小细节,不竭更新最新的教程和学习技巧 (从零根本开始到前端项目实战教程,学习工具,全栈开发学习路线以及规划) <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>定位和构造</title> </head> <style> .big { width: 900px; height: 600px; background-color: black; position: relative; } .box1 { width: 150px; height: 100px; background-color: aqua; position: relative; left: 100px; top: 10px; } .box2 { width: 150px; height: 100px; background-color: red; /* position: relative; */ left: 130px; bottom: 50px; } .box3 { width: 150px; height: 100px; background-color: yellow; /* position: relative; */ left: 170px; bottom: 100px; } .box4 { width: 150px; height: 100px; background-color: blue; position: absolute; top: 150px; left: 200px; } .box6 { width: 150px; height: 100px; background-color: rgb(27, 173, 83); } </style> <body> <div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> </div> </body> </html>此时我们以第三个盒子,黄色的盒子为例,此时我们将它的相对定位注释失,它的运行功效是这样的。
当我们给他加上相对定位,position:relative;运行功效是这样的,它以自身原先的位置为参照物向上向右移动,但是当它移动之后,它原本下面的绿色盒子没有往上移动,占据它的位置,也就是说,使用相对定位会占据位置,而固定定位不会,以刚刚阿谁黄色盒子和绿色盒子为例,如果黄色盒子使用绝对定位给他定位,当黄河盒子移走之后,绿色盒子会往上移,占据之前黄色盒子的位置。
position:fixed;
固定定位永远城市相对付浏览器窗口进行定位,固定定位会固定在浏览器的某个位置,不会随滚动条滚动。最常用的就是电脑里面是不是弹出的小告白,如果你不差失它,其时滑动鼠标检察网页时,小告白一直会在那里,还有常用的就是网站或者APP的导航栏和底部的选择栏。
web前端入门到实战:css绝对定位和相对定位、固定定位
标签:
原文地点:https://blog.51cto.com/14592820/2463911
温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/32382.html