I’m working on a web layout where I need to create a flexible column structure with varying line heights. The goal is to have one column with a higher line-height while ensuring that both paragraph texts in different columns start from the top/same line position.
In my layout I need to make sure I could have either 1, 2 or 3 columns, title centred above its content.
I’ve tried to achieve this with the following HTML and CSS:
As you can see the text on the right in yellow does not start from exactly the same line as the text on the left.
<html>
<head>
<style>
.columnA {
color: white;
margin-right: 15px;
}
.columnB {
color: yellow;
align-self: start;
margin-bottom: 20px;
vertical-align: top;
}
.comment.title {
display: grid;
color: yellow;
text-align: center;
margin-bottom: 20px;
grid-template-columns: repeat(auto-fit, minmax(0, 1fr));
}
.comment.columnA {
color: green;
line-height: 1.4;
}
.comment.columnB {
color: yellow;
line-height: 2.4;
}
.content {
display: grid;
margin-bottom: 20px;
align-content: start;
grid-template-columns: repeat(auto-fit, minmax(0, 1fr));
}
.black-background {
background-color: black;
}
</style>
</head>
<body class="black-background">
<div class="comment">
<div class="comment title">
<p class="columnA">TitleA</p>
<p class="columnB">TitleB</p>
</div>
<div class="content">
<p class="comment columnA">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
</p>
<p class="comment columnB">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
</p>
</div>
</div>
</body>
</html>
2
Answers
There’s a workaround to utilize
lh
unit. By setting both of the paragraphs’margin-top
to-0.5lh
to make both paragraph aligned.But because the paragraph are shifted upwards so you might want to compensate their vertical position with the
margin-bottom
of previous element or using a calculatedmargin-top
such as:Be aware that
lh
unit hasn’t been widely supported yet. If you are targeting older browsers, you may also achieve the same effect with CSS variables:Set
.content{ align-items: baseline; }
and remove.columnB { align-self: start; }
: