Search code examples
htmlcsssvgcss-shapes

CSS3 Hexagon with dotted border


I could achieve a hexagon with solid border as below:

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

However, I would like to create a hexagon with a dotted border as seen in the image below:

Hexagon with a dotted border


Solution

  • Html Code:

    <div class="hexagon"><span></span></div>
    

    Css code :

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

    output :

    enter image description here

    Apply color as you nedd.