z-index
屬性,如:固定定位
、相對定位
、絕對定位
。z-index
屬性值是0
。2
個定位的元素都沒有設置z-index
屬性,後者會覆蓋到前者。2
個定位的元素都設置了z-index
屬性,而且數值同樣大仍是後者會覆蓋到前者。z-index
屬性的屬性值大的就會覆蓋小,就是設置元素的層級。用實踐證實這句話,若是2
個定位的元素都沒有設置z-index
屬性,後者會覆蓋到前者。html
代碼塊ui
<!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>z-index屬性</title> <style> div{ width: 200px; height: 200px; } .div1{ background-color: red; position: relative; top: 50px; left: 50px; } .div2{ background-color: slateblue; position: relative; left: 100px; } </style> </head> <body> <div class="div1"></div> <div class="div2"></div> </body> </html>
結果圖code
2
個定位的元素都設置了z-index
屬性,而且數值同樣大仍是後者會覆蓋到前者。代碼塊htm
<!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>z-index屬性</title> <style> div{ width: 200px; height: 200px; } .div1{ background-color: red; position: relative; top: 50px; left: 50px; z-index: 2; } .div2{ background-color: slateblue; position: relative; left: 100px; z-index: 2; } </style> </head> <body> <div class="div1"></div> <div class="div2"></div> </body> </html>
結果圖blog
用實踐來證實這句話,z-index
屬性的屬性值大的就會覆蓋小,就是設置元素的層級。it
代碼塊io
<!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>z-index屬性</title> <style> div{ width: 200px; height: 200px; } .div1{ background-color: red; position: relative; top: 50px; left: 50px; z-index: 3; } .div2{ background-color: slateblue; position: relative; left: 100px; z-index: 2; } </style> </head> <body> <div class="div1"></div> <div class="div2"></div> </body> </html>
結果圖class