Browser white space rendering

后端 未结 3 1901
无人及你
无人及你 2020-12-07 01:06

Recently I was editing someone else\'s code and I noticed that spaces were used something like the following:

Some text     &
3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-07 01:27

    Browsers only collapse consecutive regular space characters. Text rendering is mostly governed by the CSS spec rather than the HTML spec (with exceptions); with respect to whitespace collapsing, section 16.6.1 of CSS2.1 has the details. Specifically:

    any space (U+0020) following another space (U+0020) — even a space before the inline, if that space also has 'white-space' set to 'normal', 'nowrap' or 'pre-line' — is removed.

    Since there's a non-breaking space separating every two space characters that would otherwise be consecutive (and non-breaking spaces are not considered "regular" space characters), the browser has no opportunity to collapse any of them, and so must render all of them in sequence.

    The behavior across browsers is mostly identical, except for a nasty bug in Chrome regarding the part about "a space before the inline".

提交回复
热议问题