为CSS三角形添加边框

时间:2015-03-12 03:59:49

标签: css css-shapes

我有一个三角形

<div class="triangle-left"></div>


.triangle-left {
    width: 0; 
    height: 0; 
    border-top: 22px solid transparent;
    border-bottom: 22px solid transparent;
    border-right: 22px solid white;
}

如何绘制CSS三角形的轮廓,考虑border本身用于制作三角形?外部div?

2 个答案:

答案 0 :(得分:14)

一种方法是创建一个较小的内部三角形。

&#13;
&#13;
.triangle-left {
    width: 0;
    height: 0;
    border-top: 23px solid transparent;
    border-bottom: 23px solid transparent;
    border-right: 23px solid red;
}

.inner-triangle {
    position: relative;
    top: -20px;
    left: 2px;
    width: 0;
    height: 0;
    border-top: 20px solid transparent;
    border-bottom: 20px solid transparent;
    border-right: 20px solid blue;
}
&#13;
<div class="triangle-left">
    <div class="inner-triangle"></div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:12)

我就是这样做的。

.triangle-left {
  width: 0;
  height: 0;
  border-top: 22px solid transparent;
  border-bottom: 22px solid transparent;
  border-right: 22px solid black;
  position: relative;
}

.triangle-left:after {
  content: '';
  width: 0;
  height: 0;
  border-top: 21px solid transparent;
  border-bottom: 21px solid transparent;
  border-right: 21px solid #dddddd;
  position: absolute;
  top: -21px;
  left: 1px;
}
<div class="triangle-left"></div>

此处位于JSFiddle