为方便,top、right、left、bottom属性简写为TRLBcss
举个栗子:html
将div标签的position设置为absolute浏览器
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>absolute</title> <style type="text/css"> *{ margin: 0px; padding: 0px; } .absolute{ width: 100px; height: 100px; border: 1px solid red; position: absolute; top: 0px; left: 0px; } </style> </head> <body> <p>你好</p> <div class="absolute"></div> </body> </html>
浏览器显示:ide
经过页面显示咱们发现,设置为absolute的绝对定位元素div,不顾处于标准流中的p标签的存在,仍然显示在了top:0px; left:0px;位置,布局
从中咱们也能够看出绝对定位元素脱离了正常的标准流spa
举个栗子:code
将div标签的position设置为relativehtm
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>relative</title> <style type="text/css"> *{ margin: 0px; padding: 0px; } .relative{ width: 100px; height: 100px; border: 1px solid red; position: relative; top: 0px; left: 0px; } </style> </head> <body> <p>你好</p> <div class="relative"></div> </body> </html>
浏览器显示:blog
咱们发现,设置为relative的相对定位元素div,受标准流中的p标签的约束,显示在了p标签的下方,由于它是相对于在标准流中原来的位置进行定位的.继承
绝对定位absolute
<div style="width: 240px; height: auto; border: solid 1px black;"> <div style="width: 80px; height: 80px; background-color: red; position: absolute; margin: 0 auto;"></div> </div>![]()
相对定位relative
<div style="width: 240px; height: auto; border: solid 1px black;"> <div style="width: 80px; height: 80px; background-color: red; position: relative; margin: 0 auto;"></div> </div>
咱们发现:1. 相对定位元素能够为父元素撑起高度,而绝对定位元素却会形成父元素塌陷,也说明了相对定位元素没有脱离标准流,而绝对定位元素脱离了标准流。
2.未脱离标准流的元素能够经过设置margin-left和margin-right为auto,来使块级元素水平居中。
举个栗子:
给position设置为relative的div标签,加一个position设置为relative的父标签,观察fixed是否受具备position的父标签影响,做为对比咱们再加上一个属性值为absolute的div标签
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>fixed</title> <style type="text/css"> *{ margin: 0px; padding: 0px; } .fixed{ width: 100px; height: 100px; border: 1px solid red; position: fixed; top: 0px; left: 0px; } .absolute{ width: 100px; height: 100px; border: 1px solid blue; position: absolute; top: 0px; left: 0px; } .pre{ width: 200px; height: 200px; border: 1px solid black; position: relative; top: 100px; left: 100px; } </style> </head> <body> <div class="pre"> <div class="fixed"></div> <div class="absolute"></div> </div> </body> </html>
网页显示:
咱们发现,属性值为fixed的子标签并不受具备position属性的父标签影响,脱离了来父标签的约束,相对于浏览器窗口显示在top:0px; left:0px;位置.
而属性值为absolute的子标签却受着具备position属性的父标签约束,相对于position为relative的父元素显示在了top:0; left:0;位置,这也是fixed与absolute的一个重要区别。
举个栗子:
对于父div标签咱们设置position:fixed,对于子div标签咱们设置position:inherit,观察子标签是否会继承父标签的position属性值
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>inherit</title> <style type="text/css"> *{ margin: 0px; padding: 0px; } .pre{ width: 200px; height: 200px; border: 1px solid black; position: fixed; top: 100px; left: 100px; } .inherit{ width: 100px; height: 100px; border: 1px solid red; position: inherit; top: 0px; left: 0px; } </style> </head> <body> <div class="pre"> <div class="inherit"></div> </div> </body> </html>
浏览器显示:
咱们发现,子标签具备和父标签一样的position属性值---fixed,子标签的fixed使它显示在了相对于浏览器窗口进行定位的top:0px; left:0px;位置
关于前两点举个栗子
给body标签设置内边距和外边距,观察相对定位元素和绝对定位元素的显示状况
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>inherit</title> <style type="text/css"> body{ margin: 10px; padding: 10px; } .relative{ width: 100px; height: 100px; border: 1px solid red; position: relative; top: 0px; left: 0px; } .absolute{ width: 100px; height: 100px; border: 1px solid black; position: absolute; top: 0px; left: 0px; } </style> </head> <body> <div class="relative"></div> <div class="absolute"></div> </body> </html>
网页显示:
咱们发现元素:<div class="relative"></div>受body标签内外边距的影响,显示在了元素:<div class="absolute"></div>的右下方
关于第三点再举个栗子
咱们将中间的div设置为relative
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>z-index</title> <style type="text/css"> *{ margin: 0px; padding: 0px; } .yellow{ width: 100px; height: 100px; background-color: yellow; } .relative_red{ width: 100px; height: 100px; background-color: red; position: relative; left: 200px; } .black{ width: 100px; height: 100px; background-color: black; } </style> </head> <body> <div class="yellow"></div> <div class="relative_red"></div> <div class="black"></div> </body> </html>
网页显示:
为了对比,咱们将中间div的relative改成absolute:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>z-index</title> <style type="text/css"> *{ margin: 0px; padding: 0px; } .yellow{ width: 100px; height: 100px; background-color: yellow; } .absolute_red{ width: 100px; height: 100px; background-color: red; position: absolute; left: 200px; } .black{ width: 100px; height: 100px; background-color: black; } </style> </head> <body> <div class="yellow"></div> <div class="absolute_red"></div> <div class="black"></div> </body> </html>
网页显示:
咱们发现,设置position:relative的div在原来的文档流上,仍然占有空间,而设置position:absolute的div在文档流上再也不占有空间
栗如:
未设置position的<span>标签
<span style="width: 100px; height: 100px; background-color: red"></span>
尽管给它加了width和height属性,但它仍是做为内联元素,width和height属性无效,因此网页显示空白
添加position:absolute生成绝对定位元素以后
<span style="width: 100px; height: 100px; background-color: red;position: absolute;"></span>
<span>标签同时变成了块状元素
由于先写的定位元素会被后写的定位元素覆盖,所以咱们须要设置定位元素的堆叠顺序,是其按照咱们想要的覆盖方式进行显示
举个大栗子:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>z-index</title> <style type="text/css"> *{ margin: 0px; padding: 0px; } .red{ width: 100px; height: 100px; background-color: red; position: absolute; top: 0px; left: 0px; z-index: 5; } .black{ width: 100px; height: 100px; background-color: black; position: absolute; top: 0px; left: 0px; z-index: 3; } .blue{ width: 100px; height: 100px; background-color: blue; position: absolute; top: 0px; right: 0px; z-index: -1; } .no-position-yellow{ width: 1500px; height: 1000px; background-color: yellow; } </style> </head> <body> <div class="no-position-yellow"></div> <div class="red"></div> <div class="black"></div> <div class="blue"></div> </body> </html>
网页显示:
咱们能够看到只有背景为红色和黄色的元素显示了,而且红色元素堆叠在黄色元素上方,由于黑色元素的z-index小于红色元素的z-index,而它们位置相同,所以红色元素将黑色元素彻底覆盖了.
对于蓝色元素,由于他的z-index为负数,因此它直接被标准流中的黄色元素覆盖.
我理解浅薄,若有错误或不足之处还请留言指出,十分感谢!