Vertical Alignment of Inline Text
What you'll learn: How to control the vertical positioning of inline and inline-block elements using the vertical-align property.
The Core Idea
When you have inline elements (like <span>, <img>, or <a>) sitting next to each other on the same line, they don't always line up the way you'd expect. The vertical-align property lets you control how these elements align vertically relative to their surrounding text line.
Think of it like arranging items on a shelf: by default, everything sits on the "baseline" (imagine the bottom line of handwritten text). But sometimes you want to shift things up, down, or align them differently.
How It Works
The vertical-align property only affects inline and inline-block elements—it has no effect on block-level elements.
Common values include:
baseline(default) – aligns with the parent's baselinetop– aligns with the top of the linemiddle– centers vertically within the linebottom– aligns with the bottom of the linetext-top– aligns with the top of the parent's fonttext-bottom– aligns with the bottom of the parent's fontsub– lowers text for subscriptsuper– raises text for superscript- Length values like
5pxor-3pxfor precise adjustments
img {
vertical-align: middle; /* centers image with text line */
}
.superscript {
vertical-align: super; /* raises text */
font-size: 0.8em;
}
Key Takeaway: Use vertical-align to control how inline and inline-block elements position themselves vertically within their text line—it's essential for aligning images with text or creating subscripts and superscripts.