The line-height of the container should be size of the font-size, i.e. 16px
No it's not. You never set the line-height
of the parent so the default value will apply which is around 1.2
. It can be different in some case but for sure bigger than 1
in all the cases so bigger than the defined font-size
Set an explicit line-height
and you will get what you want:
.link {
background-color: red;
font-size:16px;
line-height:1; /* or any pixel value */
}
.link span {
background-color: royalblue;
font-size:16px;
}
<div class="link">
<span>textg</span>
</div>
No need to set the line-height
of the inline element because it will inherit it from the parent.
Also setting the line-height
to only the inline element will do nothing to its parent since you have two line-height. The one of the parent that define the minimum height of the line box and the one of the child that can only increase the line box height (beyond the minimum value) and never decrease it.
.link {
background-color: red;
font-size:16px;
line-height:16px;
margin:10px;
}
.link span {
background-color: royalblue;
font-size:16px;
}
<div class="link">
<span style="line-height:0;">I will do nothing !!</span>
</div>
<div class="link">
<span style="line-height:40px;">I will make the parent bigger</span>
</div>
On a block container element whose content is composed of inline-level elements, 'line-height' specifies the minimal height of line boxes within the element.
On a non-replaced inline element, 'line-height' specifies the height that is used in the calculation of the line box height. ref