Oblique or twisted border shape

前端 未结 3 1971
生来不讨喜
生来不讨喜 2020-12-10 10:07

I\'m interested if it\'s possible to create wrapped (or maybe better said twisted) border using CSS. Effect I wanted to achieve is in the picture.

3条回答
  •  天涯浪人
    2020-12-10 10:22

    Most easiest and neat solution would be to use svg to create the border.

    enter image description here

    #container {
      position: relative;
      width: 200px;
      height: 30px;
    }
    #content {
      text-transform: uppercase;
      position: absolute;
      width: 200px;
      height: 30px;
      top: 0;
      text-align: center;
      line-height: 30px;
    }
    lorem ipsum


    You could even spice it up with some curves using quadratic curves.

    enter image description here

    #container {
      position: relative;
      width: 200px;
      height: 30px;
      margin-bottom: 30px;
    }
    #content {
      text-transform: uppercase;
      position: absolute;
      width: 200px;
      height: 30px;
      top: 0;
      text-align: center;
      line-height: 30px;
    }
    lorem ipsum
    lorem ipsum
    lorem ipsum


    You could easily add a drop shadow effect.

    enter image description here

    #container {
      position: relative;
      width: 200px;
      height: 30px;
      margin-bottom: 30px;
    }
    #content {
      text-transform: uppercase;
      position: absolute;
      width: 200px;
      height: 30px;
      top: 0;
      text-align: center;
      line-height: 30px;
    }
    lorem ipsum
    lorem ipsum
    lorem ipsum


    Alternatively you could always use :after and :before :pseudo-elements.

    The width and height of the :after and :before :pseudo-elements were calculated using some basic trigonometry.

    enter image description here

    The opposite side is the width and height of the :after and :before :pseudo-elements. The one on the left is given top and right borders and the one on the right is given top and left borders. Then, the one on the left has been rotated 45deg and the one on the right has been rotated -45deg.

    div {
      position: relative;
      text-transform: uppercase;
      width: 200px;
      height: 30px;
      text-align: center;
      line-height: 27px;
      border-top: 3px solid black;
      border-bottom: 3px solid black;
      box-sizing: border-box;
    }
    div:after,
    div:before {
      position: absolute;
      content: '';
      width: 21.21px;
      height: 21.21px;
      border-top: 3px solid black;
      border-right: 3px solid black;
      transform: rotate(45deg);
      box-sizing: border-box;
      top: 1px;
      left: -9px;
    }
    div:after {
      border-right: 0;
      border-left: 3px solid black;
      left: 100%;
      margin-left: -10px;
      transform: rotate(-45deg);
    }
    lorem ipsum

提交回复
热议问题