Add Horizontal Lines Between Elements

前端 未结 2 1155
执念已碎
执念已碎 2021-02-14 08:14

I\'m trying to add a horizontal line between two elements, like LinkedIn: I can\'t get the line on the left of the image to stop at the left side count. I\'ve been Googling for

2条回答
  •  深忆病人
    2021-02-14 08:28

    Method 1: Flexbox

    .divided {
      display: flex;
      align-items: center;
    }
    
    .divider {
      flex-grow: 1;
      border-bottom: 1px solid black;
      margin: 5px
    }

    Content 1 Content 2

    Because the parent is flexbox, we can just tell the .divider element to fill all available space, by setting flex-grow: 1;.


    Method 2: Solid background color

    .divided {
      background: linear-gradient(transparent 45%, black 45%, black 55%, transparent 55%);
    }
    
    .divided span {
      background: white;
      padding: 0 5px;
    }
    
    .divided span:last-of-type {
      float: right;
    }

    Content 1 Content 2

    We are giving .divided a linear-gradient as a background, to make a display that looks like a line of the color you want, then settings the 's to have the same color background, so they cover the line.

    You can accomplish the same thing quite a few different ways, such as using a pseudo element as the line, then giving the 's a position and z-index.

    What are the drawbacks of this method? It only works with a background that's a solid color. Throw an image, gradient, or anything that the content's background is gonna cover, and you'll end up with a display like so:

提交回复
热议问题