我可以实现带有实心边框的六边形,如下所示:
.hex {
margin-top: 70px;
width: 208px;
height: 120px;
background: red;
position: relative;
}
.hex:before,
.hex:after {
content: "";
border-left: 104px solid transparent;
border-right: 104px solid transparent;
position: absolute;
}
.hex:before {
top: -59px;
border-bottom: 60px solid red;
}
.hex:after {
bottom: -59px;
border-top: 60px solid red;
}
.hex.inner {
background-color: lightgreen;
-webkit-transform: scale(.98, .98);
-moz-transform: scale(.98, .98);
transform: scale(.98, .98);
z-index: 1;
}
.hex.inner:before {
border-bottom: 60px solid lightgreen;
}
.hex.inner:after {
border-top: 60px solid lightgreen;
}
<div class="hex">
<div class="hex inner">
</div>
</div>
但是,我想创建一个带有虚线边框的六边形,如下图所示:
最佳答案
HTML程式码:
<div class="hexagon"><span></span></div>
CSS代码:
.hexagon {
position: relative;
width: 300px;
height: 173.21px;
background-color: lightgreen;
margin: 86.60px 0;
border-left: 3px dotted #f00;;
border-right:3px dotted #f00;
box-shadow: 0 0 20px rgba(0,0,0,0.6);
}
.hexagon:before,
.hexagon:after {
content: "";
position: absolute;
z-index: 1;
width: 212.13px;
height: 212.13px;
-webkit-transform: scaleY(0.5774) rotate(-45deg);
-ms-transform: scaleY(0.5774) rotate(-45deg);
transform: scaleY(0.5774) rotate(-45deg);
background-color: inherit;
left: 40.9340px;
box-shadow: 0 0 20px rgba(0,0,0,0.6);
}
.hexagon:before {
top: -106.0660px;
border-top: 3px dotted #f00;
border-right:3px dotted #f00;
}
.hexagon:after {
bottom: -106.0660px;
border-bottom: 3px dotted #f00;
border-left: 3px dotted #f00;
}
/*cover up extra shadows*/
.hexagon span {
display: block;
position: absolute;
top:1.7320508075688772px;
left: 0;
width:294px;
height:169.7410px;
z-index: 2;
background: inherit;
}
输出 :
根据需要应用颜色。