Make hexagon shape with border, rounded corners and transparent background

后端 未结 1 1856
鱼传尺愫
鱼传尺愫 2021-01-07 11:15

I want to make a hexagonal shape with border, rounded corners and transparent background in CSS3 like in this image:

I can\'t make this with rounded corners

相关标签:
1条回答
  • 2021-01-07 11:44

    Hexagon with rounded corners are complex shapes to create and I usually recommend using SVG for creating them. The need for a transparent background makes it even more better suited for SVG. With SVG you can get better control over the shape, its curves etc and you don't have to add a lot of extra (unnecessary) elements to your markup also.

    All that is needed for creating this shape with SVG is to use a single path element along with a few L (line) and A (arc) commands. The L (line) command basically draws a line from point 1 to point 2 whereas the A (arc) command draws an arc of the specified radius (the first two values immediately following the A command).

    You can read more about the SVG path element and its commands in this MDN tutorial.

    svg {
      height: 200px;
      width: 240px;
    }
    path {
      stroke: #777;
      fill: none;
    }
    
    body {
      background: black;
    }
    <svg viewBox='0 0 120 100'>
      <path d='M38,2 
               L82,2 
               A12,12 0 0,1 94,10 
               L112,44 
               A12,12 0 0,1 112,56
               L94,90       
               A12,12 0 0,1 82,98
               L38,98
               A12,12 0 0,1 26,90
               L8,56
               A12,12 0 0,1 8,44
               L26,10
               A12,12 0 0,1 38,2' />
    </svg>

    If you still want to use CSS, you could follow the approach used by jbutler483 in this fiddle of his. (I have appended the code from that fiddle also into this answer to avoid link rot problems)

    .roundHex {
      position: relative;
      margin: 0 auto;
      background: transparent;
      border-radius: 10px;
      height: 300px;
      width: 180px;
      box-sizing: border-box;
      transition: all 1s;
      border: 10px solid transparent;
      border-top-color: black;
      border-bottom-color: black;
    }
    .roundHex:before,
    .roundHex:after {
      content: "";
      border: inherit;
      position: absolute;
      top: -10px;
      left: -10px;
      background: inherit;
      border-radius: inherit;
      height: 100%;
      width: 100%;
    }
    .roundHex:before {
      transform: rotate(60deg);
    }
    .roundHex:after {
      transform: rotate(-60deg);
    }
    <div class="roundHex"></div>

    0 讨论(0)
提交回复
热议问题