如何仅使用 HTML & CSS 构建一个特殊的多边形(风筝形状)?

How to build a special polygon (a kite shape) with HTML & CSS only?

我正在做我的新项目,在这个项目中我需要一些不规则的结构。 其中之一是:

我取得的成绩是:

.mainkite {
  width: 200px;
  height: 200px;
  background: #f00;
  transform: skew(180deg, 180deg) rotate(45deg);
  position: relative;
  margin: 0px auto;
  margin-top: 50px;
  overflow: hidden;
}
.midLine {
  border: solid 1px #000;
  transform: skew(180deg, 180deg) rotate(45deg);
  position: absolute;
  top: 99px;
  width: 140%;
  left: -41px;
}
<div class="mainkite">
  <div class="midLine"></div>
</div>

jsfiddle

我怎样才能得到我想要的其余形状?

与 CSS :

使用:

  • 仅HTML和CSS
  • 2 个元素和 2 个伪元素
  • 内线的边界半径和变换
  • 底部三角形border technique

.kite {
  position: relative;
  width: 200px; height: 200px;
  background: #f00;
  transform: rotate(45deg);
  margin: 0px auto;
  margin-top: 50px;
}
.kite:before, .kite:after {
  content: '';
  position: absolute;
}
.kite:before {
  top: 50%; left: -20.5%;
  width: 141%;
  margin-top:-1px;
  border-top: 2px solid #000;
  transform: rotate(45deg);
}
.kite:after {
  top: 0; left: 0;
  width: 198px; height: 198px;
  border-top-left-radius: 100%;
  border-left: 2px solid #000;
  border-top: 2px solid #000;
}
.tail {
  position: absolute;
  top: 199px; left: 199px;
  width:60px; height:60px;
  overflow:hidden;
}
.tail:before{
  content:'';
  display:block;
  width:141%; height:100%;
  background:#000;
  transform-origin:0 100%;
  transform:rotate(-45deg);
}
<div class="kite"><span class="tail"></span>
</div>

使用 SVG

您应该考虑的另一种方法是使用 inline SVG。由于您似乎正在制作图形元素,因此 SVG 在语义上可能更合适并且:

  • 易于扩展
  • 更短的代码
  • 更好地控制形状和曲线

在下面的例子中我使用 polyline elements to make the red square, the bottom black triangle and the vertical line. For the circular line, I use a path element with a quadratic bezier curve command :

svg{display:block;width:400px;margin:0 auto;}
<svg viewbox="0 0 10 10">
  <polyline fill="red" points="5 0 9 4 5 8 1 4" />
  <polyline points="5 0 5.05 0.05 5.05 7.95 5 8 4.95 7.95 4.95 0.05" />
  <path d="M1.05 4.05 Q5 1 8.95 4.05" fill="none" stroke-width="0.1" stroke="#000" />
  <polyline points="5 8 6 9 4 9 " />
</svg>

奖金

感谢 Harry 让我进一步思考这个问题并让我找到另一个 CSS 唯一的方法 div :

.kite {
  position: relative;
  width: 200px; height: 200px;
  background: #f00;
  transform: rotate(45deg);
  margin: 0px auto;
  margin-top: 50px;
}
.kite:before, .kite:after {
  content: '';
  position: absolute;
}
.kite:before {
  top: 50px; left: -41px;
  width: 282px; height: 2px;
  margin-top: -1px;
  background: #000;
  transform-origin: 141px 52px;
  transform: rotate(45deg);
  background-clip: content-box;
  border-right: 50px solid #000;
  border-top: 50px solid transparent;
  border-bottom: 50px solid transparent;
}
.kite:after {
  top: 0; left: 0;
  width: 198px; height: 198px;
  border-top-left-radius: 100%;
  border-left: 2px solid #000;
  border-top: 2px solid #000;
}
<div class="kite"></div>

我分别为 Arc 和 Tail 创建了两个 div。我将 mainkitetale 包装在一个 div 中以正确定位尾部,因为 mainkite 溢出已设置为 hidden

你可以看到我的jsfiddle:https://jsfiddle.net/80qs2a4y/7/

Arc 是通过简单地添加 border-radius: 50%; 并增加宽度和高度到 200%.

创建的

参考:仅使用 CSS 创建三角形:https://css-tricks.com/snippets/css/css-triangle/

web-tiki 给出的答案非常好,我建议将 SVG 用于复杂的形状,原因与他的答案中所述的相同。然而,使用 CSS 创建此形状相当简单,下面是仅使用一个元素创建此形状的另一种变体。

黑色的尾巴部分是 pseudo-element 而红色的风筝是它的 box-shadow。中间的线是使用 parent 上的 linear-gradient 创建的,弯曲的字符串是第二个伪。

我为所有部分使用了视口单位以使输出具有响应性。这是因为框阴影不能采用百分比值并且不能响应,除非使用视口单位。

.kite {
  position: relative;
  height: 25vw;
  width: 25vw;
  background: linear-gradient(to right, transparent calc(50% - 1px), black calc(50% - 1px), black calc(50% + 1px), transparent calc(50% + 1px));
  overflow: hidden;
}
.kite:before {
  position: absolute;
  content: '';
  top: calc(84.5% + 1px);  /* (15/25 * 1.414 is approximately 84.5% + 1px for correction */
  left: 50%;
  height: 15vw;
  width: 15vw;
  background: black;
  transform-origin: left top;
  transform: rotate(45deg);
  box-shadow: -15vw -15vw 0px red; /* the x and y are same as height and width */
  z-index: -1;
}
.kite:after {
  position: absolute;
  content: '';
  top: calc(0% - 2px);
  left: calc(50% + 1px);
  width: calc(15vw - 2px);
  height: calc(15vw - 1px);
  border-top-left-radius: 100%;
  border-left: 2px solid #000;
  border-top: 2px solid #000;
  transform-origin: left top;
  transform: rotate(45deg);
}
<div class="kite"></div>