How to make a border overlay child div?

后端 未结 1 491
无人及你
无人及你 2020-11-27 23:31

I have to make a border overlay it\'s content to match the looking of this image:

And I got the big picture of it:

And I need to make that bord

相关标签:
1条回答
  • 2020-11-27 23:49

    You can consider the use of pseudo element to create the border and avoid extra markup. You can also easily control its position/size:

    body {
      background: grey;
    }
    
    .button {
      background: #94c120;
      width: 200px;
      height: 50px;
      margin: 50px;
      position: relative;
    }
    
    .button:before {
      content: "";
      position: absolute;
      top: -15px;
      left: -15px;
      width: 100%;
      height: 100%;
      border: 5px solid #fff;
      box-sizing: border-box;
    }
    <div class="button">
      some text
    </div>

    Here is also another idea using linear-gradient and multiple background:

    body {
     background:grey;
    }
    .button {
      width: 200px;
      height: 80px;
      margin: 50px;
      padding:20px 0 0 20px;
      box-sizing:border-box;
      background: 
        linear-gradient(to right,#fff 5px,transparent 5px calc(100% - 5px),#fff calc(100% - 5px)) 0 0/ calc(100% - 10px) calc(100% - 10px),
        linear-gradient(to bottom,#fff 5px,transparent 5px calc(100% - 5px),#fff calc(100% - 5px)) 0 0/ calc(100% - 10px) calc(100% - 10px),
        linear-gradient(#94c120,#94c120) 15px 15px;
      background-repeat:no-repeat;
    }
    <div class="button">
      some text
    </div>

    Another syntax with gradient:

    body {
     background:grey;
    }
    .button {
      width: 200px;
      height: 80px;
      margin: 50px;
      padding:20px 0 0 20px;
      box-sizing:border-box;
      background: 
        linear-gradient(#fff,#fff) left -10px top     0  /100% 5px,
        linear-gradient(#fff,#fff) top  -10px left    0  /5px 100%,
        linear-gradient(#fff,#fff) left -10px bottom 10px/100% 5px,
        linear-gradient(#fff,#fff) top  -10px right  10px/5px 100%,
        linear-gradient(#94c120,#94c120) 20px 20px; 
      background-repeat:no-repeat;
    }
    <div class="button">
      some text
    </div>

    Another idea using background and box-shadow:

    body {
     background:grey;
    }
    .button {
      width: 200px;
      height: 80px;
      margin: 50px;
      padding:15px 0 0 15px;
      box-sizing:border-box;
      background: #94c120 content-box;
      border:5px solid #fff;
      box-shadow: 20px 20px 0px #94c120; /* value of padding + border*/
    }
    <div class="button">
      some text
    </div>

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