How to Work with Whitespace in HTML

In computer languages, whitespace characters are those characters that create horizontal or vertical space. In this how-to you'll learn how to use whitespace in HTML documents.

  1. Create a simple HTML document by copying the following and pasting it into a new text file:
    <!DOCTYPE HTML>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title></title>
    </head>
    <body>
    
    </body>
    </html>
    

  2. Type some text between <body> and </body>. For example:
    <!DOCTYPE HTML>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title></title>
    </head>
    <body>
    Here is some text.
    </body>
    </html>
    

  3. Insert some whitespace characters into the text you typed between <body> and </body>. For example:
    <!DOCTYPE HTML>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title></title>
    </head>
    <body>
    Here
    			is      some
    			text.
    </body>
    </html>
    

  4. Save the file as whitespace.html and open it in your web browser. Notice that the words in your text still appear as if they were each separated by a single space. Web browsers ignore whitespace characters inside of elements.
    Whitespace in content.

  5. Create a <pre> element between <body> and </body> and put some text with whitespace into it. The <pre> element is the exception to the rule of browsers ignoring whitespace characters. When text is within a <pre> element web browsers will display it without ignoring the whitespace characters.
    <!DOCTYPE HTML>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title></title>
    </head>
    <body>
    <pre>This 			text
    has
    pre
    around
    it.</pre>
    Here
    			is      some
    			text, without
    				pre.
    </body>
    </html>
    

  6. Save the HTML document and open it in your web browser. It will now look like this:
    Demo of whitespace characters inside a pre element
Author: Chris Minnick

Chris Minnick, the co-founder of WatzThis?, has overseen the development of hundreds of web and mobile projects for customers from small businesses to some of the world’s largest companies. A prolific writer, Chris has authored and co-authored books and articles on a wide range of Internet-related topics including HTML, CSS, mobile apps, e-commerce, e-business, Web design, XML, and application servers. His published books include Adventures in Coding, JavaScript For Kids For Dummies, Writing Computer Code, Coding with JavaScript For Dummies, Beginning HTML5 and CSS3 For Dummies, Webkit For Dummies, CIW E-Commerce Designer Certification Bible, and XHTML.

About Webucator

Webucator provides instructor-led training to students throughout the US and Canada. We have trained over 90,000 students from over 16,000 organizations on technologies such as Microsoft ASP.NET, Microsoft Office, Azure, Windows, Java, Adobe, Python, SQL, JavaScript, Angular and much more. Check out our complete course catalog.