If we check the documentation we will read this:
The vertical-align property can be used in two contexts:
To vertically align an inline element's box inside its containing line
box. For example, it could be used to vertically position an <img>
in
a line of text:
But what is a line box?
You can refer to this part of the specification to get better idea about line boxes. It may be complex at the start but to make it easy the height of the line box is defined by the inline element.
In your case, you have only one inline element that define the line box and its height is equal to the height of the line box thus there is nothing to align as all the alignment will be equivalent.
.box {
height:100px;
border:1px solid red;
}
span {
background:pink;
/*All these values will define the line box*/
line-height:25px;
font-size:20px;
}
<div class="box">
<span>text</span>
</div>
<div class="box">
<span style="vertical-align:middle;">text</span>
</div>
<div class="box">
<span style="vertical-align:top;">text</span>
</div>
<div class="box">
<span style="vertical-align:bottom;">text</span>
</div>
<div class="box">
<span style="vertical-align:text-top;">text</span>
</div>
<div class="box">
<span style="vertical-align:text-bottom;">text</span>
</div>
Now, suppose you have two inlines elements with different properties (font-size
, line-height
, font-family
, etc). In this case, the line box will be calulcated considering both elements like described in the specification and vertical-align
will have an effect in this case because:
When the height of a box B is less than the height of the line box
containing it, the vertical alignment of B within the line box is
determined by the 'vertical-align' property.
.box {
height:100px;
border:1px solid red;
}
span {
background:pink;
}
span:last-child {
font-size:2em;
}
<div class="box">
<span>text</span><span>big text</span>
</div>
<div class="box">
<span style="vertical-align:middle;">text</span><span >big text</span>
</div>
<div class="box">
<span style="vertical-align:top;">text</span><span>big text</span>
</div>
<div class="box">
<span style="vertical-align:bottom;">text</span><span >big text</span>
</div>
<div class="box">
<span style="vertical-align:text-top;">text</span><span >big text</span>
</div>
<div class="box">
<span style="vertical-align:text-bottom;">text</span><span >big text</span>
</div>
As you can see the line box is defined by the bigger text and now you can align the small text with within it.
This is a very simplified explanation using simple wording, refer to the specification for a more accurate and complete explanation.