Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spaces In between the <span> and </span> tags is not displaying

Tags:

html

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?

like image 857
prime Avatar asked Mar 02 '14 09:03

prime


5 Answers

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 &ensp; or &emsp; (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 &nbsp; 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).

like image 59
Felix Kling Avatar answered Oct 12 '22 11:10

Felix Kling


use &nbsp; instead of spaces

<span>"|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Testing"</span>
like image 25
Mohammad Masoudian Avatar answered Oct 12 '22 11:10

Mohammad Masoudian


don't use &nbsp; 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; }

like image 38
Gregory Avatar answered Oct 12 '22 09:10

Gregory


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 &nbsp; instead of a space to force insertion of a space. Example:

<span>"|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Testing"</span>

will display as:

"|      Testing"
like image 2
elixenide Avatar answered Oct 12 '22 11:10

elixenide


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.

like image 1
Jukka K. Korpela Avatar answered Oct 12 '22 11:10

Jukka K. Korpela