gpt4 book ai didi

带 Angular CSS 菱形 (120, 60, 120, 60)

转载 作者:太空宇宙 更新时间:2023-11-04 06:39:34 26 4
gpt4 key购买 nike

是否可以用纯 CSS 创建一个菱形形状, Angular 分别为 120 度和 60 度?

Rhombus image

最佳答案

一个带有渐变的简单背景,您可以使用元素的宽度/高度轻松调整以控制 Angular 和尺寸:

.box {
background:
linear-gradient(to top right ,orange 49.5%, transparent 50%) top right,
linear-gradient(to top left ,orange 49.5%, transparent 50%) top left,
linear-gradient(to bottom right,orange 49.5%, transparent 50%) bottom right,
linear-gradient(to bottom left ,orange 49.5%, transparent 50%) bottom left;
background-size:50% 50%;
background-repeat:no-repeat;

width:260px; /* 2*sin(120/2)*150 = 260 */
height:150px; /* 2*sin(60/2)*150 = 150 OR 2*cos(120/2)*150 = 150*/
display:inline-block;
margin:5px;
}
<div class="box">
</div>
<div class="box" style="height:100px;">
</div>
<div class="box" style="width:200px;">
</div>

为确保保持 Angular 不变,请考虑保持 div 的纵横比:

.box {
background:
linear-gradient(to top right ,orange 49.5%, transparent 50%) top right,
linear-gradient(to top left ,orange 49.5%, transparent 50%) top left,
linear-gradient(to bottom right,orange 49.5%, transparent 50%) bottom right,
linear-gradient(to bottom left ,orange 49.5%, transparent 50%) bottom left;
background-size:50% 50%;
background-repeat:no-repeat;

width:260px;
display:inline-block;
margin:5px;
}

/* 2*sin(120/2)*h = w
h = w / (sin(60)*2)
h = w * 0.57736
*/
.box:before {
content:"";
display:block;
padding-top:57.73%;
}
<div class="box">
</div>
<div class="box" style="width:150px;">
</div>
<div class="box" style="width:80px;">
</div>

CSS Rhombus shape

相同的想法,但使用clip-path

.box {
background:orange;
clip-path:polygon(0 50%, 50% 100%,100% 50%,50% 0);
width:260px;
display:inline-block;
margin:5px;
}

/* 2*sin(120/2)*h = w
h = w / (sin(60)*2)
h = w * 0.57736
*/
.box:before {
content:"";
display:block;
padding-top:57.73%;
}
<div class="box">
</div>
<div class="box" style="width:150px;">
</div>
<div class="box" style="width:80px;">
</div>

如果你想要一些边框,你可以调整渐变:

.box {
--g:var(--c,orange) calc(49.5% - 3px),#000 calc(49.5% - 2px),#000 49.5%, transparent 50%;
background:
linear-gradient(to top right ,var(--g)) top right,
linear-gradient(to top left ,var(--g)) top left,
linear-gradient(to bottom right,var(--g)) bottom right,
linear-gradient(to bottom left ,var(--g)) bottom left;
background-size:50% 50%;
background-repeat:no-repeat;

width:260px;
display:inline-block;
margin:5px;
}

/* 2*sin(120/2)*h = w
h = w / (sin(60)*2)
h = w * 0.57736
*/
.box:before {
content:"";
display:block;
padding-top:57.73%;
}
<div class="box">
</div>
<div class="box" style="--c:transparent;">
</div>

CSS Rhombus shape with border

关于带 Angular CSS 菱形 (120, 60, 120, 60),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53917297/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com