In HTML, sometimes we need to create space, and it’s not as easy as just using the spacebar to create it. This is especially true if we need multiple spaces in a row or tabs – HTML tends to collapse those multiple spaces to one space. This is where HTML entities come in. In this article, we take a look at the different HTML entities that allow us to create space of all different sizes.
There are four main types of spaces we can work with in HTML elements: thin space, non-breaking space, en space, and em space. The code editor below outlines the differences in size between the four types:
<!DOCTYPE html> <!-- <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title></title> <style> span { background: pink; } <style> body { font-family: Arial } </style> </head> <body style="font-family:Arial;"> <h1>Differences in Sizes of Space Entities in HTML</h1> <!-- At beginning of sentences --> <p style="font-size:16px;text-decoration:underline;font-weight:bold;"> Beginning of Sentences</p> <p><span> </span>I am a sentence.</p> <p><span> </span>I am a sentence.</p> <p><span> </span>I am a sentence.</p> <p><span> </span>I am a sentence.</p> <!-- in between words --> <p style="font-size:16px;text-decoration:underline;font-weight:bold;"> In Between Words</p> <p>I<span> </span>am<span> </span>a<span> </span>sentence.</p> <p>I<span> </span>am<span> </span>a<span> </span>sentence.</p> <p>I<span> </span>am<span> </span>a<span> </span>sentence.</p> <p>I<span> </span>am<span> </span>a<span> </span>sentence.</p> </body> </html>
The different space types can serve different purposes. In this code example, we look at the differences between a standard space using the spacebar vs using an HTML entity.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>repl.it</title> </head> <body style="font-family:Arial;"> <div> <strong>Using nbsp; example</strong> <br/> <br/> <div style="width:50px;height: 50px;border:1px solid red;margin:10px;">Dr. John Doe</div> <div style="width:50px;height: 50px; border: 1px solid red;margin:10px;">Dr. John Doe</div> <div style="width:50px;height: 50px;border:1px solid red;margin:10px;">Reno, NV</div> <div style="width:50px;height: 50px; border: 1px solid red;margin:10px;">Reno, NV</div> </div> <div> <strong>Using emsp; example</strong> <br/> <br/> <div style="border:1px solid red;margin:10px;"> <p>Here are some quotes.</p> <p> <em> —To be or not to be, that is the question...</em></p> <p> <em> —This above all: to thine own self be true...</em></p> <p> <em> —What's in a name? That which we call a rose By any other word would smell as sweet...</em></p> </div> <div style="border: 1px solid red;margin:10px;"> <p>Here are some quotes.</p> <p> <em> —To be or not to be, that is the question...</em></p> <p> <em> —This above all: to thine own self be true...</em></p> <p> <em> —What's in a name? That which we call a rose By any other word would smell as sweet...</em></p> </div> </div> </body>
When you have a phrase, name or number that should not be wrapped to the next line, use an nbsp character entity. When you have a block of text that needs to be emphasized in it’s own line or paragraph, use an emsp character entity.
This table outlines the codes that can be used with different size spaces:
Any of these codes work for their respective space size/type. Browser support is fairly widespread for Entity Names, but if it turns out to be not the case, Hex and Dec codes work as well.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.