是否可以用CSS制作圆角三角形?

AnA*_*ice 7 html css html5 css3 css-shapes

我想用CSS制作以下形状:

圆形Triange

这是我目前拥有的:

.triangle {
  width: 0;
  height: 0;
  border-style: solid;
  border-width: 56px 56px 0 0;
  border-color: #ff4369 transparent transparent transparent;
}
Run Code Online (Sandbox Code Playgroud)
<div class="triangle">
</div>
Run Code Online (Sandbox Code Playgroud)

我无法使用边框半径使左上角变圆...有没有其他方法可以做到这一点,还是我需要使用SVG?

ash*_*q.p 8

是的,可以使用border-radius:

.triangle {
  box-sizing: border-box;
  width: 60px;
  height: 60px;
  border-top: solid 30px rgb(200,30,50);
  border-left: solid 30px rgb(200,30,50);
  border-top-left-radius: 12px;
  border-right: solid 30px transparent;
  border-bottom: solid 30px transparent;
}
Run Code Online (Sandbox Code Playgroud)
<div class="triangle triangle-0"></div>
Run Code Online (Sandbox Code Playgroud)


VXp*_*VXp 5

您可以考虑border-radiusclip-path: polygon()解决方案:

.triangle {
  width: 56px;
  height: 56px;
  background: #ff4369;
  border-top-left-radius: 12px;
  -webkit-clip-path: polygon(0 0, 0% 100%, 100% 0);
  clip-path: polygon(0 0, 0% 100%, 100% 0);
}
Run Code Online (Sandbox Code Playgroud)
<div class="triangle"></div>
Run Code Online (Sandbox Code Playgroud)

使用:beforeor :after 伪元素的另一个解决方案,可以作为一个层:

.triangle {
  width: 56px;
  height: 56px;
  background: #ff4369;
  border-top-left-radius: 12px;
  position: relative;
  overflow: hidden;
}

.triangle:after {
  content: "";
  position: absolute;
  top: 0;
  left: 50%;
  width: 100%;
  height: 100%;
  background: #fff;
  transform: skew(-45deg);
}
Run Code Online (Sandbox Code Playgroud)
<div class="triangle"></div>
Run Code Online (Sandbox Code Playgroud)

  • IE和EDGE不支持clip-path.https://caniuse.com/#feat=css-clip-path (4认同)
  • @ ashfaq.p你看到有任何支持需求吗? (4认同)

Tem*_*fif 5

您可以轻松地使用linear-gradient它并得到很好的支持:

body {
  background: #ccc;
}

.triangle {
  margin: 10px;
  width: 56px;
  height: 56px;
  background: linear-gradient(to bottom right, #ff4369 50%, transparent 0);
  border-radius: 10px 0 0 0;
}
Run Code Online (Sandbox Code Playgroud)
<div class="triangle">
</div>
Run Code Online (Sandbox Code Playgroud)


and*_*eas 5

To actually answer your question (and provide the first answer without border-radius): If you want a CSS only solution, you will have to use border-radius.

Nevertheless I would highly recommend to use SVG for creating shapes, as simple shapes like this are easy to create manually, it's responsive, it's widely supported now and (as @chharvey mentioned in the comments) semantically more appropriate.

<svg viewbox="0 0 50 50" height="56px">
  <path d="M1 50 V10 Q1 1 10 1 H50z" fill="#ff4369" />
</svg>
Run Code Online (Sandbox Code Playgroud)

You can find more information about the path properties in the specs.