Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML whitespace: spaces before and after <br>

I am trying to better understand the HTML whitespace processing model. Right now I'm comparing two HTML snippets:

<div>a <br>z</div>

and

<div>a<br> z</div>

The first snippet, when renered, yields two lines: "a " and "z" (So the first line has a trailing space.)

The second snippet yields two lines: "a" and "z". There is no leading space on the second line.

My question is: why? I'm currently using this http://www.w3.org/TR/CSS2/text.html#white-space-model as a reference. It states

  1. If a space (U+0020) at the beginning of a line has 'white-space' set to 'normal', 'nowrap', or 'pre-line', it is removed.
  2. All tabs (U+0009) are rendered as a horizontal shift that lines up the start edge of the next glyph with the next tab stop. Tab stops occur at points that are multiples of 8 times the width of a space (U+0020) rendered in the block's font from the block's starting content edge.
  3. If a space (U+0020) at the end of a line has 'white-space' set to 'normal', 'nowrap', or 'pre-line', it is also removed.
  4. If spaces (U+0020) or tabs (U+0009) at the end of a line have 'white-space' set to 'pre-wrap', UAs may visually collapse them.

A naive reading of this would indicate that, since a space that the beginning or end of a line is to be removed (when 'white-space' is 'normal'), the first of my snippets ought to result in no trailing space. But that isn't the case.

So what's going on?

My current theory is that the <br> is secretly counted as a "character" which, in the first snippet, prevents the trailing space from being at the "end" of its line. But I really have no idea.

EDIT: To be clear, I know how to use &nbsp; to create spaces at will. My question is about what rule (with regard to some spec) induces the above behavior.

like image 678
tjhance Avatar asked Sep 10 '15 04:09

tjhance


People also ask

How do you put a space before and after text in HTML?

Creating extra spaces before or after text To create extra spaces before, after, or in-between your text, use the &nbsp; (non-breaking space) extended HTML character.

How do I make white space in HTML?

To add blank space in HTML to text, type the entity &nbsp; for each blank space to add.

What is whitespace Nowrap?

nowrap. Sequences of whitespace will collapse into a single whitespace. Text will never wrap to the next line. The text continues on the same line until a <br> tag is encountered.

What is collapsing white space in HTML?

White space refers to empty or blank values in the code which the browser reads and renders. Html has a special feature of collapsing these white spaces. If you put extra/consecutive white spaces or newlines in the code it will regard it as one white space this is known as collapsing of white spaces.


1 Answers

Good question! I've confirmed the behavior in both Chrome and Firefox, and confirmed that it has nothing to do with <br>, as it's also triggered by an ordinary linebreak in white-space: pre-line conditions:

<div style="white-space:pre-line">a 
z</div>

I've sent an email to the list asking for clarification on this issue, and inquiring whether we should change the spec to match implementations, or file bugs on browsers to match the spec.

like image 54
Xanthir Avatar answered Nov 07 '22 15:11

Xanthir