阻止孩子覆盖父母大纲?

时间:2016-08-17 12:13:09

标签: css outline

由于幻灯片内部有一些元素,我使用的是带负偏移而不是边框​​的轮廓。

然而,子元素覆盖了轮廓,但我想要它们的边界。我用它来构建内容。

http://jsfiddle.net/z22kw2zq/1/

.parent {
 position:relative; outline: green 3px solid;
  outline-offset:0px;
background-color:pink;
pading:5px;
overflow:hidden;
}
.child {position:relative; top:26px; background-color:yellow;
display:inline;
}

2 个答案:

答案 0 :(得分:2)

添加z-index将解决您的问题:

.parent {
  position:relative; outline: green 3px solid;
  outline-offset: -3px;
background-color:pink;
pading:5px;
overflow:hidden;
z-index:999;
}
.child {position:relative; top:23px; background-color:yellow;
display:inline;
z-index:-1
}

z-index属性指定元素的堆栈顺序。堆栈顺序较大的元素始终位于堆栈顺序较低的元素前面。

注意:z-index仅适用于定位元素(位置:绝对,位置:相对或位置:固定)。

答案 1 :(得分:2)

您可以使用:after伪元素作为大纲,并添加position: absolute

.parent {
  position: relative;
  background-color: pink;
  overflow: hidden;
}
.parent:after {
  width: 100%;
  height: 100%;
  content: '';
  outline: green 3px solid;
  outline-offset: -3px;
  position: absolute;
  top: 0;
  left: 0;
}
.child {
  position: relative;
  top: 23px;
  background-color: yellow;
  display: inline;
}
p {
  margin: 0;
}
<div class="parent">
  <div class="child">lorem ipsum doler sit amet</div>
  <p>text here</p>
</div>

相关问题