Scroll to the end to get the fix if you want to avoid the boring and long explanation
You need to consider 3 things. First, line-height
applied to block element define the minimum height of the line box.
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 ref
In your case the minimum height is equal to 22pt = 1.2*20pt
. Even if the p
element is empty you will at least have this height:
.wrapper {
width: 175px;
border: 1px solid black;
font-size: 20pt;
}
.wrapper p {
margin: 0.5em 0 0 0;
background-color: orange;
line-height: 1.2;
word-wrap: break-word;
}
.wrapper p span {
display: inline-block;
}
<div class="wrapper">
<p style="text-align:center"><span></span></p>
</div>
Second, you should note that an inline-block
element is placed in one line box even if the text wrap inside it. We may have more than one line boxes inside it but it doesn't matter because your span elements will then take one line box from p
Inline-level boxes that are not inline boxes (such as replaced inline-level elements, inline-block elements, and inline-table elements) are called atomic inline-level boxes because they participate in their inline formatting context as a single opaque box. ref
The last thing you need to consider is vertical alignment which is by default baseline and define how our elements are placed inside the line box.
Now we have everything to understand what is happening:
In the first case, you have a span with one line of text and a font-size
equal to 0.5em
so the height will be around half the height of the line box of its container p
. That element will be aligned at the baseline of the p
element. Basically, we have an element having its height less than the line box so there is some free space to align it.
For the other cases, you have an element with 2 lines inside it, the same font-size
but a height almost equal to the height of the line box where they are. If you look closely you will notice that the height of the p
is even bigger because the baseline alignment will leave some space at the bottom.
So for the first case, we put a small element in a big space aligned at the baseline and for the other cases, an element in a space that fit it, also at the baseline.
Here is a better illustration to see what is happening. Notice how all the 3 elements are aligned the same at the bottom (the basline) and how the last 2 elements are slightly bigger.
.wrapper {
border: 1px solid black;
font-size: 20pt;
}
.wrapper p {
width: 175px;
margin:0;
background-color: orange;
line-height: 1.2;
word-wrap: break-word;
display:inline-block;
}
.wrapper p span {
display: inline-block;
outline:1px solid red;
}
<div class="wrapper">
<p style="text-align:center"><span style="font-size:0.5em"><span style="color:#00bcd4;">AAAAA & AAAAA</span></span></p>
<p style="text-align:center"><span style="font-size:0.5em">BBBBB · BBBBB · BBBBB BBBBB BBBBB</span></p>
<p style="text-align:center"><span style="font-size:0.5em"><span style="color:#00bcd4;">CCCCCCCC & CCCCCCCCCC</span></span></p>
</div>
Here is another illustration to see the effect of vertical alignment:
.wrapper {
border: 1px solid black;
font-size: 20pt;
}
.wrapper p {
width: 70px;
margin:0;
background-color: orange;
line-height: 1.2;
word-wrap: break-word;
display:inline-block;
vertical-align:bottom;
}
.wrapper p span {
display: inline-block;
outline:1px solid red;
}
<div class="wrapper">
<p style="text-align:center"><span style="font-size:0.5em"><span style="color:#00bcd4;">AAAAA </span></span></p>
<p style="text-align:center"><span style="font-size:0.5em;vertical-align:top"><span style="color:#00bcd4;">AAAAA </span></span></p>
<p style="text-align:center"><span style="font-size:0.5em;vertical-align:bottom"><span style="color:#00bcd4;">AAAAA </span></span></p>
<p style="text-align:center"><span style="font-size:0.5em;vertical-align:top">BBBBB BBBBB BBBBB</span></p>
<p style="text-align:center"><span style="font-size:0.5em;vertical-align:bottom">BBBBB BBBBB BBBBB</span></p>
<p style="text-align:center"><span style="font-size:0.5em">BBBBB BBBBB BBBBB</span></p>
<p style="text-align:center"><span style="font-size:0.5em"><span style="color:#00bcd4;">CCCCC CCCCC</span></span></p>
<p style="text-align:center"><span style="font-size:0.5em;vertical-align:bottom"><span style="color:#00bcd4;">CCCCC CCCCC</span></span></p>
</div>
Notice how vertical-align
different from baseline will have no effect on the element in the second case since the element is bigger that the minimum height defined so its height will define the height of the line box and there is no free space to align it unlike in the first case.
If you remove inline-block
from the span element, you will remove the second part I have explained and each line inside the same will be placed in a different linebox inside the p
.
.wrapper {
border: 1px solid black;
font-size: 20pt;
}
.wrapper p {
width: 70px;
margin:0;
background-color: orange;
line-height: 1.2;
word-wrap: break-word;
display:inline-block;
vertical-align:bottom;
}
.wrapper p span {
outline:1px solid red;
}
<div class="wrapper">
<p style="text-align:center"><span style="font-size:0.5em"><span style="color:#00bcd4;">AAAAA </span></span></p>
<p style="text-align:center"><span style="font-size:0.5em;vertical-align:top"><span style="color:#00bcd4;">AAAAA </span></span></p>
<p style="text-align:center"><span style="font-size:0.5em;vertical-align:bottom"><span style="color:#00bcd4;">AAAAA </span></span></p>
<p style="text-align:center"><span style="font-size:0.5em;vertical-align:top">BBBBB BBBBB BBBBB</span></p>
<p style="text-align:center"><span style="font-size:0.5em;vertical-align:bottom">BBBBB BBBBB BBBBB</span></p>
<p style="text-align:center"><span style="font-size:0.5em">BBBBB BBBBB BBBBB</span></p>
<p style="text-align:center"><span style="font-size:0.5em"><span style="color:#00bcd4;">CCCCC CCCCC</span></span></p>
<p style="text-align:center"><span style="font-size:0.5em;vertical-align:bottom"><span style="color:#00bcd4;">CCCCC CCCCC</span></span></p>
</div>
If we consider the second case, we had an inline-block
element with 3 line boxes inside it placed inside one line box of the p
element. Now, we have an inline element placed inside 3 line boxes of the p
elements.
To avoid this, simply make sure that the line-height
is equal to only one line of text. Eiher use a small line-height
(even 0 will do the job) and make sure to redefine it for span
because line-height
is inherited.
.wrapper {
width: 175px;
border: 1px solid black;
font-size: 20pt;
}
.wrapper p {
margin:0.5em 0 0;
background-color: orange;
line-height: 0;
word-wrap: break-word;
}
.wrapper p span {
display: inline-block;
line-height: 1.2;
}
<div class="wrapper">
<p style="text-align:center"><span style="font-size:0.5em"><span style="color:#00bcd4;">AAAAA & AAAAA</span></span></p>
<p style="text-align:center"><span style="font-size:0.5em">BBBBB · BBBBB · BBBBB BBBBB BBBBB</span></p>
<p style="text-align:center"><span style="font-size:0.5em"><span style="color:#00bcd4;">CCCCCCCC & CCCCCCCCCC</span></span></p>
</div>
Or define the font-size
you are using for span inside p
.wrapper {
width: 175px;
border: 1px solid black;
font-size: 20pt;
}
.wrapper p {
margin:0.5em 0 0;
background-color: orange;
line-height: 1.2;
font-size:0.5em;
word-wrap: break-word;
}
.wrapper p span {
display: inline-block;
}
<div class="wrapper">
<p style="text-align:center"><span ><span style="color:#00bcd4;">AAAAA & AAAAA</span></span></p>
<p style="text-align:center"><span >BBBBB · BBBBB · BBBBB BBBBB BBBBB</span></p>
<p style="text-align:center"><span><span style="color:#00bcd4;">CCCCCCCC & CCCCCCCCCC</span></span></p>
</div>
Related
questions for more detail and examples: Line height issue with inline-block elements