I have a grid with two columns. The second column has a larger font size:
.container {
display: grid;
grid-template-columns: max-content 1fr;
gap: 0.5rem;
}
.text1 {
color: #fff;
background-color: #000;
}
.text2 {
font-size: 3.75rem;
}
<div class="container">
<div class="text1">text1</div>
<div class="text2">text2</div>
</div>
I wanted to align the texts to the baseline so I added align-items: baseline;
to .container
and this happened:
The texts are aligned correctly but the black background doesn’t take the whole grid block. How can I keep the text1 container stretched while aligning its content to baseline?
I’ve tried these but none of them worked:
- adding
height: 100%
to.text1
- adding
align-self: stretch
andalign-content: baseline
to.text1
- a wrapper
div
for the background and aspan
for the content and settingalign-self: baseline
for thespan
.
3
Answers
If you don’t want to use a table, this answer might point you in the correct direction.
If you don’t mind using
display: table
, you can use thevertical-align
property, as shown here.You can try to align text item by flex:
I used
align-items:self-end
,display:flex
andline-height
attributes to align the texts same level.