Oblique or twisted border shape

前端 未结 3 1967
生来不讨喜
生来不讨喜 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:20

    Yes, you can do this purely in CSS by manipulating the :before and :after psuedo elements.

    The key advantages are that you can keep your HTML 'as is', and it maintains a strict seperation of concerns between content (html) and styling (CSS).

    body {
      text-align: center;
    }
    div {
      border: 2px solid;
      display: inline-block;
      position: relative;
      padding: 0 40px;
      margin: 20px;
      height: 30px;
      line-height: 30px;
      overflow: hidden;
      border-right: 0;
      border-left: 0;
    }
    div:after,
    div:before {
      border: 2px solid;
      height: 30px;
      width: 30px;
      content: '';
      display: block;
      position: absolute;
      top: -2px;
      transform: rotate(45deg);
    }
    div:after {
      right: -23px;
    }
    div:before {
      left: -23px;
    }
    Lorem Ipsum

提交回复
热议问题