一、绝对定位
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>Document</title><style>*{margin:0;padding:0;}div{width:800px;height: 200px;background: pink;margin:0 auto;position: relative;}p{width:200px;height: 150px;float: left;}p:nth-child(1){background: red;position: absolute;top:20px;left:20px;z-index: 1;}p:nth-child(2){background: green;position:absolute;left:30px;top:30px;z-index: 2;}p:nth-child(3){background: yellow;position:absolute;left:40px;top:40px;z-index: 3;}</style></head><body><!--相对于父级元素的绝对定位,浮出、脱离布局流,它不占据空间,就是我们所说的层,其位置相对于最近的已定位父元素而言的位置,可直接指定 “left”、“top”、“right” 以及 “bottom” 属性。若父级都没有定位,则以 html(根元素)。层叠的顺序 z-index:value--><div><p></p><p></p><p></p></div></body></html>
二、相对定位
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>Document</title><style>*{margin:0;padding:0;}div{width:800px;height: 200px;background: pink;margin:0 auto;}p{width:200px;height: 150px;float: left;}p:nth-child(1){background: red;}p:nth-child(2){background: green;/*只需要加在自己身上,不需要父元素加任何东西,相对于自己刚开始的位置*/position: relative;left:20px;top:20px;}p:nth-child(3){background: yellow;}</style></head><body><!--是相对于默认位置的相对定位,通过设置 left、top、right、bottom 值可将其移至相对于其正常位置的地方(相对于自己的开始的位置发生的位置上的移动,【不会破坏正常的布局流】,不会脱离文档流)--><div><p></p><p></p><p></p></div></body></html>
三、固定定位
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>Document</title><style>*{margin:0;padding:0;}.box{height:1200px;background: pink;}p{width:100px;height: 50px;background: yellow;text-align: center;line-height: 50px;position: fixed;bottom: 20px;right:30px;}</style></head><body><!--相对浏览器的绝对定位,是相对于浏览器窗口的指定坐标进行定位。此元素的位置可通过 "left"、"top"、"right" 以及 "bottom" 属性来规定。不论窗口滚动与否,元素都会留在那个位置。脱离文档流;--><div class="box"></div><p>返回顶部</p></body></html>
默认值。位置设置为 static 的元素会正常显示,它始终会处于文档流给予的位置(static 元素会忽略任何 top、bottom、left 或 right 声明)。
不管是定位还是浮动,只要脱离文档流的盒子,他的宽都是内容给撑起来的
四、元素在另一个盒子水平垂直居中
水平垂直居中:
方式一:<br /> left:50%;<br /> top:50%;<br /> margin-left:-盒子宽度的一半;<br /> margin-top:-盒子高度的一半;方式二:<br /> left:0;<br /> top:0;<br /> bottom: 0;<br /> right:0;<br /> margin:auto;
