在 css3 中制作带有遮罩和边框的六边形
Making a hexagon with a mask and border in css3
我正在尝试制作一个带有边框半径、边框和类似遮罩的六边形。是这样的:
但是,我对 'mask' 以及我应该如何制作该形状感到有点困惑。将其导出为 svg 是否比使用 css3 制作它更好?
目前我有这个:
<div id="hexagon"></div>
#hexagon {
position: relative;
margin: 1em auto;
width: 10.2em;
height: 17.32em;
border-radius: 1em/.5em;
background: #FF5E5E;
transition: opacity .5s;
cursor: pointer;
border-top: 2px solid black;
border-bottom: 2px solid black;
}
#hexagon:before {
position: absolute;
width: inherit;
height: inherit;
border-radius: inherit;
background: inherit;
content: '';
-webkit-transform: rotate(60deg); /* Chrome, Opera 15+, Safari 3.1+ */
-ms-transform: rotate(60deg); /* IE 9 */
transform: rotate(60deg); /* Firefox 16+, IE 10+, Opera */
border-top: 2px solid black;
border-bottom: 2px solid black;
}
#hexagon:after {
position: absolute;
width: inherit;
height: inherit;
border-radius: inherit;
background: inherit;
content: '';
-webkit-transform: rotate(-60deg); /* Chrome, Opera 15+, Safari 3.1+ */
-ms-transform: rotate(-60deg); /* IE 9 */
transform: rotate(-60deg); /* Firefox 16+, IE 10+, Opera */
border-top: 2px solid black;
border-bottom: 2px solid black;
}
但即便如此,这也不是很好,因为它在拐角处看起来有点不对劲:
我会考虑 my previous answer 我将在其中复制形状以创建边框:
.hex {
position:relative;
--s:200px;
width: 200px;
height:calc(0.866*var(--s));
}
.hex div {
filter: url('#goo');
position:absolute;
inset:0;
}
.hex div::before{
content: "";
position:absolute;
inset:0;
background:#000;
clip-path: polygon(25% 0%, 75% 0%, 100% 50%, 75% 100%, 25% 100%, 0% 50%);
}
.hex div:nth-child(2)::before{
background:red;
inset:4px;
}
<div class="hex">
<div></div>
<div></div>
</div>
<svg style="visibility: hidden; position: absolute;" width="0" height="0" xmlns="http://www.w3.org/2000/svg" version="1.1">
<defs>
<filter id="goo"><feGaussianBlur in="SourceGraphic" stdDeviation="8" result="blur" />
<feColorMatrix in="blur" mode="matrix" values="1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 19 -9" result="goo" />
<feComposite in="SourceGraphic" in2="goo" operator="atop"/>
</filter>
</defs>
</svg>
我正在尝试制作一个带有边框半径、边框和类似遮罩的六边形。是这样的:
但是,我对 'mask' 以及我应该如何制作该形状感到有点困惑。将其导出为 svg 是否比使用 css3 制作它更好?
目前我有这个:
<div id="hexagon"></div>
#hexagon {
position: relative;
margin: 1em auto;
width: 10.2em;
height: 17.32em;
border-radius: 1em/.5em;
background: #FF5E5E;
transition: opacity .5s;
cursor: pointer;
border-top: 2px solid black;
border-bottom: 2px solid black;
}
#hexagon:before {
position: absolute;
width: inherit;
height: inherit;
border-radius: inherit;
background: inherit;
content: '';
-webkit-transform: rotate(60deg); /* Chrome, Opera 15+, Safari 3.1+ */
-ms-transform: rotate(60deg); /* IE 9 */
transform: rotate(60deg); /* Firefox 16+, IE 10+, Opera */
border-top: 2px solid black;
border-bottom: 2px solid black;
}
#hexagon:after {
position: absolute;
width: inherit;
height: inherit;
border-radius: inherit;
background: inherit;
content: '';
-webkit-transform: rotate(-60deg); /* Chrome, Opera 15+, Safari 3.1+ */
-ms-transform: rotate(-60deg); /* IE 9 */
transform: rotate(-60deg); /* Firefox 16+, IE 10+, Opera */
border-top: 2px solid black;
border-bottom: 2px solid black;
}
但即便如此,这也不是很好,因为它在拐角处看起来有点不对劲:
我会考虑 my previous answer 我将在其中复制形状以创建边框:
.hex {
position:relative;
--s:200px;
width: 200px;
height:calc(0.866*var(--s));
}
.hex div {
filter: url('#goo');
position:absolute;
inset:0;
}
.hex div::before{
content: "";
position:absolute;
inset:0;
background:#000;
clip-path: polygon(25% 0%, 75% 0%, 100% 50%, 75% 100%, 25% 100%, 0% 50%);
}
.hex div:nth-child(2)::before{
background:red;
inset:4px;
}
<div class="hex">
<div></div>
<div></div>
</div>
<svg style="visibility: hidden; position: absolute;" width="0" height="0" xmlns="http://www.w3.org/2000/svg" version="1.1">
<defs>
<filter id="goo"><feGaussianBlur in="SourceGraphic" stdDeviation="8" result="blur" />
<feColorMatrix in="blur" mode="matrix" values="1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 19 -9" result="goo" />
<feComposite in="SourceGraphic" in2="goo" operator="atop"/>
</filter>
</defs>
</svg>