css – 边界上的Z-index

前端之家收集整理的这篇文章主要介绍了css – 边界上的Z-index前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直在寻找一种方法添加z-index到边框但似乎找不到一个所以我想我会在这里问.

假设我有父div的div.父有一个边框,我希望该边框与子div重叠,但我不希望父对象重叠.

解决方法

你不能用边框做到这一点.

有趣的是,你可以用大纲来做

* {
Box-sizing: border-Box;
}

.parent {
width: 200px;
height: 200px;
margin: 25px auto;
position: relative;
background: #bada55;
border:12px solid #663399;
outline: 12px solid red;
padding:25px
 }

.child {
width: 220px;
height: 100px;
background: lightblue;
}
<div class="parent">
  <div class="child"></div>
</div>

其他选择

使用伪元素

1.带边框的伪元素

需要一些额外的变换才能轻推到位.

* {
  Box-sizing: border-Box;
}
.parent {
  width: 200px;
  height: 200px;
  margin: 25px auto;
  position: relative;
  background: #bada55;
  padding: 25px;
}
.child {
  width: 220px;
  height: 100px;
  background: lightblue;
}
.absolute::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%,-50%);
  width: 100%;
  height: 100%;
  border: 12px solid red;
}
<div class="parent absolute">
  <div class="child"></div>
</div>

2.具有盒阴影的伪元素

* {
  Box-sizing: border-Box;
}
.parent {
  width: 200px;
  height: 200px;
  margin: 25px auto;
  position: relative;
  background: #bada55;
  padding: 25px;
}
.child {
  width: 220px;
  height: 100px;
  background: lightblue;
}
.shadow::after {
  content: '';
  position: absolute;
  top: 0%;
  left: 0%;
  width: 100%;
  height: 100%;
  Box-shadow: 0 0 0 12px red;
}
<div class="parent shadow">
  <div class="child"></div>
</div>
原文链接:https://www.f2er.com/css/216084.html

猜你在找的CSS相关文章