As other explained in the comments, the issue is that you have a fixed height of 20px
and you set a line-height
that get inherited from the parent to 80px
so the line-box height is bigger thus you are having an overflow.
If you change the line-height
of the inner span it will get fixed:
div {
height: 80px;
border: 1px solid green;
line-height: 80px
}
.inner-span {
height: 20px;
line-height: initial;
display: inline-block;
border: 1px solid red;
}
<div>
<span class="inner-span">123123</span>
</div>
Now why the border is positionned on the top?
It's because the default alignment is baseline and to define the baseline we consider the text.
Aligns the baseline of the element with the baseline of its parentref
If you change the vertical-align
to be bottom
, for example, you will see that the border will be on the bottom.
Aligns the bottom of the element and its descendants with the bottom
of the entire line.ref
div {
height: 80px;
border: 1px solid green;
line-height: 80px
}
.inner-span {
height: 20px;
display: inline-block;
vertical-align:bottom;
border: 1px solid red;
}
<div>
<span class="inner-span">123123</span>
</div>
If you add overflow:auto
will clearly understand the overflow issue and you will also change the baseline of the element to make it the bottom border:
div {
height: 80px;
border: 1px solid green;
line-height: 80px
}
.inner-span {
height: 20px;
display: inline-block;
overflow:auto;
border: 1px solid red;
}
<div>
<span class="inner-span">123123</span>
</div>
If you remove the fixed height you will also notice that the height of the element will get defined by the line-height
(the height of the line-box) and will logically be 80px
:
div {
height: 80px;
border: 1px solid green;
line-height: 80px
}
.inner-span {
display: inline-block;
border: 1px solid red;
}
<div>
<span class="inner-span">123123</span>
</div>