CSS技巧-CSS的形状有很多不同的CSS形状,我特别困惑于三角形:

#三角形向上{宽度:0;高度:0;左边框:50px实心透明;右边框:50px实心透明;边框底部:100px纯红色;}<div id=“triangle up”></div>

它是如何以及为什么工作的?


当前回答

这是另一把小提琴:

.container:after {
    position: absolute;
    right: 0;
    content: "";
    margin-right:-50px;
    margin-bottom: -8px;
    border-width: 25px;
    border-style: solid;
    border-color: transparent transparent transparent #000;
    width: 0;
    height: 0;
    z-index: 10;
    -webkit-transition: visibility 50ms ease-in-out,opacity 50ms ease-in-out;
    transition: visibility 50ms ease-in-out,opacity 50ms ease-in-out;
    bottom: 21px;
}
.container {
    float: left;
    margin-top: 100px;
    position: relative;
    width: 150px;
    height: 80px;
    background-color: #000;
}

.containerRed {
    float: left;
    margin-top: 100px;
    position: relative;
    width: 100px;
    height: 80px;
    background-color: red;
}

https://jsfiddle.net/qdhvdb17/

其他回答

不同的方法。具有线性梯度(对于IE,仅IE 10+)。您可以使用任何角度:

.三角形{边距:50px自动;宽度:100px;高度:100px;/*线性梯度*/背景:-moz线性梯度(-45deg,rgba(255,0,0,0)0%,rgba,255,0,0,50%,rgba)255,0,0,1,50%;/*FF3.6以上*/背景:-webkit渐变(线性、左上、右下、颜色停止(0%,rgba(255,0,0,0))、颜色停止器(50%,rgba)255,0,0,0))、色彩停止器(50%,rgba;/*铬,Safari4+*/背景:-webkit线性梯度(-45deg,rgba(255,0,0)0%,rgba,255,0,0,0,50%,rgba)255,0,0,1,50%;/*Chrome10+、Safari5.1+*/背景:o-linear-gradient(-45度,rgba(255,0,0)0%,rgba(2550,0,0)50%,rgba(255,00,1)50%;/*歌剧11.10+*/背景:-ms线性梯度(-45度,rgba(255,0,0)0%,rgba,255,0,0,0,50%,rgba)255,0,0,1,50%;/*即10+*/背景:线性梯度(135度,rgba(255,0,0)0%,rgba,255,0,0,0,50%,rgba)255,0,0,1,50%;/*W3C*/;}<div class=“triangle”></div>

这是jsfiddle

试试看:-

.三角形{边框颜色:透明透明红色透明;边框样式:实心;边框宽度:0px 200px 200px;高度:0px;宽度:0px;}<div class=“triangle”></div>

更进一步,使用基于此的css,我在上一个按钮和下一个按钮上添加了箭头(是的,我知道它不是100%跨浏览器,但仍然很流畅)。

.三角形{宽度:0;高度:0;左边框:50px实心透明;右边框:50px实心透明;边框底部:100px纯红色;边距:20px自动;}.向下三角形{边框底部:无;边框顶部:100px纯红色;}.左三角形{左侧边框:无;右边框:100px纯红色;边框底部:50px实心透明;边框顶部:50px实心透明;}.直角三角形{边界权:无;左边框:100px纯红色;边框底部:50px实心透明;边框顶部:50px实心透明;}.三角形后:后{宽度:0;高度:0;左侧边框:5px实心透明;右边框:5px实心透明;边框底部:5px纯红色;边距:0 5px;内容:“”;显示:内联块;}.右后三角:后{边界权:无;左侧边框:5px纯蓝色;边框底部:5px实心透明;边框顶部:5px实心透明;}前三角形:前{宽度:0;高度:0;左侧边框:5px实心透明;右边框:5px实心透明;边框底部:5px纯蓝色;边距:0 5px;内容:“”;显示:内联块;}.左前三角:前{左侧边框:无;右边框:5px纯蓝色;边框底部:5px实心透明;边框顶部:5px实心透明;}<div class=“triangle”></div><div class=“triangle triangle down”></div><div class=“triangle triangle left”></div><div class=“triangle triangle right”></div><a class=“triangle before triangle after left”href=“#”>返回</a><a class=“triangle after triangle”href=“#”>下一步</a>

SASS(SCSS)三角形混合

我写这篇文章是为了让自动生成CSS三角形变得更容易(也更干燥):

// Triangle helper mixin (by Yair Even-Or)
// @param {Direction} $direction - either `top`, `right`, `bottom` or `left`
// @param {Color} $color [currentcolor] - Triangle color
// @param {Length} $size [1em] - Triangle size
@mixin triangle($direction, $color: currentcolor, $size: 1em) {
  $size: $size/2;
  $transparent: rgba($color, 0);
  $opposite: (top:bottom, right:left, left:right, bottom:top);

  content: '';
  display: inline-block;
  width: 0;
  height: 0;
  border: $size solid $transparent;
  border-#{map-get($opposite, $direction)}-color: $color;
  margin-#{$direction}: -$size;
}

用例示例:

span {
  @include triangle(bottom, red, 10px);
}

游乐场页面


重要提示:如果三角形在某些浏览器中显示为像素化,请尝试此处描述的方法之一。

使用剪辑路径:多边形(50%0%,100%100%,0%100%);用于创建简单的三角形

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

.triangle{width:200px; height:200px;background:#000;clip-path: polygon(50% 0%, 100% 100%, 0% 100%);}