The box’s height is 130px, couldn’t figure out why?
.box {
line-height: 120px;
background-color: #cd0000;
color: #fff;
}
.content {
display: inline-block;
line-height: 20px;
}
<div class="box">
<div class="content">1<br />2<br />3<br />1</div>
</div>
Set .box’s height 120px
,it will be 120px
, but I don’t know why the 130px
happen?
2
Answers
Just use
height
instead ofline-height
for the.box
div.line-height
really doesn’t make sense here:Add
vertical-align:top
to the inline-block element. The extra space is due to the default baseline alignment.Your element is
80px
tall (4 x 20px of its line-height). Its baseline is the last line of text so by default that last line will be somehow at the center of the linebox.The last line is
20px
tall and placed at the center so10px
below and10px
above. To the10px
above you add the remaining60px
and you get70px
above the center.You
line-height
is equal to120px
so half of it is60px
. We are missing10px
to be able to place70px
so we increase the height up to130px
Add another line and the height will be
150px
Remove a line and the height will be
120px
and notice how the content no more touch the top because we have enough space to place50px
(10px + 2*20px
) inside60px
(120px/2
)