Align Inline SVG with Text Using CSS vertical-align
Aligning inline SVG icons with adjacent text is a common front-end
challenge because browser defaults align inline elements to the
baseline, causing icons to look unnaturally raised. This guide explains
why this misalignment occurs and how to utilize the
vertical-align property alongside relative units
(em or ex) to achieve pixel-perfect, scalable
alignment without breaking text flow or relying on flexbox hacks.
Why Default Alignment Fails
By default, SVG elements are treated as inline content. Browsers place inline elements on the baseline of the text. Because text has descenders (like the tail of a “g” or “y”) and capital letter heights that sit above the baseline, placing the bottom edge of an SVG directly on the baseline causes the icon to appear too high relative to the surrounding words.
The Recommended CSS Solution
The most robust technique is to size the SVG using em
units and apply a negative relative offset using
vertical-align. This ensures the icon scales automatically
whenever the parent font size changes.
.icon {
display: inline-block;
width: 1em;
height: 1em;
vertical-align: -0.125em;
fill: currentColor;
}Choosing the Right
vertical-align Value
Depending on the icon set and typeface design, the exact offset may vary slightly:
Negative
emValues (-0.125emto-0.2em):
This is the industry-standard approach used by modern icon libraries. An offset between-0.125em(1/8th of the font size) and-0.15emshifts the icon down just enough to center its bounding box with the text’s cap height rather than the baseline.vertical-align: middle:
This value aligns the vertical midpoint of the SVG with the baseline of the parent box plus half the x-height of the font. While it improves alignment instantly, it can sometimes leave the icon slightly off-center compared to uppercase letters.vertical-align: text-bottom:
Aligns the bottom of the SVG with the bottom of the parent element’s font. This works well for square icons that need to sit flush with the lowest point of typical text characters.
Key Rules for Perfect Icon Alignment
- Set
fill: currentColor: This forces the SVG to inherit the text color automatically, keeping the visual hierarchy consistent. - Match Dimensions to Text: Sizing with
width: 1em; height: 1em;ensures the icon remains proportional to header tags (h1,h2), buttons, and body copy without requiring separate classes for different font sizes. - Avoid Hardcoded Pixels: Avoid using fixed pixel
values for
vertical-align(e.g.,-2px) on components that appear at multiple font sizes, as fixed offsets will look disproportionate on larger or smaller text.