How to introduce space and new lines in web pages with the help of HTML character entities.
introducing spaces and new lines, formatting web pages, HTML character entities,  
Introducing spaces and new lines with character entitiesGo to Introducing spaces and new lines with character entitiesHTML & JavaScriptGo to HTML and JavaScript tips and tricksTips and tricksGo to web development Tips and tricksHomepage

Introducing spaces and new lines with character entities

When displaying the web page, browsers collapse and ignore, spaces, tabs and new line characters in the HTML. You might have spent a lot of time formatting a document using spaces only to be surprised at its display in a browser. Let us check this out.

This contains          ten space characters.
A new line character has been inserted.
		And here are 2 tabs before this line.

is displayed in the browser as:

This contains ten space characters. A new line character has been inserted. And here are 2 tabs before this line.

You'll notice that the 10 spaces, new lines and tabs that were visible in the code above are not shown by the browser, though you will find them in the source code (VIEW-SOURCE or PAGE SOURCE).

The only HTML tag that preserves such characters is <PRE> and I have used consistently for code display (as above), but <PRE> renders the font as fixedspaced!

Web developers employ various tricks to achieve indentation and introduce spaces in their documents. They use list tags - <OL>, <UL> and <LI> - or <BLOCKQUOTE>. This is not advisable as HTML has a special character for introducing spaces, &nbsp; (lowercase).

Placing 8 &nbsp; will display equivalent number of space characters

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;8

          8


If you're wondering what nbsp; means; it stands for non-breaking space character. The &nbsp; is a character entity. You can find a thorough list of character entities here.

For introducing new lines, use <BR> tag.


AddThis Social Bookmark Button


Page contents: details on formatting web pages correctly by introducing new lines and spaces created with HTML character entities.

Page URL: http://www.webdevelopersnotes.com/tips/html/ introducing_spaces_and_new_lines_with_character_entities.php3



Join Mailing List


Feedback/Questions




50+ web hosting FAQs

Search engine for your website

Free software from Google - The Google Pack Collection

Create your own search engine
Search WebDevelopersNotes.com