我正在尝试在水平线上创建三角形指针/边框。
这是我想要实现的一个例子:
我尝试操纵div的顶部边框,但到目前为止我所做的一切都根本不起作用。
答案 0 :(得分:7)
有几种方法可以实现这一点,这可能取决于您的布局。一种解决方案是使用带有边框的旋转元素。
.triangle {
background: green;
border: 2px solid white;
border-width: 2px 2px 0 0;
transform: rotate(-45deg);
}
.box {
background: green;
width: 400px;
height: 80px;
position: relative;
}
.line {
height: 39px;
border-bottom: 2px solid white;
}
.triangle {
background: green;
border: 2px solid white;
border-width: 2px 2px 0 0;
transform: rotate(-45deg);
position: relative;
left: 300px;
top: 28px;
width: 20px;
height: 20px;
}

<div class="box">
<div class="line">
<div class="triangle">
</div>
</div>
</div>
&#13;
答案 1 :(得分:1)
您可以使用:before
和:after
伪元素:
body{
background:black;
}
div{
border-top:1px solid green;
position:relative;
margin-top: 100px;
}
div:before{
content: '';
position:absolute;
left: calc(90% - 20px);
top: -9px;
width: 0;
height: 0;
border-style: solid;
border-width: 0 10px 10px 10px;
border-color: transparent transparent black transparent;
z-index: 10;
}
div:after{
content: '';
position:absolute;
right:10%;
top: -10px;
width: 0;
height: 0;
border-style: solid;
border-width: 0 10px 10px 10px;
border-color: transparent transparent green transparent;
}
<div></div>
基本上,有两个三角形,一个是背景颜色,大z-index
,另一个是边框颜色。
<强>更新强>
提到@misterManSam时,您可以在不使用z-index
- fiddle
答案 2 :(得分:1)
这可以使用单个div元素来实现。
这是一个快速演示:
html,body{background:green; height:100%;padding:0;margin:0;overflow:hidden;}
.line {
margin-top: 50px;
height: 5px;
width: 80%;
background: #fff;
position: relative;
box-sizing: border-box;
}
.line:after,
.line:before {
content: "";
position: absolute;
}
.line:after {
left: calc(100% + 2px);
height: 25px;
width: 25px;
top: -13px;
border-top: 5px solid #fff;
border-left: 5px solid #fff;
transform: rotate(45deg);
}
.line:before {
height: 100%;
top: 0;
left: calc(100% + 34px);
width: 20%;
background: inherit;
}
<div class="line"></div>
答案 3 :(得分:0)
.box {
margin-top: 100px;
height: 50px;
background: green;
position: relative;
}
.box .line {
position: absolute;
top: 50%;
left: 5px;
right: 5px;
margin-top: -1px;
height: 2px;
background: white;
}
.box .triangle {
position: absolute;
width: 0;
height: 0;
border-bottom: 8px solid white;
border-left: 8px solid transparent;
border-right: 8px solid transparent;
border-top: 8px solid transparent;
top: 50%;
right: 50px;
margin-top: -15px;
}
.box .triangle .inner {
position: absolute;
top: -4px;
left: -6px;
width: 0;
height: 0;
border-bottom: 6px solid green;
border-left: 6px solid transparent;
border-right: 6px solid transparent;
border-top: 6px solid transparent;
}
&#13;
<div class="box">
<div class="line"></div>
<div class="triangle"><div class="inner"></div></div>
</div>
&#13;