选择器 { position: static;}
静态定位按照标准流的特性位置摆放,它没有边偏移静态定位在布局时候较少用到选择器 { position: relative;}
自恋型定位是相对于它原来的位置来说的虽然这个盒子是走了的,但其位置还是保留,其他的盒子没有办法上升<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> .box1 { position: relative; top: 100px; left: 100px; width: 200px; height: 200px; background-color: pink; } .box2 { width: 200px; height: 200px; background-color: deeppink; } </style> </head> <body> <div class="box1"></div> <div class="box2"></div> </body> </html>
1234567891011121314151617181920212223242526272829303132选择器 {position:absolute;}
如果没有祖先元素或者祖先元素没有定位,则以浏览器为准进行定位即使页面缩放,也会随着缩放后的浏览器定位绝对定位的高度很高(比浮动要高),可以随意移动位置(脱标)<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> .son { position: absolute; top: 100px; right: 200px; width: 200px; height: 200px; background-color: pink; } .father { width: 500px; height: 500px; background-color: skyblue; } </style> </head> <body> <div class="father"> <div class="son"></div> </div> </body> </html>
123456789101112131415161718192021222324252627282930313233父亲没有加定位
父亲加了定位后
就近原则:跟最近的一级父元素的定位为准
选择器 { position:fixed; }
以浏览器的可视窗口为参照点移动元素跟父元素没有啥关系固定定位不占有原来的位置固定定位还可以贴在我们版心的位置固定在板心右侧的算法
让盒子固定定位在left%50可以走到浏览器的可视区的一半的位置让固定定位的盒子margin-left走板心宽度一半的距离<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> .w { width: 800px; height: 1400px; background-color: pink; margin: 0 auto; } .fixed { position: fixed; /* 1.走浏览器宽度的一半 */ left: 50%; margin: 405px; width: 50px; height: 150px; background-color: skyblue; } </style> </head> <body> <div class="fixed"></div> <div class="w">版心盒子 800像素</div> </body> </html>
12345678910111213141516171819202122232425262728293031323334儿子元素使用绝对定位,父亲元素使用相对定位
定位的特点
以浏览器的可视窗口为参照的元素(固定定位特点)年薪定位占有原先的位置(相对定位的特点)必须添加上下左右当阶段的开发用的少<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> body { height: 3000px; } .nav { /* 粘性定位 */ position: sticky; top: 0; width: 800px; height: 50px; background-color: pink; margin: 100px auto; } </style> </head> <body> <div class="nav">我是导航栏</div> </body> </html>
123456789101112131415161718192021222324252627282930选择器 { z-index: 1;}
数值可以是正整数、负数或0,默认是auto,数值越大,盒子越靠上(权值越大)只有定位的盒子才有属性数值权重后面不能够加单位如果属性值(权重)相同,则后来者居上<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> .box { position: absolute; top: 0px; left: 0px; width: 200px; height: 200px; } .xiongda { z-index: 1; background-color: red; } .xionger { z-index: 2; background-color: blue; left: 50px; top: 50px; } .guangtouqiang { background-color: green; left: 100px; top: 100px; } </style> </head> <body> <!-- 第三个盒子在上面(后来者居上) --> <div class="box xiongda">熊大</div> <div class="box xionger">熊二</div> <div class="box guangtouqiang">光头强</div> </body> </html>
123456789101112131415161718192021222324252627282930313233343536373839404142434445<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> .box { position: absolute; width: 200px; height: 200px; background-color: pink; /* margin: auto; */ /* 1.left走50% */ left: 50%; /* 负值往左走自己盒子的宽度的一半 */ margin-left: -100px; top: 50%; margin-top: -100px; } </style> </head> <body> <div class="box"></div> </body> </html>
1234567891011121314151617181920212223242526272829303132注:上述笔记均来自黑马程序员pink老师前端入门教程,零基础必看的h5(html5)+css3+web前端视频教程 这一课程的学习记录,主要供自己的学习分享
各位看官,都看到这里了,麻烦动动手指头给博主来个点赞8,您的支持作者最大的创作动力哟! <(^-^)>
才疏学浅,若有纰漏,恳请斧正
本文章仅用于各位同志作为学习交流之用,不作任何商业用途,若涉及版权问题请速与作者联系,望悉知
相关知识
HTML(Basic) Chapter6(Pink)定位
使用美汤从HTML中提取特定的标题
用户登录界面设计代码html
【PINK婚礼课堂】花仙子主题婚礼
Visual Basic 6.0在花卉分类检索中的应用及开发实例.pdf
「PINK新娘课堂」花环造型推荐 让你秒变花仙子
基于草图的花开建模与动画
鲜花静态HTML网页作业作品 大学生鲜花网页设计制作成品 简单DIV CSS布局网站
HTML+CSS鲜花静态网页设计
HTML5期末大作业:鲜花网站设计——网上鲜花网页设计(5页) HTML+CSS+JavaScript 期末作业HTML代码
网址: HTML(Basic) Chapter6(Pink)定位 https://m.huajiangbk.com/newsview104727.html
上一篇: 受益一生的45种思维方法 |
下一篇: JAVA编程艺术 |