cssborder

How to add a border to a jagged border container


Hello I am having trouble trying to come up with a way of adding a border of 1px to a container with a jagged border such as:

https://codepen.io/swizenfeld/pen/ZyBybW

body {
  background: #f4f4f4;
}
.edge {
  width: 100%;
  height: 400px;
  background: #fff;
  margin-top: 30px;
}
.edge:before {
    content: " ";
    display: block;
    position: relative;
    width: 100%;
    top:-30px;
    height:30px;
    background: linear-gradient(135deg, transparent 75%, white 76%) 0 50%,
                linear-gradient(-135deg, transparent 75%, white 76%) 0 50%;
    background-repeat: repeat-x;
    background-size: 30px 30px, 30px 30px;
}
<div class="edge"></div>

Any ideas?


Solution

  • You need to add more linear-gradient() to show jagged border

    body {
      background: #f4f4f4;
    }
    .edge {
      width: 100%;
      height: 400px;
      background: #fff;
      margin-top: 30px;
    }
    .edge:before {
        content: " ";
        display: block;
        position: relative;
        width: 100%;
        top:-30px;
        height:30px;
        background: linear-gradient(135deg, transparent 75%, white 76%) 0 50%, linear-gradient(-135deg, transparent 75%, white 76%) 0 50%, linear-gradient(45deg, red 30%, transparent 0%), linear-gradient(-45deg, red 30%, transparent 0%);
        background-repeat: repeat-x;
        background-size: 30px 30px, 30px 30px;
    }
    <div class="edge"></div>

    For border-left, -bottom, -right, try to play with below snippet and see the comment also given for css properties.

    body {
      background: #ccc;
    }
    .edge {
      width: 100%;
      height: 400px;
      background: white;
      margin-top: 30px;
      border-left:2px solid red;
      border-bottom:2px solid red;
      border-right:2px solid red;
      position:relative;    /*make it relative*/
    }
    .edge:after {
        content: " ";
        display: block;
        position:absolute; /*make it absolute*/
        width: 100%;
        top:-6px;  /* play with top and height too*/
        height:23px;
      /*background: linear-gradient(135deg, transparent 75%, white 76%) 0 50%, linear-gradient(-135deg, transparent 75%, white 76%) 0 50%, linear-gradient(45deg, red 30%, transparent 0%), linear-gradient(-45deg, red 30%, transparent 0%);*/
        background: linear-gradient(45deg,white 14px, red 16px, transparent 17px), linear-gradient(-45deg, white 14px, red 16px, #ccc 17px);
    
        background-repeat: repeat-x;
        background-size: 30px 30px, 30px 30px;
    
    }
    <div class="edge"></div>