Alan
Alan

Reputation: 11

<pre> is not preserving whitespace

Why does:

<pre style="background:red">
line 1
</pre>

render the same as:

<pre style="background:red">line 1</pre>

The first has two more line breaks, but it seems the browser ignores them. What's the rule for this?

Upvotes: 1

Views: 1063

Answers (1)

ButchMonkey
ButchMonkey

Reputation: 1899

If a text node begins with white space (space, new line) it will be ignored by HTML parsers. Encoding the new line into a proper HTML entity forces the parser to acknowledge it.

&#13; == carriage return

use this instead:

<pre style="background:red">&#13;line 1</pre>

The pre tag will keep all formatting inbetween, but not at the beginning

See https://stackoverflow.com/a/15529725/6852641 for more

Upvotes: 4

Related Questions