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
.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;
.
.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: