Basically, the only problem left for me is the automatically adjusting height of the line beside the supposed title. I contained the vertical line and the title with a div flex, though I am not certain if the structure itself is even correct or the ideal solution to my case.
Here is what I did:
.post-title {
font-weight: 700;
}
.vertical-line {
height: /* (Main Issue Here) */;
width: 10px;
background-color: #007bff;
margin-right: 1rem;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<div class="post-title-container d-flex align-items-center">
<div class="vertical-line"></div>
<h1 class="post-title">
Lorem ipsum dolor sit amet consectetur adipisicing
</h1>
</div>
3
Answers
Shout out to @TemaniAfif, we've now got it figured out.
Edit: @TayneLeite solution worked as well!
align-items-center
in the<div class="post-title-container d-flex align-items-center">
.So just removing that solves the problem. Also, it is better to use padding rather than width when defining the thickness of the
<div class="vertical-line"></div>
, so that its size doesn't readjusts when the text is longer or shorter, or when resized/zoomed in.Here's my revised code:
Have you tried this way?
Solution 1
As per TemaniAfif’s comments:
Remove BS class
.align-items-center
. Ifalign-items: center
is applied to the flex container🞴, all flex items✢ will have the height of their content. Since.vertical-line
is empty it’s height would be 0. Without anyalign-items
defined it isstretch
by default which makes flex items’ heights at 100%.Add padding to
.vertical-line
. Since.vertical-line
is a flex item it’s width will adjust according to the width of the flex container, any space within said flex container, and the other flex item widths (see this article). Padding of a flex item is not affected.In addition, since
.vertical-line
has no content you can apply padding as shorthand with half the width:padding: 5px
and it will always be 10px wide and a minimum height of 10px (but it’s current height will be of it’s flex container).🞴any mention of "flex container" refers to
.post-title-container
which hasflex-direction: row
✢any mention of "flex items" refers to
.vertical-line
and.post-title
Solution 2
As an alternative to using an extra markup, try this:
div.vertical-line
. Visually, it’s really just a border.border-left: border-left: 10px solid #007bff;
to.post-title-container
. This is a real border which is just a style (as it should be)..ps-3
to.post-title-container
..ps-3
ispadding-left: 1rem
which is the spacing equivalent tomargin-right: 1rem
on.vertical-line
.The example below features both solutions in a minimal Bootstrap 5.3.3 layout. Aesthetically, they are identical, but semantically Solution 2 is better. Additional details are commented in the example. View in Full page mode.
Example