在使用定位布局的时候,可能会出现盒子重叠的情况。此时,可以使用z-index来控制盒子的前后持续。(参考为坐标轴的Z轴)
语法:
选择器 {
z-index: 1;
}
注意细节:
代码示例:
<!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>定位的堆叠顺序</title> <style> .box { position: absolute; top: 0; left: 0; width: 200px; height: 200px; } .xiongda { background-color: red; z-index: 1; } .xionger { background-color: green; left: 50px; top: 50px; z-index: 2; } .qiangge { background-color:blue; left: 100px; top: 100px; } </style> </head> <body> <div class="box xiongda">熊大</div> <div class="box xionger">熊二</div> <div class="box qiangge">光头强</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>定位的特殊特性</title> <style> span { position: absolute; top: 300px; width: 200px; height: 150px; background-color: pink; } div { position: absolute; background-color: skyblue; } </style> </head> <body> <span>123</span> <div>abcd</div> </body> </html>