CSS中的左右三角形

and*_*e91 1 css css3 css-shapes

嗨,我正在努力做到以下几点:

在此处输入图片说明

它们的三角形的高度大约为容器的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;
}
Run Code Online (Sandbox Code Playgroud)
<header>
  <div class="right"></div>
  <div class="left"></div>

</header>
Run Code Online (Sandbox Code Playgroud)

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

Tem*_*fif 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;
}
Run Code Online (Sandbox Code Playgroud)
<div class="box">

</div>
Run Code Online (Sandbox Code Playgroud)

相关答案以获取更多详细信息: CSS三角形如何工作?


如果您想拥有其他元素,则可以使用伪元素(可以替换为通用元素)的另一种想法。

.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%);
}
Run Code Online (Sandbox Code Playgroud)
<div class="box">

</div>
Run Code Online (Sandbox Code Playgroud)

  • 他们是,他们为我做 (2认同)