修复 Android 浏览器中等宽字体对齐错误的问题
Fixing miss-aligned monospace text in Android browsers

原始链接: https://moritzhamann.com/blog/2026-07-26-box-drawing-glyphs-on-android.html

在 HTML `

` 标签中渲染 ASCII 图表时,Android 设备常会出现错位问题。这是因为浏览器会针对某些框线字符回退到系统字体,而这些字符的宽度与原本指定的等宽字体并不一致。

虽然常见的解决方案(如通过 `` 标签引用 Google Fonts 的 `Noto Sans Mono`)通常无效,但问题在于这些托管版本往往经过“精简”,缺失了必要的字形。

要解决此问题,您必须自行托管完整的 `.ttf` 字体文件。通过部署完整文件并使用 `@font-face` CSS 规则进行嵌入,可以确保浏览器使用一致的字形集,从而消除回退导致的不一致,并在所有设备上恢复完美的对齐效果。

**CSS 修复方案:**

```css
@font-face {
  font-family: "Noto Sans Mono";
  src: url("/path/to/your.ttf");
}

pre {
  font-family: "Noto Sans Mono", monospace;
}
```

抱歉。
相关文章

原文

I've recently tried to render some ASCII diagrams like the one below in a <pre> tag in HTML. While it looked fine in Neovim and in the MacOS and iOS browsers, I've noticed that on Android the boxes were slightly misaligned.


     │
┌────▼─────┐
│  Phase 1 │
└────┬─────┘
     │
┌────▼─────┐
│  Phase 2 │
└────┬─────┘
     │
     ▼     

Original: the diagram above should not align on Android devices

     │
┌────▼─────┐
│  Phase 1 │
└────┬─────┘
     │
┌────▼─────┐
│  Phase 2 │
└────┬─────┘
     │
     ▼     

Fixed: the diagram above should correctly align on Android devices

A Google search showed that this seems to be a common problem, but no solutions was immediately obvious. ChatGPT wasn't able to fix the issue, but it at least pointed in the right direction: Even with a webfont loaded, the browser is most likely falling back to a system monospace font for glyphs that are not available in the (web)font. And for some reason the monospaced system font on Android has a different width, resulting in the misalignment.

Some google searches and multiple different LLMs suggested to load the Noto Sans Mono from Google fonts. This font would have no missing glyphs (at least for the box-drawing glyphs) and so no fallback should occur. However, after embedding the webfont via a simple <link> tag, the miss-alignment persisted. Various others suggested CSS hacks did not fix the problem either. I was about to give up, but then found the discussion on the mkdocs org which mentioned that the file size of the WOFF2 version of Noto Sans Mono is noticeable smaller than the TTF version since many symbols were missing.

Turns out that the font served from Google fongts via <link> is a most likely a trimmed down version. After downloading the .ttf itself and serving it myself, the alignment issue was gone.

Relevant CSS snippet to embed a .ttf directly in CSS:

@font-face {
  font-family: "Noto Sans Mono";
  src: url("/path/to/your.ttf");
}

pre {
    font-family: "Noto Sans Mono"
}
联系我们 contact @ memedata.com