Incomplete borders around div

后端 未结 5 1545
日久生厌
日久生厌 2021-02-12 09:09

I am looking for a way to create an incomplete square with borders with some text and a background with pure css. Here is what I am trying to achieve:

<

5条回答
  •  无人共我
    2021-02-12 09:27

    This approach allows you to:

    • add any content and the borders will adapt around it regardless of height or width of the content
    • support transparent background and can be displayed over an image or non plain colors
    • doesn't add any unsemantic elements

    It relies on 2 absolutely positioned pseudo elements and one div. The spacing between the content and the borders is controlled by the padding on the div :

    div{
      position:relative;
      display:inline-block;
      padding:50px 100px;
      border-left:1px solid #000;
      text-align:center;
    }
    div:before, div:after{
      content:'';
      position:absolute;
      right:50%; left:0;
      height:50px;
      border-right:1px solid #000;
    }
    div:before{
      top:0;
      border-top:1px solid #000;
    }
    div:after{
      bottom:0;
      border-bottom:1px solid #000;
    }
    body{background:url('http://i.imgur.com/3IXm5qm.jpg');background-size:cover;}

    This is a very long title on
    2 lines

    Some text

提交回复
热议问题