一种选择使用所生成的内容:
.borders {
width:500px;
height:500px;
position: relative;
background:#efefef;
border-top:10px solid red;
border-bottom:10px solid green;
}
.borders::before,
.borders::after {
content: '';
position: absolute;
width: 10px;
top: 0;
bottom: 0;
background-color: #000;
}
.borders::before {
left: 0;
}
.borders::after {
right: 0;
}
JS Fiddle demo。
或者嵌套HTML(如果你真的必须):
<div class="borders">
<div class="innerBorder left"></div>
<div class="innerBorder right"></div>
</div>
.borders {
width:500px;
height:500px;
position: relative;
background:#efefef;
border-top:10px solid red;
border-bottom:10px solid green;
}
.borders .innerBorder{
content: '';
position: absolute;
width: 10px;
top: 0;
bottom: 0;
background-color: #000;
}
.borders .left {
left: 0;
}
.borders .right {
right: 0;
}
JS Fiddle demo。
,并且其中左,右,border-color
是缠绕元件的background-color
单嵌套元件的解决方案,并且由所述后代的margin
控制的宽度:
<div class="borders">
<div class="inner"></div>
</div>
CSS:
.borders {
width:500px;
height:500px;
background-color: #000;
border-top:10px solid red;
border-bottom:10px solid green;
}
.borders .inner {
background-color: #efefef;
height: 100%;
margin: 0 10px;
}
JS Fiddle demo。
一个孩子元素似乎已经足够。看到我的答案。 – PointedEars
你是对的!并且...我已经添加了一个(虽然,当然,这是一个*不同的*单独的后代解决方案)=) –