How to use clip-path property for border in css

前端 未结 3 1383
暖寄归人
暖寄归人 2021-01-21 12:11

I have clip-part to make "cut corner" effect.

I would like to change background to white and use green border. Problem is, when I change ba

相关标签:
3条回答
  • 2021-01-21 12:37

    add some gradient to fill the missing spaces:

    .test {
      background: red;
      width: 100px;
      height: 100px;
      box-sizing:border-box;
      
      /* CORNERS */
        clip-path: polygon(10px 0%, calc(100% - 10px) 0%, 100% 10px, 100% calc(100% - 10px), calc(100% - 10px) 100%, 10px 100%, 0% calc(100% - 10px), 0% 10px);
    }
    
    .test:hover {
      --grad:transparent 49.5%,green 50%;
      background: 
        linear-gradient(to top right   ,var(--grad)) top    right,
        linear-gradient(to top left    ,var(--grad)) top    left,
        linear-gradient(to bottom right,var(--grad)) bottom right,
        linear-gradient(to bottom left ,var(--grad)) bottom left,
        white;
      background-size:13px 13px; /* 10px of the clip-path + 3px of border */
      background-repeat:no-repeat;
      background-origin:border-box;
      cursor: pointer;
      
      border: 3px solid green;
    }
    <div class='test'>
     </div>

    0 讨论(0)
  • 2021-01-21 12:37

    i'll make an answer of my comment for readability :

    For infos , if clip-path is used on a 2 level container, it is possible to add a shadow around the translucide edges with drop-shadow() filter .

    • clip-path applied on the child
    • drop-shadow() on the parent
    • without a blur , it looks like a border no matter the shape : https://jsfiddle.net/q9tdpvfg/

    jsfiddle demo in the snippet below:

    .test div {
      background: red;
      width: 100px;
      height: 100px;
      /* CORNERS */
      clip-path: polygon(10px 0%, calc(100% - 10px) 0%, 100% 10px, 100% calc(100% - 10px), calc(100% - 10px) 100%, 10px 100%, 0% calc(100% - 10px), 0% 10px);
    }
    
    .test:hover {
      filter: drop-shadow(0px 3px 0px green) drop-shadow(3px 0px 0px green) drop-shadow(-3px 0px 0px green) drop-shadow(0px -3px 0px green);
      /* made 1 for each sides */
    }
    
    .test:hover div {
      background: white;
      cursor: pointer;
    }
    
    .test {
      width: max-content;
    }
    
    .test div {
      /* demo purpose */
      display: flex;
      align-items: center;
      justify-content: center;
      flex-direction: column;
    }
    <div class='test'>
      <div>
        TEST
      </div>
    </div>

    0 讨论(0)
  • 2021-01-21 12:37

    As far as I know, it's not really possible to make a border around an element with a clip-path. This method uses inside and outside elements. Source: https://codepen.io/bennettfeely/pen/azJWWX/

    .outside {
      position: relative;
        width: 70vmin;
        height: 70vmin;
        background: red;
        -webkit-clip-path: polygon(50% 0%, 83% 12%, 100% 43%, 94% 78%, 68% 100%, 32% 100%, 6% 78%, 0% 43%, 17% 12%);
    clip-path: polygon(50% 0%, 83% 12%, 100% 43%, 94% 78%, 68% 100%, 32% 100%, 6% 78%, 0% 43%, 17% 12%);
    }
    
    .inside {
      position: absolute;
      top: 5px;
      left: 5px;
      right: 5px;
      bottom: 5px;
      background: red;
      -webkit-clip-path: polygon(50% 0%, 83% 12%, 100% 43%, 94% 78%, 68% 100%, 32% 100%, 6% 78%, 0% 43%, 17% 12%);
      clip-path: polygon(50% 0%, 83% 12%, 100% 43%, 94% 78%, 68% 100%, 32% 100%, 6% 78%, 0% 43%, 17% 12%);
    }
    
    .inside:hover {
      background: white;
      transition: all .2s;
    }
    
    .outside:hover {
      background: green;
      transition: all .2s;
    }
    
    /* Center the demo */
    html, body { height: 100%; }
    body {
        display: flex;
        justify-content: center;
        align-items: center;
      background: papayawhip;
    }
    <div class="outside">
      <div class="inside"></div>
    </div>

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