I have the below HTML file.
<HTML>
<head>
</head>
<body>
<div>
<span>"| Testing"</span>
</div>
</body>
</HTML>
I want to print "| Testing"
, But instead printing that this print "| Testing"
. What happens to all those spaces
.
Isn't that code suppose to print what's In between those <span>
tags? Am I doing something wrong?
From the HTML4 specification about white space characters (emphasis mine):
Note that a sequence of white spaces between words in the source document may result in an entirely different rendered inter-word spacing (except in the case of the PRE element). In particular, user agents should collapse input white space sequences when producing output inter-word space.
So the specification dictates that multiple consecutive spaces should be collapsed into one.
There are a couple of options for "preserving" consecutive spaces:
Use CSS to change the way how the browser renders the spaces (demo):
span {
white-space: pre-wrap;
}
You could encode the spaces as  
or  
(depending on the width of the space you want to insert) (demo) *.
You could use the <pre>
element (demo):
<pre><span>"| Testing"</span></pre>
Since browsers use a different font for the content of pre
elements, you'd also have to use CSS to reset the font to the font used in the rest of your page, e.g.
pre {
font-family: serif;
}
*: You could also use the
HTML entity, however, this change will another behavior of the browser: When the content is too long to fit in the available width of the element, the browser will automatically break the content on spaces. nbsp
stands for *no-break_space*, so while the browser will render spaces, it won't break overflowing content on them (demo).
use
instead of spaces
<span>"| Testing"</span>
don't use
in your html. Add a css stylesheet, put a class in your span. add a padding to your span element.
<span class="padd">Testing</span>
In your stylesheet
span.padd{padding-left:60px;}
You could add the '|' before the span tag or just use span.padd{ border-left: 1px solid #000; }
Multiple spaces in HTML are collapsed and shown as only one space. It's the same reason your line breaks don't all show up and clutter up your text.
You can use
instead of a space to force insertion of a space. Example:
<span>"| Testing"</span>
will display as:
"| Testing"
What happens is that browser treat a sequence of space characters as equivalent to a single space. This has always been how browsers behave and what HTML specifications say.
There are ways to change the handling of spaces with CSS or using certain special HTML elements. However, the most flexible approach is to use CSS to add padding (or margin). For example:
<style>
.bar { padding-right: 1.25em; }
</style>
...
<span>"<span class=bar>|</span> Testing"</span>
The value of 1.2em
in this example roughly corresponds to the width of five spaces (to be used in addition to the spacing caused by space character). The width of a space varies by font. But normally it is best to select the spacing using units like em
, rather than thinking in terms of amount of spaces.
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