html - 如何对齐几何形状
问题描述
我正在尝试对齐如下图所示的几何形状
我尝试使用绝对位置,但没有成功,这是我尝试使用的代码
<div className={style.container}>
<div className={style.hexagons}>
<div>
<Hexagon />
</div>
<Hexagon />
<div className={style.teste}>
<Hexagon />
</div>
</div>
</div>
CSS:
.container {
height: 800px;
}
.hexagons {
display: flex;
position: absolute;
width: 100%;
}
.teste {
margin-top: 50px;
}
屏幕上是这样的
如果你能帮助我,或者告诉我一个感激的方式
解决方案
将它们全部放在彼此之上,然后使用转换。这是一个例子:
.box {
width: 100px; /* adjust this to control the size */
margin: 80px auto 0;
display: grid;
}
.box div {
grid-area: 1/1; /* above each other */
clip-path: polygon(25% 0%, 75% 0%, 100% 50%, 75% 100%, 25% 100%, 0% 50%);
transform: rotate(var(--d)) translate(-52%); /* a little bigger than 50% to create gap */
}
.box div::before {
content: "";
padding-top: 86.6%; /* 100%*cos(30) */
display: block;
}
.box div:nth-child(1) {--d:0deg; }
.box div:nth-child(2) {--d:120deg; }
.box div:nth-child(3) {--d:240deg; }
<div class="box">
<div style="background:red;"></div>
<div style="background:blue;"></div>
<div style="background:green;"></div>
</div>
没有 CSS 网格以获得更好的支持:
.box {
width: 100px; /* adjust this to control the size */
margin: 80px auto 0;
position:relative;
}
.box div {
clip-path: polygon(25% 0%, 75% 0%, 100% 50%, 75% 100%, 25% 100%, 0% 50%);
transform: rotate(var(--d)) translate(-52%); /* a little bigger than 50% to create gap */
}
.box div:not(:first-child) {
position:absolute;
top:0;
left:0;
right:0;
}
.box div::before {
content: "";
padding-top: 86.6%; /* 100%*cos(30) */
display: block;
}
.box div:nth-child(1) {--d:0deg; }
.box div:nth-child(2) {--d:120deg; }
.box div:nth-child(3) {--d:240deg; }
<div class="box">
<div style="background:red;"></div>
<div style="background:blue;"></div>
<div style="background:green;"></div>
</div>
推荐阅读
- python - 如何使用函数参数作为分配给函数中 input() 的变量的名称?
- css - React devtools 自动触发调试器
- python - 运行 Python 脚本的 Linux 服务不更新 SQL 数据库
- python - Python / Google API 客户端 v3 / 确保线程安全
- ios - SwiftUI 中默认展开的 List 或 OutlineGroup
- c# - 为什么 C# Math.Sin() 从 C++ sin() 返回不同的值?
- python - 在 Python 中使用 replace() 函数替换字符串中的元音
- amazon-web-services - AWS Fargate 可以像 Lambda 那样自动扩展吗?
- fonts - PostScript /(字体名称) FindFont 产生“未找到,使用 Courier”。
- c++ - 我可以隐藏第三方头文件中的定义吗?