Using Whitespace Characters

This section describes how whitespace can be used in HTML documents. Whitespaces characters are collapsed and trimmed in element contents of all elements except 'pre'.

Syntax rules about whitespace characters can be summarized as:

1. There are 5 characters are defined as whitespace characters in HTML specification:

U+0020   Space 
U+0009   Tab
U+000A   Line feed
U+000C   Form feed
U+000D   Carriage return

2. Whitespace characters are preserved in element attribute values.

3. Whitespace characters are preserved in "pre" element contents.

4. Any sequence of whitespace characters is collapsed into a single space character of U+0020 in any element contents.

5. Any leading and trailing whitespace characters are trimmed off from any element contents.

These rules give us the freedom to enter extra space characters and line breaks make the HTML document code easier to read. For example, the following code is easy to read:

<article>
 <section>
  <p>
     Twinkle, twinkle, little star,
     How I  wonder  what  you  are.
     Up above the  world  so  high,
     Like a  diamond  in  the  sky.
  </p>
 </section>
</article>

This code is much harder to read. But it represents the same HTML document as the above code:

<article><section><p>Twinkle, twinkle, little star, How I wonder what
you are. Up above the world so high, Like a diamond in the sky.</p>
</section></article>

Last update: 2014.

Table of Contents

 About This Book

 Introduction of HTML

 Introduction of HTML5 Features

 HTML Document Structure and Content

HTML Document and Elements Syntax

 HTML Document Syntax

 Document Character Set and Encoding

 Entering Comments in HTML Document

 Opening and Closing Tags of Elements

 Using Nested Elements

 Specifying Element Attributes

 Specifying Element Contents

 Element Content Syntax Types

Using Whitespace Characters

 Using Character Entity References

 Using Numeric Character References

 References

 PDF Printing Version