CSS3 带虚线边框的六边形

CSS3 Hexagon with dotted border

我可以实现如下所示的具有实心边框的六边形:

.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>

但是,我想创建一个 带虚线边框的六边形,如下图所示:

这是一个 inline svg 使用的方法:

svg{width:30%;margin:0 auto;}
<svg viewbox="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg">
  <polygon fill="#92D050" 
           stroke="red"
           stroke-width="1"
           stroke-linecap="round"
           stroke-dasharray="0.5,3"
           points="50 1 95 25 95 75 50 99 5 75 5 25"/>
</svg>

您无法使用有问题的方法创建虚线边框,因为形状本身是使用边框创建的。产生六边形的 border-topborder-bottom。当您为其设置 dotted 边框样式时,您得到的只是非常大的点,这与预期的输出不同。虽然您可以使用其他 CSS 方法来创建所需的形状 + 边框(如其他答案中所述),但最好对此类复杂形状使用 SVG,因为它很容易。

您可以使用单个 path 元素使用 SVG 轻松完成此操作。一旦您很好地理解创建它时使用的命令,path 就很容易创建。

解释如下:

  • M5,30 - 此命令 M 将虚构的笔移动到由 (5,30) 表示的点。
  • L50,0 - 从前一点 (5,30) 到点 (50,0) 绘制一条 L 直线。
  • 95,30 95,70 50,100 5,70 - 这些与前面的命令相同,并绘制到各个点的线。命令 (L) 本身不需要重复,因为它是相同的。

虚线边框是通过为 stroke-dasharraystroke-linecap 属性设置正确的值来实现的(如 web-tiki 的回答中所述)。

svg {
  height: 200px;
  width: 200px;
}
path {
  fill: green;
  stroke: red;
  stroke-dasharray: 0.1, 3;
  stroke-linecap: round;
}
<svg viewBox='0 0 100 100'>
  <path d='M5,30 L50,0 95,30 95,70 50,100 5,70z' />
</svg>

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;
}

输出:

根据需要应用颜色。

喜欢 SVG 解决方案(@web-tiki 和@Harry),但这是一个使用 3 个矩形的 CSS 解决方案:

.main{
  padding: 50px;
  position: relative;
}

.a, .b, .c{
  position: absolute;
  bottom: 0;
  width: 120px;
  height: 70px;
  background-color: green;
  border-left: 2px dotted red;
  border-right: 2px dotted red;
}

.a{
  z-index: 1;
}

.b{
  transform: rotate(60deg);
  z-index: 2;
}

.c{
  transform: rotate(120deg);
  z-index: 3;
}
<div class="main">
  <div class="a"></div>
  <div class="b"></div>
  <div class="c"></div>
</div>

JSFiddle


仅使用一个 HTML 元素的解决方案:

body{
  padding: 50px;
}

.hex, .hex:before, .hex:after{
  width: 120px;
  height: 70px;
  background-color: green;
  border-left: 2px dotted red;
  border-right: 2px dotted red;
}

.hex:before, .hex:after{
  content: '';
  position: absolute;
  bottom: 0;
}

.hex{
  z-index: 1;
  position: relative;
}

.hex:before{
  transform: rotate(60deg);
  z-index: 2;
}

.hex:after{
  transform: rotate(120deg);
  z-index: 3;
}
<div class="hex"></div>

JSFiddle