How do I keep HTML from removing whitespaces? - html

<p>This is a paragraph</p>
becomes
This is a paragraph
but I want it to remain as is. Like
This is a paragraph
How do I do it?

If you don't want to use , you can use something like
<p class="allspace">This is a paragraph</p>
.allspace { white-space: pre }
white-space:pre will format the html with spaces. This approach is better as it doesn't require multiple use of

Use:
<p>This is a paragraph</p>
& nbsp ;
Alternatively referred to as a fixed space or hard space, Non-Breaking
SPace (NBSP) is used in programming, and word processing to create a
space in a line that cannot be broken by word wrap.

Use - it is a non-breakable space.
For example:
<p>This is a paragraph</p>
A non-breaking space (also called no-break space, non-breakable space (NBSP), hard space, or fixed space) is a space character that prevents an automatic line break at its position. In some formats, including HTML, it also prevents consecutive whitespace characters from collapsing into a single space.

Use . For example:
Hello World

Instead you can use <pre> tag. This tag is used for indicating preformatted text. The code tag surrounds the code being marked up.
Browsers normally render pre text in a fixed-pitched font, with whitespace in tact, and without word wrap.
<pre>hello world</pre>

Related

Is there a CSS solution for removing spaces for non-space using languages?

Some languages don't use space. Japanese for example.
A typical paragraph might look like this (taken from the Japanese Wikipedia article on Stack Overflow)
本サービスはコンピュータ・プログラミングの広範囲なトピックを扱っていることが特色である。ウェブサイトは質問と回答を行う機能、またそれらに対する評価付け、wikiやdiggに似た文書の編集機能を備えており、ユーザの活発な参加を促している。Stack Overflowのユーザは良質な回答を行うことによって、評価ポイントや「バッヂ」を得ることができ、本サービスは伝統的なQ&Aサイト・フォーラムにゲーミフィケーションを施したものと言える。全てのユーザによる記述内容はクリエイティブ・コモンズライセンス下にある。
Even though there are 3 sentences in the paragraph above the only space in inside Stack Overflow.
So there's the issue. Japanese users generally don't write long sentences and paragraphs with no breaks. To write the paragraph above most people would not write.
<p>本サービスはコンピュータ・プログラミングの広範囲なトピックを扱っていることが特色である。ウェブサイトは質問と回答を行う機能、またそれらに対する評価付け、wikiやdiggに似た文書の編集機能を備えており、ユーザの活発な参加を促している。Stack Overflowのユーザは良質な回答を行うことによって、評価ポイントや「バッヂ」を得ることができ、本サービスは伝統的なQ&Aサイト・フォーラムにゲーミフィケーションを施したものと言える。全てのユーザによる記述内容はクリエイティブ・コモンズライセンス下にある。</p>
They'd write something more along the lines of
<p>
本サービスはコンピュータ・プログラミングの広範囲なトピックを扱っていることが特色である。
ウェブサイトは質問と回答を行う機能、またそれらに対する評価付け、wikiやdiggに似た文書の
編集機能を備えており、ユーザの活発な参加を促している。Stack Overflowのユーザは
良質な回答を行うことによって、評価ポイントや「バッヂ」を得ることができ、本サービスは
伝統的なQ&Aサイト・フォーラムにゲーミフィケーションを施したものと言える。全てのユーザに
よる記述内容はクリエイティブ・コモンズライセンス下にある。
</p>
Which unfortunately becomes this
With all these unwanted gaps
The only solution I can think of requires JavaScript to go through and remove spaces between Japanese characters and any other character at display time or by adding a build step.
Is there a CSS only solution?
Here's a live sample: The first paragraph is one long hard to edit line. The 2nd paragraph has the line breaks in it
<p>
本サービスはコンピュータ・プログラミングの広範囲なトピックを扱っていることが特色である。ウェブサイトは質問と回答を行う機能、またそれらに対する評価付け、wikiやdiggに似た文書の編集機能を備えており、ユーザの活発な参加を促している。Stack Overflowのユーザは良質な回答を行うことによって、評価ポイントや「バッヂ」を得ることができ、本サービスは伝統的なQ&Aサイト・フォーラムにゲーミフィケーションを施したものと言える。全てのユーザによる記述内容はクリエイティブ・コモンズライセンス下にある。
</p>
<p>
本サービスはコンピュータ・プログラミングの広範囲なトピックを扱っていることが特色である。
ウェブサイトは質問と回答を行う機能、またそれらに対する評価付け、wikiやdiggに似た文書の
編集機能を備えており、ユーザの活発な参加を促している。Stack Overflowのユーザは
良質な回答を行うことによって、評価ポイントや「バッヂ」を得ることができ、本サービスは
伝統的なQ&Aサイト・フォーラムにゲーミフィケーションを施したものと言える。全てのユーザに
よる記述内容はクリエイティブ・コモンズライセンス下にある。
</p>
Here are screenshots to show the difference.
1st paragraph with no breaks in HTML
2nd with
Also note that whatever solution it should not collapse the space in Stack Overflow
Use white-space: pre or white-space: pre-wrap
Reference: https://www.w3schools.com/cssref/pr_text_white-space.asp
JSFiddle : https://jsfiddle.net/kqcvp10w/

&nlsp; HTML space. What was it?

I can not remember what it was that was basically blank, but also worked as a space. It's something like &nlsp; or something like that.
I was using  but I really wanna remember this one. I wish I could word the post better to explain what I'm talking about, but I'm sure someone here understands just by the confusion of &nlsp;
It is used for non-line breaking space in html.
and
having too many consecutive is eye itching and bad. If you want bigger space to be generated then use a <span class="spaceOf5px"> <span>. This is useful only as inline space. For block level, use DIV tag.
/*CSS for Wide Space Class*/
.spaceOf5px{
width: 5px;
}
.spaceOf10px{
width: 10px;
}
Remember - there is a space between the span tags.
It is . You can find out more about it in several online references, such as wikipedia. It stands for non-breaking space.
is the HTML entity for non-breaking space. No line break will occur between words separated by a non-breaking space even in the cases where a browser would normally perform text wrapping.
is a Non-Breaking SPace in HTML. Normally in HTML, when multiple spaces separate text, as seen here,
<h3>Before spaces After spaces</h3>
the browser renders them as only one space:
However, if you use , the browser renders each space as non-breaking:
<h3>Before spaces After spaces</h3>
The spaces do not collapse to one (hence 'non-breaking'). Each space is rendered:

How to get a tab character?

In HTML, there is no character for a tab, but I am confused as to why I can copy and paste one here: " " (You can't see the full width of it, but if you click to edit my question, you will see the character.) If I can copy and paste a tab character, there should be a unicode equivalent that can be coded into html. I know it doesn't exist, but this is a mystery I've never been able to grasp.
So my question is: why is there not a unicode character for a tab even if I can copy and paste it?
Sure there's an entity for tabs:
(The tab is ASCII character 9, or Unicode U+0009.)
However, just like literal tabs (ones you type in to your text editor), all tab characters are treated as whitespace by HTML parsers and collapsed into a single space except those within a <pre> block, where literal tabs will be rendered as 8 spaces in a monospace font.
Try  
as per the docs :
The character entities   and   denote an en space and an em
space respectively, where an en space is half the point size and an em
space is equal to the point size of the current font. For fixed pitch
fonts, the user agent can treat the en space as being equivalent to A
space character, and the em space as being equuivalent to two space
characters.
Docs link : https://www.w3.org/MarkUp/html3/specialchars.html
put it in between <pre></pre> tags then use this characters
it would not work without the <pre></pre> tags
Posting another alternative to be more complete. When I tried the "pre" based answers, they added extra vertical line breaks as well.
Each tab can be converted to a sequence non-breaking spaces which require no wrapping.
" "
This is not recommended for repeated/extensive use within a page. A div margin/padding approach would appear much cleaner.
I use <span style="display: inline-block; width: 2ch;"> </span> for a two characters wide tab.
Tab is [HT], or character number 9, in the unicode library.
As mentioned, for efficiency reasons sequential spaces are consolidated into one space the browser actually displays. Remember what the ML in HTML stand for. It's a Mark-up Language, designed to control how text is displayed.. not whitespace :p
Still, you can pretend the browser respects tabs since all the TAB does is prepend 4 spaces, and that's easy with CSS. either in line like ...
<div style="padding-left:4.00em;">Indenented text </div>
Or as a regular class in a style sheet
.tabbed {padding-left:4.00em;}
Then the HTML might look like
<p>regular paragraph regular paragraph regular paragraph</p>
<p class="tabbed">Indented text Indented text Indented text</p>
<p>regular paragraph regular paragraph regular paragraph</p>

White spaces not ignored in pre tag

I have read that the 'pre' tag will collapse all white spaces and tabs into one space, but it doesn't do so for me.
why is it so? is it depending on web browser or should I do something more?
From HTML 4.01 Specification
The PRE element tells visual user agents that the enclosed text is "preformatted". When handling preformatted text, visual user agents:
May leave white space intact.
May render text with a fixed-pitch font.
May disable automatic word wrap.
Must not disable bidirectional processing
PRE tags will leave white space as you have typed it. The purpose is just that. If you don't use a pre tag, standard HTML will collapse white space as you have written. Use PRE if you are interested in preserving white space, not collapsing it.
Here is an example in JSFiddle.
This will preserve white space:
<pre>
Spaces
and more
galore
</pre>
This will collapse white space:
<div>
All
together
now
</div>
The PRE tag will just keep you contents in the same format as you wrote it.
it is used to keep your "preformatted" text the same way as you wrote it.

What's the difference between " " and " "?

Both of them mean space, but is there any difference?
One is non-breaking space and the other is a regular space. A non-breaking space means that the line should not be wrapped at that point, just like it wouldn’t be wrapped in the middle of a word.
Furthermore as Svend points out in his comment, non-breaking spaces are not collapsed.
The entity produces a non-breaking space, which is used when you don't want an automatic line break at that position. The regular space has the character code 32, while the non-breaking space has the character code 160.
For example when you display numbers with space as thousands separator: 1 234 567, then you use non-breaking spaces so that the number can't be split on separate lines. If you display currency and there is a space between the amount and the currency: 42 SEK, then you use a non-breaking space so that you don't get the amount on one line and the currency on the next.
In addition to the other answers here, non-breaking spaces will not be "collapsed" like regular spaces will. For example:
<!-- Both -->
<p>Word1 Word2</p>
<!-- and -->
<p>Word1 Word2</p>
<!-- will render the same on any browser -->
<!-- While the below one will keep the spaces when rendered. -->
<p>Word1 Word2</p>
Not an answer as much as examples...
Example #1:
<div style="width:45px; height:45px; border: solid thin red; overflow: visible">
Hello There
</div>
Example #2:
<div style="width:45px; height:45px; border: solid thin red; overflow: visible">
Hello There
</div>
And link to the fiddle.
Multiple normal white space characters (space, tabulator and line break) are treated as one single white space character:
For all HTML elements except PRE, sequences of white space separate "words" (we use the term "word" here to mean "sequences of non-white space characters"). When formatting text, user agents should identify these words and lay them out according to the conventions of the particular written language (script) and target medium.
So
foo bar
is displayed as
foo bar
But no-break space is always displayed. So
foo&‍nbsp;&‍nbsp;&‍nbsp;bar
is displayed as
foo bar
You can see a working example here:
http://codepen.io/anon/pen/GJzBxo
and
http://codepen.io/anon/pen/LVqBQo
Same div, same text, different "spaces"
<div style="width: 500px; background: red"> [loooong text with spaces]</div>
vs
<div style="width: 500px; background: red"> [loooong text with ]</div>
As already mentioned, you will not receive a line break where there is a "no-break space".
Also be wary, that elements containing only a " " may show up incorrectly, where will work. In i.e. 6 at least (as far as I remember, IE7 has the same issue), if you have an empty table element, it will not apply styling, for example borders, to the element, if there is no content, or only white space. So the following will not be rendered with borders:
<td></td>
<td> <td>
Whereas the borders will show up in this example:
<td>& nbsp;</td>
Hmm -had to put in a dummy space to get it to render correctly here
The first is not treated as white space by the HTML parser, the second is. As a result the " " is not guaranteed to showup within certain HTML markup, while the non breakable space will always show up.
should be handled as a whitespace.
should be handled as two whitespaces
' ' can be handled as a non interesting whitespace
' ' + ' ' can be handled as a single ' '
is stackable, meaning you can create multiple spaces all together.
HTML will only parse one space '' and it drops the rest...
If you want five spaces, you would place 5 x
#Zoidberg is right,
example:
<h1>title</h1> <h2>date</h2>
will not display space between header markup, with
& nbsp ;
will do space :)
When having line-breaks, the line will not break when you use an $bnsp; because it's a 'non-breaking space'. This can be important if you have certain product-names or such, that always shall be written together.
Can be interesting if you (have to) use a whitespace as delimiter in numbers, like 12344567, that is displayed 12 344 567 in France. Without the the line would break in the middle of the number, very ugly. Test:12 344 567
TLDR; In addition to the accepted answer; One is implicit and one is explicit.
When the HTML you've written or had generated by an application/library/framework is read by your browser it will do it's best to interpret what your HTML meant (which can vary from browser to browser). When you use the HTML entity codes, you are being more specific to the browser. You are explicitly telling it you wish to display a character to the user (and not that you are just spacing your HTML for easier readability for the developer for instance).
To be more concrete, if the output HTML were:
<html>
<title>hello</title>
<body>
<p>
Tell me and I will forget. Teach me and I
may remember. Involve me and I will learn.
</p>
</body>
</html>
The browser would only render one space between all of these words (even the ones that have been indented for better developer readability.
If, however, you put the same thing and only changed the <p> tag to:
<p>Hello There</p>
Then it would render the spaces, as you've instructed it more explicitly. There is some history of using these spaces for styling. This use has somewhat been diminished as CSS has matured. However, there are still valid uses for many of the HTML character entities: avoiding unexpectedly/unintentionally interpretation (e.g. if you wanted to display code). The w3 has a great page to show the other character codes.