我很困惑,因为必须让它在CSS中工作只有一个div,其中边框只能在宽度的一半上看到。
边框样式很简单1px solid #000;
但是,我希望div
边框的顶部不会在任何地方都可见(在div
的宽度上为100%),
而只是div
宽度的前50%。
我无法在任何地方得到这样的例子,它需要以百分比形式出现,所以常用的技巧包(下半部分的图像,......)。
答案 0 :(得分:24)
这会有效吗?
#holder {
border: 1px solid #000;
height: 200px;
width: 200px;
position:relative;
margin:10px;
}
#mask {
position: absolute;
top:-1px;
left:1px;
width:50%;
height: 1px;
background-color:#fff;
}
<div id="holder">
<div id="mask"></div>
</div>
答案 1 :(得分:17)
如果您根本不想弄乱HTML,可以使用空伪元素,仅使用CSS。当然,您仍然需要知道背景颜色(假设这里是白色):
<span class="half-a-border-on-top">Hello world!</span>
<style>
.half-a-border-on-top {
border-top:1px solid black;
position: relative;
}
.half-a-border-on-top:after {
padding:0;margin:0;display:block;/* probably not really needed? */
content: "";
width:50%;
height:1.1px;/* slight higher to work around rounding errors(?) on some zoom levels in some browsers. */
background-color:white;
position: absolute;
right:0;
top:-1px;
}
</style>
<强>结果:强>
<强>段强>
.half-a-border-on-top {
border-top:1px solid black;
position: relative;
}
.half-a-border-on-top:after {
padding:0;margin:0;display:block;/* probably not really needed? */
content: "";
width:50%;
height:1.1px;
background-color:white;
position: absolute;
right:0;
top:-1px;
}
&#13;
<span class="half-a-border-on-top">Hello world!</span>
&#13;
<强>小提琴:强>
答案 2 :(得分:1)
让我们告诉你我如何编辑狮子座的代码,将半边框放在中间的左边。
试试这个:
html代码
<div class="half-a-border-on-left">Hello world!</div>
css代码
<style>
.half-a-border-on-left {
border-left: 1px solid black;
position: relative;
height: 50px;
background: red;
}
.half-a-border-on-left:after {
padding:0;
margin:0;
content: "";
width: 1px;
height: 10px;
background-color:white;
position: absolute;
left:-1px;
top: -10px;
}
.half-a-border-on-left:before {
padding:0;
margin:0;
content: "";
width: 1px;
height: 10px;
background-color:white;
position: absolute;
left: -1px;
bottom: -5px;
}
</style>
这些是用于放置半边框的代码,谢谢leo,
答案 3 :(得分:1)
您可以使用 CSS 渐变边框
.half-a-border-on-top {
border-top: 1px solid;
border-image: linear-gradient(to right, #000 50%, #FFF 50%) 100% 1;
}
<span class="half-a-border-on-top">Hello world!</span>
答案 4 :(得分:-1)
***
.div_1 {
width: 50px;
border-bottom: 2px solid black;
}
.div_2 {
width: max-content;
margin-bottom: 10px;
}
<div class="div_1" ><div class="div_2">I love Hyderabad</div></div>