日本語

Aligning Icons with Text Using CSS vertical-align

​​CSS​Flexbox,Fonts,HTML,Playwright,SVG

When you place an SVG icon next to text in a button, menu, or link, the icon appears to float above the text if you don't set anything. For a 1em icon, vertical-align: -0.125em centered it on the kanji. With vertical-align: middle, the icon sat slightly too low.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and headless Chromium 153. The main font was Noto Sans JP (@fontsource/noto-sans-jp 5.3.1), which I loaded on the page. I also measured with IPAGothic and Inter, which were already installed in the test environment, for comparison. The icon was a square SVG. I took screenshots and found each center from the top and bottom edges of the icon's colored pixels and the dark text pixels. I did not test Firefox or Safari, or fonts on macOS or Windows.

Without any setting, the bottom of the icon aligns with the baseline

An <svg> or <img> written in HTML is placed in the line like text. The initial value of vertical-align is baseline. According to MDN, for an element with no baseline, the bottom margin edge is aligned to the parent's baseline. In the test environment, the bottom of the SVG was at the same position as the baseline.

html
<a href="/settings">
  <svg class="icon" viewBox="0 0 16 16" aria-hidden="true">…</svg>設定
</a>
css
.icon {
  width: 1em;
  height: 1em;
}

The baseline is the line at the bottom of the lowercase "x" in Latin text. A 1em icon sitting on the baseline has its center 0.5em above the baseline. In Noto Sans JP, the center of the kanji was about 0.38em above the baseline. The kanji extend a little below the baseline and their tops are lower than the icon's top, so the icon looks shifted upward relative to the kanji. At a font size of 40px, the icon's center was 5px above the center of the Settings label.

Comparing the positions for each approach

I used Noto Sans JP at a font size of 40px with line-height: 1.6. I placed a 1em icon next to the Settings label and measured how far the icon's center was from the text's center. A negative value means the icon is higher.

Approach Center offset (at 40px)
Not set (baseline) -5px
vertical-align: middle +4px
vertical-align: -0.125em 0px
vertical-align: -0.15em +1px
vertical-align: text-bottom +7px
vertical-align: text-top -11px
display: inline-flex with align-items: center -2px

When I measured with the English word "Save", every approach gave the same offsets. This is because in Noto Sans JP, the center of the whole word "Save" (0.369em above the baseline) and the center of the kanji (0.381em above) are at almost the same height.

A comparison of four approaches stacked vertically. Each row shows a light blue round icon next to the text "Settings" (a Japanese label). A pink dotted line marks the center of the text. In the unset row, the icon is above the dotted line; in the middle row, it is below; in the -0.125em row, the icon's center overlaps the dotted line; in the inline-flex row, it is slightly above the dotted line. The labels in the image are in Japanese.
Four approaches at a font size of 56px. The pink dotted line marks the center of the Settings label

text-top and text-bottom align the top or bottom edge of the icon to the top or bottom of the text's font box. Neither is meant to center the icon, and as the table shows, both were far off.

middle uses half the x-height, so the icon sits a bit low

MDN describes middle as aligning "the vertical midpoint of the box with the baseline of the parent box plus half the x-height of the parent." The x-height is the height of the lowercase "x". In Noto Sans JP, the center of "x" is 0.269em above the baseline, which is lower than the kanji center (0.381em above). As a result, middle placed the icon lower than the kanji center. At 40px, the offset was 4px, about 0.1em relative to the font size.

If you use a length to lower the icon, write it in em

When you give vertical-align a length, as MDN describes, the element's baseline is placed that distance above the parent's baseline. A negative value lowers it. For an SVG, which has no baseline, the bottom edge is placed at that position.

css
.icon {
  width: 1em;
  height: 1em;
  vertical-align: -0.125em;
}

If you write the value in em instead of px, the icon shifts by the same proportion when the font size changes. I compared -0.125em and -2px at different font sizes:

Font size Center offset with -0.125em Center offset with -2px
14px +0.3px +0.3px
16px +0.3px +0.3px
20px +0.5px -0.5px
24px 0px -1.0px
32px +0.3px -1.8px

-0.125em stayed within 0.5px at every size. -2px lines up around 16px, but as the text gets larger, as in headings, the icon rises higher and higher relative to the text.

When the icon is not 1em

The value -0.125em is for a 1em icon. In Noto Sans JP, the kanji center was about 0.38em above the baseline. To put the icon's center there, place its bottom edge half the icon's height below that point. If you use the round value 0.375em (which gives -0.125em for a 1em icon), the value is "0.375em − icon height ÷ 2".

css
.icon-large {
  width: 1.5em;
  height: 1.5em;
  vertical-align: -0.375em; /* 0.375em − 1.5em ÷ 2 */
}

I placed a 1.5em icon on a line with 16px text and line-height: 1.6. With -0.375em, the center offset was 0.25px. The line height stayed at 25.6px, the same as a text-only line. Without a setting, the line height grew to 30.6px, and with middle, it grew to 26.7px.

The right value depends a little on the font

How far the kanji and Latin letters are drawn above the baseline depends on the font. At 40px, I found the value that centers a 1em icon on the text, for each font.

Font Text Text center height Value that centers
Noto Sans JP Settings 0.381em -0.119em
Noto Sans JP Save 0.369em -0.131em
IPAGothic Settings 0.375em -0.125em
IPAGothic Save 0.406em -0.094em
Inter Save 0.362em -0.138em

In the range I measured, the values fell between -0.094em and -0.138em. The largest difference from -0.125em was 0.031em, which is about 0.5px at 16px. If your headings use a fixed font at a large size, measuring the center with that font and choosing the value will align the icon more precisely.

Inside a button, inline-flex with align-items: center also works

For an icon and text that form one unit, such as a button or tag, you can lay them out with flexbox.

css
.button {
  display: inline-flex;
  align-items: center;
  gap: 0.25em;
}
.button svg {
  width: 1em;
  height: 1em;
}
html
<button class="button" type="button">
  <svg viewBox="0 0 16 16" aria-hidden="true">…</svg>
  <span>設定</span>
</button>

align-items: center places each item's margin box at the center of the cross axis (vertically, for items in a row). This method centers the icon in the text's box, so you do not need to work out a vertical-align value. Even with a 1.5em icon, the line height matched a text-only line.

However, what gets centered is the text's box, not the visual center of the kanji. In Noto Sans JP, the icon sat about 0.04em above the kanji center. That is 1px at 24px and about 2px at 40px. At 24px and 40px, this offset did not change when I changed line-height from 1.6 to 1, or the icon size from 1em to 1.5em. At 16px, the offset ranged from +0.25px to -0.75px, under 1px. For buttons at body-text size, this method is good enough.

If you place an icon in the middle of a sentence, adding vertical-align to the icon is enough. To use inline-flex, you need to wrap the icon and text in one element, which adds markup.

​Posted by 86