!DOCTYPE htmlhtml head meta charset=utf-8 titletitle style type=textcss margin 0; padding 0; box1 width 300px; height 400px; background
<p>可以将红色盒子的z-index设置为更高的值,比如100,让它覆盖在黄色部分上方。同时,需要将红色盒子的top值设置为负值,比如-30px,让它向上移动一定距离,遮住黄色部分。修改后的代码如下:</p>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
.box1 {
width: 300px;
height: 400px;
background: red;
position: relative;
z-index: 100;
top: -30px;
}
.box1 span {
width: 100px;
height: 30px;
background: yellow;
position: absolute;
left: 0;
top: 0;
z-index: 10;
}
.box2 {
width: 300px;
height: 400px;
background: blue;
position: relative;
top: 10px;
z-index: 0;
}
.box2 span {
width: 100px;
height: 30px;
background: green;
position: absolute;
left: 0;
top: 0;
z-index: 0;
}
</style>
</head>
<body>
<div class="box1">
<span>1</span>
</div>
<div class="box2">
<span>2</span>
</div>
</body>
</html>
原文地址: https://www.cveoy.top/t/topic/bXDF 著作权归作者所有。请勿转载和采集!