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

4
我有一个CSS箭头框覆盖在图像滑块上,但我需要使尾巴缩进并透明,以便在其后面看到图像。需要将白色部分设置为透明。请参见下面的附加图像和我的CSS代码。谢谢。
```css .arrow_box { position: relative; background: #fff; border: 1px solid #ccc; }
.arrow_box:after, .arrow_box:before { bottom: 100%; left: 50%; border: solid transparent; content: " "; height: 0; width: 0; position: absolute; pointer-events: none; }
.arrow_box:after { border-color: rgba(255, 255, 255, 0); border-bottom-color: #fff; border-width: 10px; margin-left: -10px; }
.arrow_box:before { border-color: rgba(204, 204, 204, 0); border-bottom-color: #ccc; border-width: 11px; margin-left: -11px; } ```
#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;

}

你想将 border-left: 35px solid white; 更改为 solid transparent 吗?能否提供一个fiddle示例? - adamdc78
你也可以使用SVG遮罩:http://codepen.io/yoksel/full/fsdbu/ - AlienWebguy
5个回答

8

最简单的解决方案是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>



只要你来到“兼容性”这一部分,就会变得简单。 - Roko C. Buljan
1
@RokoC.Buljan - 只有IE8不支持 - http://caniuse.com/#search=svg - Weafs.py
1
好的,但是IE8仍然拥有巨大的市场份额约16%...以及未更新的移动和桌面浏览器。无论如何,为推动Web前进而加1。 - Roko C. Buljan
1
我认为这是一个很好的解决方案。IE8用户将无法看到SVG形状,但如果理解渐进增强的概念,这将不是一个问题。 - Terry
@Terry同意,但在AA的情况下没有备选方案。 - Roko C. Buljan
显示剩余2条评论

7
这很简单。您只需将 ::before 伪元素的上/下边框颜色设置为背景颜色,并将左边框颜色更改为透明即可。
然后,您可以使用边距/偏移量来正确定位伪元素。

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;
}
<div id="flag">This is it!</div>


1

演示

#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;
}

1
使用:before的边框来绘制箭头的主矩形部分,而不是在#flag上使用background。因此,在#flag:before上设置border-topborder-bottom#231f20。这样,您只需将#flag:before上的border-left设置为transparent即可。
这是一个修改过的版本,使用不同的颜色来帮助可视化哪些边框正在绘制此解决方案中的哪个部分。http://jsfiddle.net/fuvwsn55/

0

如果您可以将您的标志分成两个div(在所有情况下都不实用),那么您可以获得一个平滑渲染的箭头(请注意,这也会扭曲您的div内容):

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;
}
<div id="flag_top">
</div>
<div id="flag_bottom">    
</div>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接