Search code examples
htmlcsscss-shapes

Making a hexagon with a mask and border in css3


I am trying to make a hexagon with border radius, border and something like a mask. Something like this : enter image description here
However, I am a bit stumped by the 'mask' and how I should go about making that shape. Would it be better to export this as svg than to make it with css3?

Currently I have this:

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

But even then this isnt very good since it looks a little off at the corners:

enter image description here


Solution

  • I would consider my previous answer where I will duplicate the shape to create the border:

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