<div><button>one</button><button>two</button></div> <div> <button>one</button> <button>two</button> </div>
In the first div there is no space between the two buttons, in the second, there is.
Now let's consider another example:
<div>a</div> <div> b</div>
There is no space before that b
.
a<b> bold</b> man
Now the whitespace before bold
is significant
a <b> bold</b> man
Now only one of the spaces (after the "a" or before "bold") is significant.
What are the exact rules behind when whitespace is stripped or collapsed?
They allow you to format your code in a way that will make it easily readable by yourself and other people. In fact much of our source code is full of these white space characters (that is, unless you write obfuscated code).
Active white space - the white space used to enhance page structure and help guide the user through the page's content. Passive white space – applied to improve the aesthetics of the layout without guiding the user through a specific reading, flow, or content order.
Yes, the whitespace here does matter. Typically spaces don't matter, but CSS functions are sensitive like that.
Whitespace refers to characters which are used to provide horizontal or vertical space between other characters. Whitespace is often used to separate tokens in HTML, CSS, JavaScript, and other computer languages.
The reality is somewhat complicated. There are two parts
The parsing actually removes very little white space whilst parsing text (as opposed to markup). It will remove an initial line feed character at the start of <textarea>
and <pre>
elements and also on the invalid <listing>
element, but that's about it.
Jukka refers to the HTML 4.01 section B.3.1 Line breaks saying that "a line break immediately following a start tag must be ignored, as must a line break immediately before an end tag" but that is in a non-normative appendix and browsers do not follow it except for the three elements mentioned above.
That can be demonstrated using Jukka's example here on line breaks with no spaces . Note the #text:
nodes around the button elmeents in the tree display, and that if the line breaks are removed, the '#text:` nodes no longer appear.
We can also see that the rule is not applied by using that first example from the specification here. By adding display:pre
it's clear that the line breaks are not exactly ignored but that the rendering the two examples the same is merely a property of the default white-space handling being white-space:normal
Which brings us to the relevant spec, which is 16.6.1 The 'white-space' processing model in the CSS spec. This covers the systematic rules to be applied to the text characters for each of the white-space setting values.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With