[html] How to leave space in HTML

I do know that HTML is insensitive to space. But what can I use to make empty spaces between words, or phrases. I have tried <p></p> such kind of tags, but the HTML automatically omit it.

Can somebody give me example codes?

This question is related to html tags

The answer is


“Insensitive to space” is an oversimplification. A more accurate description is that consecutive whitespace characters (spaces, tabs, newlines) are equivalent to a single space, in normal content.

You make empty spaces between words using space characters: “hello world”. I you want more space, you should consider what you are doing, since in normal text content, that does not make sense. For spacing elements, use CSS margin properties.

To get useful example codes, you need to describe a specific problem, like markup and a description of desired rendering.


If you are looking for paragraph indent then you can go for 'text-indent' declaration in CSS.

<!DOCTYPE html>   
 <html>
        <head>
            <style>
                p { text-indent: 50px; }
            </style>
        </head>
        <body>
            <p>This paragraph will be indented by 50px. I hope this helps! Only the first line will be indented.</p>
        </body>
    </html>

After, or in-between your text, use the &nbsp; (non-breaking space) extended HTML character.

  • EG 1 :

    This is an example paragraph. &nbsp;&nbsp; This is the next line.


&nbsp;

This will give you the space you're looking for.


You can preserve white-space with white-space: pre CSS property which will preserve white-space inside an element. https://www.w3schools.com/cssref/pr_text_white-space.asp


You can use &nbsp;, aka a Non-Breaking Space.

It is essentially a standard space, the primary difference being that a browser should not break (or wrap) a line of text at the point that this &nbsp; occupies.


Either use literal non-breaking space symbol (yes, you can copy/paste it), HTML entity, or, if you're dealing with big pre-formatted block, use white-space CSS property.


As others already answered, $nbsp; will output no-break space character. Here is w3 docs for &nbsp and others.

However there is other ways to do it and nowdays i would prefer using CSS stylesheets. There is also w3c tutorials for beginners.

With CSS you can do it like this:

<html>
    <head>
        <title>CSS test</title>
        <style type="text/css">
            p { word-spacing: 40px; }
        </style>
    </head>
    <body>
        <p>Hello World! Enough space between words, what do you think about it?</p>
    </body>
</html>

Use white-space: pre:

_x000D_
_x000D_
<!DOCTYPE html>_x000D_
<html>_x000D_
<span style="white-space: pre">    My spaces   </span>_x000D_
<br>_x000D_
<span>     My spaces   </span>_x000D_
</html>
_x000D_
_x000D_
_x000D_


If you want to leave blank space in HTML while editing the website just use <br /> and copy-paste it under the last one and keep going like that until you have enough space. Hope it helps.


To add non-breaking space or real space to your text in html, you can use the &nbsp; character entity.


The "pre" tag defines preformatted text. It preserves both spaces and line breaks.

<!DOCTYPE html>   
 <html>
        <body>
            <pre>This paragraph will be      pre-formatted. 
I hope this helps! 
All spaces will                    be shown as it is in the original file.
            </pre>
        </body>
 </html>