html - 如何使用 after 和 before 属性选择 div 的所有外侧

标签 html css css-selectors

请看我的html代码。

.new-div{
  width:200px;
  height:200px;
  border:1px solid orange;
  text-align:center;
}

.new-div::before{
content: "&nbsp";
position: absolute;
top: 0;
width: 100%;
z-index: 90;
background-color: gray;
left: 0;

}
.new-div::after{
content: "&nbsp";
position: absolute;
bottom: 0;
width: 100%;
z-index: 90;
background-color: gray;
left: 0;

}
 <div class="new-div">
  content
 </div>

这里我想使用 after 和 before 属性将 new-div 的所有外侧都设为灰色。我怎么能得到这个。当我使用 after 和 before 属性时,我只能得到 2 面。

请看下图。我想要这样的输出。

enter image description here

最佳答案

我想这和你想要的是一样的。

.new-div{
  width:200px;
  height:200px;
  border:1px solid orange;
  text-align:center;
  margin:0 auto;
  background-color:#fff;
}

.new-div::before{
content: "&nbsp";
position: absolute;
top: 0;
width: 100%;
z-index: -1;
height:100%;
background-color: gray;
left: 0;

}
.new-div::after{
content: "&nbsp";
position: absolute;
bottom: 0;
width: 100%;
z-index: 90;
background-color: gray;
left: 0;

}
 <div class="new-div">
  content
 </div>

关于html - 如何使用 after 和 before 属性选择 div 的所有外侧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46741737/

相关文章:

html - Div 中的 Youtube Iframe,背景卡在图像响应上

css - 分组 Css 选择器(即子 OR 选择器)

html - 从 iframe 重定向

javascript - 如何使用 JQuery 使音频仅播放一次?

html - 如何使用表格在行上添加边框?

javascript - 仅当其他 div 可见时才让 div 可见

css - 在 Sass 中更正 webkit 文本笔画

css - 不使用通用选择器选择元素的所有直接后代

javascript - 如何从鼠标指针创建 "aura"效果?

html - 如何在输入表单顶部对齐标签?