CSS中的左右三角形

时间:2019-05-07 14:47:04

标签: css css3 css-shapes

嗨,我正在尝试做以下事情:

enter image description here

它们的三角形的高度应为容器的高度的40%,宽度应为50%,因此它们在中间相遇。

我一直在尝试做类似的事情..但到目前为止没有成功..

环顾四周,我发现到目前为止我什么也无法使用。

我的代码:

div {
  height: 373px;
  width: 0px;
  margin: 26px;
  display: inline-block;
}

.left {
  border-bottom: 100px solid #ff0;
  border-left: 320px solid transparent;
}

.right {
  border-bottom: 100px solid #f00;
  border-right: 320px solid transparent;
}

header {
  border: 2px solid black;
  width: 50%;
  height: 500px;
}
<header>
  <div class="right"></div>
  <div class="left"></div>

</header>

希望有人比我聪明,看看我应该从这里去...

3 个答案:

答案 0 :(得分:6)

使用如下背景色:

.box {
  height:300px;
  background:
    /* Right bottom triangle*/
    linear-gradient(to bottom right,transparent 49.5%,blue 50%) bottom right,
    /* left bottom triangle*/
    linear-gradient(to bottom left ,transparent 49.5%,red  50%) bottom left ,
    yellow;
  background-size:50% 40%; /* Width height*/
  background-repeat:no-repeat;
}
<div class="box">

</div>

相关答案以获取更多详细信息:How do CSS triangles work?


关于伪元素的另一种想法(可以替换为通用元素),以防您拥有其他元素。

.box {
  height: 300px;
  background: yellow;
  position: relative
}

.box:before,
.box:after {
  content: "";
  position: absolute;
  height: 40%;
  width: 50%;
  bottom: 0;
}

.box:before {
  left: 0;
  background: linear-gradient(to bottom left, transparent 49.5%, red 50%);
}

.box:after {
  right: 0;
  background: linear-gradient(to bottom right, transparent 49.5%, blue 50%);
}
<div class="box">

</div>

答案 1 :(得分:1)

由于需要百分比值,因此可以使用clip-path。请注意,某些浏览器https://caniuse.com/#feat=css-clip-path可能不完全支持它,对于某些浏览器,您可能需要前缀(例如-webkit-clip-path

.wrap {
  height: 200px;
  width: 100%;
  position: relative;
  background: #333;
}
.triangle {
  background: red;
  clip-path: polygon(0 40%, 0% 100%, 100% 100%);
  position: absolute;
  left: 0;
  bottom: 0;
  top: 0;
  width: 50%;
}
.triangle.tr-right {
  left: auto;
  right: 0;
  clip-path: polygon(100% 40%, 0% 100%, 100% 100%);
}

<div class="wrap">
  <div class="triangle tr-left"></div>
  <div class="triangle tr-right"></div>
</div>

JSFiddle

使用Clippy创建的剪切路径

答案 2 :(得分:-1)

 * {
        box-sizing: border-box;
      }
      .triangular-pointer-box {
        display: flex;
        align-items: center;
        background: #161616;
        padding: 20px;
        padding-left: 120px;
        height: 200px;
        position: relative;
        width: 80%;
      }
      .triangular-pointer-box > h3 {
        color: #fff;
      }

      .triangular-pointer-box:after {
        content: "";
        width: 0;
        height: 0;
        border-top: 100px solid transparent;
        border-bottom: 100px solid transparent;
        border-left: 100px solid #161616;
        position: absolute;
        right: -100px;
        top: 0;
      }
      .triangular-pointer-box:before {
        content: "";
        width: 0;
        height: 0;
        border-top: 100px solid transparent;
        border-bottom: 100px solid transparent;
        border-left: 100px solid #ffffff;
        position: absolute;
        left: 0;
        top: 0;
      }
<div class="triangular-pointer-box">
      <h3>Title goes here</h3>
    </div>