Nonbreaking Spaces - HTML

Just as you will want to break some text into discrete chunks, at other times you will want to keep text together. For example, you wouldn’t want words separated in dates (December 25, 2003), awkward phrases that include letters and numbers (24 hours), or in some company names (“International Business Machine Corporation”).

Suppose you were to use the phrase “12 Angry Men.” You would not want a browser to split the “12” and “Angry” across two lines, as shown here:

A good example of this technique appears in the movie “12 Angry Men.”

In cases where you do not want the client browser to break text, you should use a nonbreaking space entity ( ) instead of a normal space. For example, when coding the “12 Angry Men” paragraph, you would use something similar to the following code:

<p>A good example of this technique appears in the movie“12&nbsp;Angry&nbsp;Men.”</p>

The browser will then be forced to keep the phrase together, treating it as one cohesive word.

Tip

Nonbreaking spaces have long been used to force formatting on the client browser. For example, to indent a line by three spaces, HTML coders would use something like the following:

&nbsp;&nbsp;&nbsp;Indented by three spaces

Before robust CSS styles, this was the only way to “space fill” text. However, now that there are a myriad of ways to achieve this result using styles, this technique becomes sloppy and should not be used. Instead, create an appropriate style and use it to achieve the same results.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

HTML Topics