Square brackets with CSS

后端 未结 5 1902
花落未央
花落未央 2021-02-14 22:36

I wish to create a [ and ] bracket just with CSS. Is there a way to specify the top and bottom border (Without slicing the image) so it looks like a br

5条回答
  •  清酒与你
    2021-02-14 23:11

    This is possible by usage of container's :before and :after pseudo elements. Sass code:

    $container-selector: h1;
    $bg-color: white;
    $bracket-color: orange;
    $bracket-width: 10px;
    $bracket-length: 30px;
    // just reset
    body {
      background-color: $bg-color;
    }
    
    // not really related
    $container-selector {
      padding: 0 30px;
    }
    
    $container-selector {
      position: relative;
      border: $bracket-width solid $bracket-color;
      &:before,
      &:after {
        content: "";
        display: block;
        position: absolute;
        top: -$bracket-width;
        right: $bracket-length - $bracket-width;
        left: $bracket-length - $bracket-width;
        height: $bracket-width;
        background-color: $bg-color;
      }
      &:after {
        top: initial;
        bottom: -$bracket-width;
      }
    }
    

    Working example is available here: https://jsfiddle.net/1uuodzdw/

提交回复
热议问题