简体   繁体   English

如何在保留字符宽度的 HTML 中使用等宽字体显示特殊的 unicode 字符

[英]How to display special unicode characters using monospace font in HTML with preserved character width

I'm using pre element to display some text including special unicode characters (⚡ ⚑ ▶ ◀ ⁋).我正在使用pre元素来显示一些文本,包括特殊的 unicode 字符(⚡ ⚑ ▶ ◀ ⁋)。 I noticed that browsers render these special characters wider than regular ones, occupying more space horizontally.我注意到浏览器将这些特殊字符呈现得比常规字符更宽,在水平方向上占据更多空间。

This can be easily seen here: https://gist.github.com/968b5c22cce14909cf27 Both lines have 20 characters but notice that first one is longer (screen pixels).在这里可以很容易地看到: https : //gist.github.com/968b5c22cce14909cf27两行都有 20 个字符,但请注意第一行更长(屏幕像素)。

Is there a way (CSS) to force pre elements (or other element with monospace font applied) to have really fixed character width?有没有办法(CSS)强制预元素(或其他应用等宽字体的元素)具有真正固定的字符宽度?

I checked on Chrome and Firefox and they both expand the width of special characters.我检查了 Chrome 和 Firefox,它们都扩展了特殊字符的宽度。

It's not the browser that's expanding the characters, it's the font definition itself.扩展字符的不是浏览器,而是字体定义本身。 I doubt that there exists a truly monospace font that includes all these characters and defines them all at the same width, but that's what you should be looking for.我怀疑是否存在真正的等宽字体,包括所有这些字符并以相同的宽度定义它们,但这正是您应该寻找的。 See this question for more details about why this is probably not available.有关为什么这可能不可用的更多详细信息,请参阅此问题

The reason is that some of the characters are missing from the first font listed in the font-family declaration being applied.原因是正在应用的font-family声明中列出的第一个字体中缺少某些字符。 They will thus be displayed some other font(s) in the system, or some indicator of unrepresentable character is shown.因此,它们将在系统中显示一些其他字体,或者显示一些不可表示字符的指示符。

For example, the first character is present in a handful of fonts only, see http://www.fileformat.info/info/unicode/char/26a1/fontsupport.htm (which does not cover all fonts but most fonts that people probably have on their computers).例如,第一个字符仅出现在少数字体中,请参阅http://www.fileformat.info/info/unicode/char/26a1/fontsupport.htm (它并未涵盖所有字体,但人们可能会使用的大多数字体在他们的电脑上)。

Even if the other fonts used are monospace font, they may have different advance widths for characters.即使使用的其他字体是等宽字体,它们也可能具有不同的字符前进宽度。 For example, Everson Mono has a slightly smaller width than DejaVu Sans Mono .例如, Everson Mono 的宽度比DejaVu Sans Mono略小。 Being monospace means just that within the font , all characters have the same advance width.等宽意味着在 font 中,所有字符都具有相同的前进宽度。

So you would need to use a single font that contains all the characters you need.因此,您需要使用包含您需要的所有字符的单一字体。 For this collection of characters, the two above-mentioned fonts are probably the only publicly distributed monospace fonts that contain them all.对于这个字符集,上面提到的两种字体可能是唯一包含它们的公开发行的等宽字体。 Well, there is unifont , but it is a bitmap font with very coarse design;嗯,有unifont ,但它是一种设计非常粗糙的位图字体; it might look tolerable in 12pt size or somewhat larger.它在 12pt 或更大的尺寸下看起来可能是可以接受的。

Add a span around each special character and set a fixed width for all span elements.在每个特殊字符周围添加一个span ,并为所有span元素设置一个固定的宽度。

Example CSS:示例 CSS:

pre .specialchar {
   display:inline-block;
   width: 8.7px;  /* Find the right value */
}

Example HTML:示例 HTML:

<pre>Test 123 AAA 123
Test 123 <span class="specialchar">▶</span><span class="specialchar">▶</span><span class="specialchar">▶</span> 123</pre>

Results in same width for each character:每个字符的宽度相同:

预先格式化的相同宽度的特殊字符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM