是否有一个固定宽度的空格字符,不能在合理的段落中进行扩展?

时间:2023-01-29 20:24:08

Justified paragraphs expand inter-word spacing, but I expected   to be unaffected by this expansion. Unfortunately, it is affected, and so I need to find an unaffected space character (preferably one with the same width as an ordinary space in an ordinary left-justified paragraph). Any suggestions?

合理的段落扩展了词间距,但我预计 不受这种扩张的影响。不幸的是,它受到了影响,因此我需要找到一个未受影响的空格字符(最好是一个与普通左对齐段落中的普通空格宽度相同的字符)。有什么建议?

3 个解决方案

#1


5  

You could just add a character such as . and make it invisible.

你可以添加一个像。并使它隐形。

It works. jsFiddle here

有用。 jsFiddle在这里

It is not parsed as whitespace, since it is technically a character that just isn't visible.

它不被解析为空格,因为它在技术上是一个不可见的字符。

For a cleaner method, you could insert the character via the :after pseudo element..

对于更干净的方法,您可以通过:after伪元素插入字符。

span:after {
    content: ".";
    visibility: hidden;
}

jsFiddle here

#2


4  

The rendering of space characters other than SPACE and NO-BREAK SPACE is not defined in HTML specifications. In practice, various fixed-width spaces can be used, and they are not expanded in justification (effectively, they are treated as graphic characters, just with empty glyphs). The one that most closely matches a normal space in width is FOUR-PER-EM SPACE,  , but from the comments, it seems that you would rather want to use THIN SPACE or SIX-PER-EM SPACE.

HTML规范中未定义除SPACE和NO-BREAK SPACE之外的空格字符的呈现。在实践中,可以使用各种固定宽度的空间,并且它们不会在理由中扩展(实际上,它们被视为图形字符,仅使用空的字形)。与宽度中的正常空间最匹配的那个是FOUR-PER-EM SPACE, ,但是根据评论,您似乎更愿意使用THIN SPACE或SIX-PER-EM SPACE。

Previously, browsers used to treat NO-BREAK SPACE as non-expansible, and some may still do, but for some odd reason, most browsers have abandoned this sensible idea. There is no guarantee that they won’t do the same to fixed-width space.

以前,用于将NO-BREAK SPACE视为不可扩展的浏览器,有些可能仍然可以,但由于一些奇怪的原因,大多数浏览器已经放弃了这个明智的想法。无法保证他们不会对固定宽度的空间做同样的事情。

Apart from the theoretical point and the risk mentioned, fixed-width spaces suffer from font problems and breakability. The font problems mainly apply to old versions of IE, and the breakability (i.e., fixed-width spaces are treated as allowing a line break after them) could be addressed by using NARROW NO-BREAK SPACE if its width suits you and you take the risk with the font issue.

除了理论上的点和所提到的风险之外,固定宽度空间还存在字体问题和易碎性。字体问题主要适用于IE的旧版本,并且如果其宽度适合您并且您采用了NARROW NO-BREAK SPACE,则可以通过使用NARROW NO-BREAK SPACE解决可破坏性(即,固定宽度空间被视为允许它们之后的换行符)。有字体问题的风险。

So it is probably best to do the spacing in CSS, even though it means a bit more work. For e.g. “e.g.” with spacing after the first period, you would need to decide whether it should be “e.g.” or “e. g.” at the character level. In the former case, just wrap the first period in a span and set padding-right on it. In the latter case, you could e.g. use e.<span class=thin>&nbsp;</span>g. and set the width of the span to something sufficiently small (using display: inline-block and a width setting).

所以最好在CSS中做间距,即使它意味着更多的工作。对于例如“例如”在第一个时期之后有间距,你需要决定它应该是“例如”还是“e。 g。“在角色层面。在前一种情况下,只需在span中包装第一个句点并在其上设置padding-right。在后一种情况下,您可以例如使用e。  g。并将跨度的宽度设置为足够小(使用display:inline-block和width设置)。

#3


1  

Unicode offers several spacing characters. Of use here is probably the thin space, (Unicode 0x2009, &thinsp; in HTML) which is smaller than, but close to, a regular space. See it working on JSFiddle.

Unicode提供了几个间距字符。这里使用的可能是稀疏空间(Unicode 0x2009, 在HTML中),它小于但接近常规空间。看到它在JSFiddle上工作。

You may also be interested in the related Unicode 0x202F which is the non-breaking version of thin space.

您可能还对相关的Unicode 0x202F感兴趣,这是瘦空间的非破坏版本。

The benefit of this over the hidden character method is that the whitespace actually still exists in the source, and thus will be read as such by a screen reader (whereas the hidden character will be wholly ignored).

这对隐藏字符方法的好处是,空格实际上仍然存在于源中,因此将由屏幕阅读器读取(而隐藏字符将被完全忽略)。

#1


5  

You could just add a character such as . and make it invisible.

你可以添加一个像。并使它隐形。

It works. jsFiddle here

有用。 jsFiddle在这里

It is not parsed as whitespace, since it is technically a character that just isn't visible.

它不被解析为空格,因为它在技术上是一个不可见的字符。

For a cleaner method, you could insert the character via the :after pseudo element..

对于更干净的方法,您可以通过:after伪元素插入字符。

span:after {
    content: ".";
    visibility: hidden;
}

jsFiddle here

#2


4  

The rendering of space characters other than SPACE and NO-BREAK SPACE is not defined in HTML specifications. In practice, various fixed-width spaces can be used, and they are not expanded in justification (effectively, they are treated as graphic characters, just with empty glyphs). The one that most closely matches a normal space in width is FOUR-PER-EM SPACE, &#x2005;, but from the comments, it seems that you would rather want to use THIN SPACE or SIX-PER-EM SPACE.

HTML规范中未定义除SPACE和NO-BREAK SPACE之外的空格字符的呈现。在实践中,可以使用各种固定宽度的空间,并且它们不会在理由中扩展(实际上,它们被视为图形字符,仅使用空的字形)。与宽度中的正常空间最匹配的那个是FOUR-PER-EM SPACE, ,但是根据评论,您似乎更愿意使用THIN SPACE或SIX-PER-EM SPACE。

Previously, browsers used to treat NO-BREAK SPACE as non-expansible, and some may still do, but for some odd reason, most browsers have abandoned this sensible idea. There is no guarantee that they won’t do the same to fixed-width space.

以前,用于将NO-BREAK SPACE视为不可扩展的浏览器,有些可能仍然可以,但由于一些奇怪的原因,大多数浏览器已经放弃了这个明智的想法。无法保证他们不会对固定宽度的空间做同样的事情。

Apart from the theoretical point and the risk mentioned, fixed-width spaces suffer from font problems and breakability. The font problems mainly apply to old versions of IE, and the breakability (i.e., fixed-width spaces are treated as allowing a line break after them) could be addressed by using NARROW NO-BREAK SPACE if its width suits you and you take the risk with the font issue.

除了理论上的点和所提到的风险之外,固定宽度空间还存在字体问题和易碎性。字体问题主要适用于IE的旧版本,并且如果其宽度适合您并且您采用了NARROW NO-BREAK SPACE,则可以通过使用NARROW NO-BREAK SPACE解决可破坏性(即,固定宽度空间被视为允许它们之后的换行符)。有字体问题的风险。

So it is probably best to do the spacing in CSS, even though it means a bit more work. For e.g. “e.g.” with spacing after the first period, you would need to decide whether it should be “e.g.” or “e. g.” at the character level. In the former case, just wrap the first period in a span and set padding-right on it. In the latter case, you could e.g. use e.<span class=thin>&nbsp;</span>g. and set the width of the span to something sufficiently small (using display: inline-block and a width setting).

所以最好在CSS中做间距,即使它意味着更多的工作。对于例如“例如”在第一个时期之后有间距,你需要决定它应该是“例如”还是“e。 g。“在角色层面。在前一种情况下,只需在span中包装第一个句点并在其上设置padding-right。在后一种情况下,您可以例如使用e。  g。并将跨度的宽度设置为足够小(使用display:inline-block和width设置)。

#3


1  

Unicode offers several spacing characters. Of use here is probably the thin space, (Unicode 0x2009, &thinsp; in HTML) which is smaller than, but close to, a regular space. See it working on JSFiddle.

Unicode提供了几个间距字符。这里使用的可能是稀疏空间(Unicode 0x2009, 在HTML中),它小于但接近常规空间。看到它在JSFiddle上工作。

You may also be interested in the related Unicode 0x202F which is the non-breaking version of thin space.

您可能还对相关的Unicode 0x202F感兴趣,这是瘦空间的非破坏版本。

The benefit of this over the hidden character method is that the whitespace actually still exists in the source, and thus will be read as such by a screen reader (whereas the hidden character will be wholly ignored).

这对隐藏字符方法的好处是,空格实际上仍然存在于源中,因此将由屏幕阅读器读取(而隐藏字符将被完全忽略)。