Sun*_*lot 23 html css svg css3 css-shapes
我正在研究我的新项目,在此我需要一些不规则的结构.其中之一是:

我取得的成就是:
.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;
}Run Code Online (Sandbox Code Playgroud)
<div class="mainkite">
<div class="midLine"></div>
</div>Run Code Online (Sandbox Code Playgroud)
我怎样才能得到我想要的其他形状?
web*_*iki 37
使用:
.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);
}Run Code Online (Sandbox Code Playgroud)
<div class="kite"><span class="tail"></span>
</div>Run Code Online (Sandbox Code Playgroud)
您应该考虑的另一种方法是使用内联SVG.由于您似乎正在制作图形元素,因此SVG更适合于语义和:
在下面的例子中,我使用折线元素来制作红色正方形,底部黑色三角形和垂直线.对于圆形线,我使用带有二次贝塞尔曲线命令的路径元素:
svg{display:block;width:400px;margin:0 auto;}Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)
THX到哈利让我多一些思考这一点,让我找到另一个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;
}Run Code Online (Sandbox Code Playgroud)
<div class="kite"></div>Run Code Online (Sandbox Code Playgroud)
Har*_*rry 13
web-tiki给出的答案非常好,我建议将SVG用于复杂形状,其原因与答案中指出的相同.然而,使用CSS创建此形状相当简单,而下面是仅使用一个元素创建此形状的另一种形式.
黑尾部分是伪元素,而红色风筝是它box-shadow.中间的线是使用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);
}Run Code Online (Sandbox Code Playgroud)
<div class="kite"></div>Run Code Online (Sandbox Code Playgroud)
我分别为Arc和Tail创建了两个div.当我设置溢出时,我在一个div中包裹mainkite并tale正确定位尾部.mainkitehidden
你可以看到我的jsfiddle:https://jsfiddle.net/80qs2a4y/7/
通过简单地添加border-radius: 50%;和增加宽度和高度来创建弧200%.
参考:仅使用CSS创建三角形:https://css-tricks.com/snippets/css/css-triangle/
| 归档时间: |
|
| 查看次数: |
4972 次 |
| 最近记录: |