我已阅读 this回答,但我正在使用 here 中的以下代码制作六边形。虽然六边形 react 灵敏,但无法使其拉长。我不太喜欢使用边框解决方案,而是想添加类来覆盖现有样式。
.socialIcon {
width: 100px;
height: 100px;
color: black;
transition: color 400ms ease-in;
}
.color {
background-color: #1ae694;
transition: background-color 500ms ease-in;
}
.hexagonWrapper {
text-align: center;
position: relative;
display: inline-block;
margin: 50px;
}
.iconContainer {
position: absolute;
display: flex;
align-items: center;
justify-content: center;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
.hexagon {
height: 100%;
width: calc(100% * 0.57735);
display: inline-block;
}
.hexagon:before {
position: absolute;
top: 0;
right: calc((100% / 2) - ((100% * 0.57735) / 2));
background-color: inherit;
height: inherit;
width: inherit;
content: "";
transform: rotateZ(60deg);
}
.hexagon:after {
position: absolute;
top: 0;
right: calc((100% / 2) - ((100% * 0.57735) / 2));
background-color: inherit;
height: inherit;
width: inherit;
content: "";
transform: rotateZ(-60deg);
}
<a class="socialIcon hexagonWrapper" href="#" target="_blank">
<div class="color hexagon"></div>
<div class="iconContainer">
<span>H</span>
</div>
</a>
更新的答案
我建议您使用来自 answer 的 Temani 方法.
它使用多个 linear-gradient
来创建背景,并且在 HTML 中需要更少的元素:
.socialIcon {
color: black;
transition: color 400ms ease-in;
}
.color {
--color: #1ae694;
background-color: var(--color);
}
.hexagonWrapper {
text-align: center;
position: relative;
display: inline-block;
}
.hexagon {
padding: 10px 60px;
display: inline-block;
background:
linear-gradient(to top right, var(--color) 49.5%, transparent 50.5%) top right / 20px 50%,
linear-gradient(to bottom right, var(--color) 49.5%, transparent 50.5%) bottom right / 20px 50%,
linear-gradient(to bottom left, var(--color) 49.5%, transparent 50.5%) bottom left / 20px 50%,
linear-gradient(to top left, var(--color) 49.5%, transparent 50.5%) top left / 20px 50%,
linear-gradient(var(--color), var(--color)) center / calc(100% - 40px) 100%;
background-repeat:no-repeat;
}
/* Added after comment */
.hexagon:hover {
--color: #aae;
}
<a class="socialIcon hexagonWrapper" href="#" target="_blank">
<div class="color hexagon"><p>xxx</p></div>
</a>
⋅⋅⋅
旧答案
由于拉长的六边形不再是六边形,我建议您使用一种完全不同的方法来实现这一目标,使用:
height
和 color
的 CSS 变量, 伪元素上的
border
以创建“三 Angular 形”形状。
这是一个片段:
.socialIcon {
--color: #1ae694; /* CSS Variable */
--h: 100px; /* CSS Variable */
width: 100px;
height: var(--h); /* Using CSS var */
color: black;
transition: color 200ms ease-in;
}
.socialIcon:hover {
--color: #aae;
}
.color {
background-color: var(--color); /* Using CSS var */
transition: background-color 500ms ease-in;
}
.hexagonWrapper {
text-align: center;
position: relative;
display: inline-block;
margin: 50px;
}
.iconContainer {
position: absolute;
display: flex;
align-items: center;
justify-content: center;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
.hexagon {
height: 100%;
width: 100%;
display: inline-block;
}
/* TAKIT: I changed everything below */
.hexagon::before,
.hexagon::after {
position: absolute;
height: 0;
width: 0;
content: "";
border: 0 solid transparent;
/* border is full height but half width, to render a thin arrow shape
This can be parameterized here: */
border-width: calc(var(--h)/2) calc(var(--h)/4); /* Using CSS var */
transition:
border-left-color 500ms ease-in,
border-right-color 500ms ease-in;
}
.hexagon::before {
left: 0;
transform: translate(-100%, 0);
border-right-color: var(--color); /* Using CSS var */
}
.hexagon::after {
right: 0;
transform: translate(100%, 0);
border-left-color: var(--color); /* Using CSS var */
}
<a class="socialIcon hexagonWrapper" href="#" target="_blank">
<div class="color hexagon"></div>
<div class="iconContainer">
<span>H</span>
</div>
</a>
使用该解决方案,唯一需要调整的是 var: --h
以获得实际的六边形或拉长的六边形......
我是一名优秀的程序员,十分优秀!