News:

This week IPhone 15 Pro winner is karn
You can be too a winner! Become the top poster of the week and win valuable prizes.  More details are You are not allowed to view links. Register or Login 

Main Menu

HTML - Introducing spaces and new lines with character entities

Started by ben2ong2, October 03, 2006, 06:19:37 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

ben2ong2

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.

You are not allowed to view links. Register or Login
You are not allowed to view links. Register or Login