Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the browser renders a newline as space?

Tags:

html

For the longest time, I have wanted to understand why the browser adds an empty space between rendered HTML elements when there is a NewLine between them, for example:

<span>Hello</span><span>World</span> 

The html above will output the “HelloWorld” string without a space between “Hello” and “World”, however in the following example:

<span>Hello</span> <span>World</span> 

The html above will output a “Hello World” string with a space between “Hello” and “World”.

Now, I have no problem accepting that this is just the way it works period, but the thing that bugs me a little is that I was always under the impression that spaces (or newlines) between the html elements would not matter at the time when the browser rendered the html to the user.

So my question is if anyone knows what the philosophical or technical reason behind this behavior.

Thank you.

like image 955
Rene Avatar asked Feb 25 '09 23:02

Rene


People also ask

Is newline a space?

Space, tab, line feed (newline), carriage return, form feed, and vertical tab characters are called "white-space characters" because they serve the same purpose as the spaces between words and lines on a printed page — they make reading easier.

Why is there a space at the top of my website?

If you're using a layout that includes your theme's styling, it's possible that you see some space or a 'gap' between your header and where your content starts, or between your content and the footer of your page. This space is actually a part of your theme's layout.

How do you ignore a new line in HTML?

HTML ignores newlines by default in most HTML elements. One element that is an exception to this rule is the textarea element (the textarea element isn't available directly with ASP.NET controls, but it's what's rendered with ASP.NET when you set a TextBox control's TextMode property to multiline .

How do you render a space in HTML?

The &nbsp; character creates a space that does not break into a new line. Two words that are separated by a non-breaking space always appear on the same line. The &#9; and &Tab; characters create tab spaces in HTML. Unfortunately, they can't be used independently.


2 Answers

Browsers condense multiple whitespace characters (including newlines) to a single space when rendering. The only exception is within <pre> elements or those that have the CSS property white-space set to pre or pre-wrap set. (Or in XHTML, the xml:space="preserve" attribute.)

like image 161
David Z Avatar answered Sep 23 '22 18:09

David Z


Whitespace between block elements are ignored. However, whitespaces between inline elements are transformed into one space. The reasoning is that inline elements might be interspersed with regular inner text of the parent element.

Consider the following example:

<p>This is my colored <span class="red_text">Hello</span> <span class="blue_text">World</span> example</p> 

In the ideal case, you want the user to see

This is my colored Hello World example 

Removing the whitespace between the two spans however would result in:

This is my colored HelloWorld example 

But that same sample can be rewritten by an author (with OCD about the HTML formatting :-)) as:

<p>   This is my colored   <span class="red_text">Hello</span>   <span class="blue_text">World</span>   example </p> 

It would be better if this was rendered consistently with the previous example.

like image 44
Franci Penov Avatar answered Sep 26 '22 18:09

Franci Penov