Hexagon shape with a border/outline

前端 未结 5 1488
余生分开走
余生分开走 2020-11-27 07:53

I know it\'s possible to create a hexagon shape using the following code:

.hex:before {
    content: \" \";
    width: 0; height: 0;
    border-bottom: 30px          


        
5条回答
  •  不知归路
    2020-11-27 08:11

    Here is another method to create hexagons with border (or outline) using the clip-path feature. In this method, we use a container element and a pseudo-element which has smaller dimensions (both height and width) than the container. When the same clip-path is applied to both the elements, the background-color of the container element is seen behind the pseudo-element only at the edges and makes it look like a border/outline to the shape.

    Advantages:

    • Hexagons can also have gradients or images (basically non-solid color) as background.
    • Shape is responsive and can automatically adapt to any change in the container dimensions.

    .hexagon {
      position: relative;
      height: 150px;
      width: 150px;
      background: black;
    }
    .hexagon:before, .double:after {
      position: absolute;
      content: '';
    }
    .hexagon:before {
      top: 4px;  /* border width */
      left: 4px;  /* border width */
      height: calc(100% - 8px);  /* 100% - (2 * border width) */
      width: calc(100% - 8px);  /* 100% - (2 * border width) */
      background: #6c6;
    }
    .hexagon, .hexagon:before, .double:after {
      -webkit-clip-path: polygon(50% 0%, 100% 25%, 100% 75%, 50% 100%, 0% 75%, 0% 25%);
      clip-path: polygon(50% 0%, 100% 25%, 100% 75%, 50% 100%, 0% 75%, 0% 25%);
    }
    .image:before {
      background: url(http://lorempixel.com/150/150/nature/1);
    }
    .double:after {
      top: 8px;
      left: 8px;
      height: calc(100% - 16px);
      width: calc(100% - 16px);
      background: black;
    }
    
    /* Just for demo */
    
    .hexagon {
      display: inline-block;
      margin: 20px;
    }

    The major disadvantage is the poor browser support at present. CSS clip-path do not work in IE and FF currently. The problem with FF can be fixed by using a SVG (inline or external) for the clip-path (like in the below snippet):

    .hexagon {
      position: relative;
      height: 150px;
      width: 150px;
      background: black;
    }
    .hexagon:before, .double:after {
      position: absolute;
      content: '';
    }
    .hexagon, .hexagon:before, .double:after {
      -webkit-clip-path: url(#hexagon-clip);
      clip-path: url(#hexagon-clip);
    }
    .hexagon:before {
      top: 4px;  /* border width */
      left: 4px;  /* border width */
      height: calc(100% - 8px);  /* 100% - (2 * border width) */
      width: calc(100% - 8px);  /* 100% - (2 * border width) */
      background: #6c6;
    }
    .image:before {
      background: url(http://lorempixel.com/200/200);
    }
    .double:after {
      top: 8px;
      left: 8px;
      height: calc(100% - 16px);
      width: calc(100% - 16px);
      background: black;
    }
    
    /* Just for demo */
    
    .hexagon {
      display: inline-block;
      margin: 20px;
    }
    
      
        
          
        
      
    
    

提交回复
热议问题