使用css创建自定义箭头形状

时间:2014-12-17 23:41:55

标签: html css css3 shape css-shapes

我有一个css箭头框覆盖图像滑块,但我需要使尾部缩进和透明,以便在其后面看到图像。需要白色部分是透明的。请参阅下面的附图和我的css。感谢。css arrow box over image

#flag { 
width: 400px; height: 80px; background: #231f20; position: relative;
}

#flag:before { 
content: ""; 
position: absolute; 
top: 0; 

width: 0; 
height: 0; 
border-top: 40px solid transparent; 

border-bottom: 40px solid transparent; 

border-left: 35px solid white;

}

#flag:after { 
content: ""; 
position: absolute; 
left: 400px; 
bottom: 0; 
width: 0; 
height: 0; 
border-top: 40px solid transparent;

border-bottom: 40px solid transparent;

border-left: 45px solid #231f20;

}

5 个答案:

答案 0 :(得分:6)

最简单的解决方案是svg

html, body {
  background: url(http://lorempixel.com/400/200/sports/) no-repeat;
  background-size: 100% 100%;
  width: 100%;
  height: 100%;
  margin: 0;
}
svg {
  position: relative;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
}
<svg width="225" height="50">
  <path d="M0,0 L200,0 L225,25 L200,50 L0,50 L25,25z" fill="black" />
</svg>


您可以轻松创建复杂的箭头形状。

html, body {
  background: teal;
  background-size: 100% 100%;
  width: 100%;
  height: 100%;
  margin: 0;
}
svg {
  position: relative;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
<svg width="200" height="100" viewBox="-5 -5 210 110">
  <path d="M0,0 L155,40 L145,0 L200,50 L145,100 L155,60 L0,100 Q100,50 0,0z" stroke-linejoin="round" stroke="#222222" stroke-width="5" fill="#333333" />
</svg>


通过添加一些二次贝塞尔曲线使其更加复杂。

html,
body {
  background: teal;
  background-size: 100% 100%;
  width: 100%;
  height: 100%;
  margin: 0;
}
svg {
  position: relative;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
<svg width="200" height="100" viewBox="-5 -5 205 107">
  <path d="M0,0 L155,40 L145,0 L200,50 L145,100 L155,60 L0,100 Q45,87.5 10,75 Q55,65 20,50 Q55,37 10,25 Q45,12.5 0,0z" stroke-linejoin="round" stroke="#222222" stroke-width="5" fill="#333333" />
</svg>


答案 1 :(得分:5)

这很简单。您只需将::before伪元素的顶部/底部边框的颜色设置为背景颜色,并将左边框的颜色更改为透明。

然后你可以使用边距/偏移来正确定位伪元素。

&#13;
&#13;
body { background-color: gold; }

#flag { 
  width: 400px; height: 80px; background: #231f20; position: relative;
  margin-left: 35px;
  color: white;
  line-height: 80px;
  text-align: center;
}

#flag:before { 
  content: ""; 
  position: absolute; 
  top: 0; 
  left: -35px;
  width: 0; 
  height: 0; 
  border-top: 40px solid #231f20; 
  border-bottom: 40px solid #231f20; 
  border-left: 35px solid transparent;

}

#flag:after { 
  content: ""; 
  position: absolute; 
  left: 400px; 
  bottom: 0; 
  width: 0; 
  height: 0; 
  border-top: 40px solid transparent;

  border-bottom: 40px solid transparent;

  border-left: 45px solid #231f20;
}
&#13;
<div id="flag">This is it!</div>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

使用:before的边框绘制箭头的主要矩形部分,而不是background上的#flag。因此,将border-top上的border-bottom#flag:before设置为#231f20。这样,您只需将border-left上的#flag:before设置为transparent

这是一个具有不同颜色的修改版本,可帮助您查看哪些边框正在绘制此解决方案中的哪个部分。 http://jsfiddle.net/fuvwsn55/

答案 3 :(得分:1)

<强> demo

#flag:before,
#flag:after{ 
  content: ""; 
  position: absolute; 
  border: 40px solid #231f20;
}
#flag:before { 
  left: -35px;
  border-left: 35px solid transparent;
}
#flag:after {
  right:-80px;
  border: 40px solid transparent;
  border-left: 40px solid #231f20;
}

答案 4 :(得分:0)

如果你可以将你的旗帜分成两个div(在所有情况下都绝对不实用),你可以得到一个平滑的渲染箭头(注意,这也会扭曲你的divs&#39;内容):

&#13;
&#13;
body
{
    background-color: gold;
}

#flag_top {
    width: 400px; height: 40px; background: #231f20; position: relative;
    	-webkit-transform: skew(40deg);
	   -moz-transform: skew(40deg);
	     -o-transform: skew(40deg);
    margin-left: 30px;
}
#flag_bottom {
    width: 400px; height: 40px; background: #231f20; position: relative;
    	-webkit-transform: skew(320deg);
	   -moz-transform: skew(320deg);
	     -o-transform: skew(320deg);
    margin-left: 30px;
}
&#13;
<div id="flag_top">
</div>
<div id="flag_bottom">    
</div>
&#13;
&#13;
&#13;