Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between pre(html tag) and white-space:pre newline omission

Tags:

html

css

I thought pre(html tag) active just like 'white-space:pre'. But it's not.

<pre>
aaa
bbb
</pre>

<p style="white-space:pre;">
aaa
bbb
</p>

<pre> ignore the first and last \n. But <p> keep the first \n and ignore the last one. Why?

jsfiddle test

like image 654
Zachary Avatar asked Jan 16 '23 17:01

Zachary


2 Answers

The HTML standard states:

Note: In the HTML syntax, a leading newline character immediately following the pre element start tag is stripped.

Read here: http://www.whatwg.org/specs/web-apps/current-work/multipage/grouping-content.html#the-pre-element

So, the <pre> element contains this special rule. It seems that if other types of elements have a white-space:pre setting, that rule doesn't apply.

like image 124
Šime Vidas Avatar answered Jan 18 '23 08:01

Šime Vidas


Actually, they are the same. The reason you're getting the space on top for <p>s is because by default <p> adds an empty line above itself so that you can distinguish between paragraphs. <pre> doesn't do that, so you get no extra space there.

like image 31
Phillip Schmidt Avatar answered Jan 18 '23 07:01

Phillip Schmidt