How to make spaces in html

HTML Space – How to Add Spaces in HTML

Quincy Larson

Quincy Larson

HTML Space – How to Add Spaces in HTML

Adding a space to your HTML can be deceptively difficult. And there are at least 5 of ways to go about doing this.

This tutorial will show you several examples. It will also show you how to use fancy versions of space, too.

You can do all this in raw HTML, without the need for CSS. But be advised that CSS is the preferred way to add space to your HTML. And freeCodeCamp has a ton of tutorials on how to accomplish this using the CSS box model.

What is the ASCII character for a space?

The ASCII character code for a space is 20. But this is just the standard way. There are several

There are 5 types of spaces in HTML that you can use. To the naked eye, they look the same but they serve slightly different purposes.

And there is also the Tab Character, which represents pressing the tab key on your keyboard. And the Carriage Return Character, which represents pressing the enter key on your keyboard.

+---------------------+-----------+ | Character | HTML Code | +---------------------+-----------+ | Non-breaking space |   | | En space |   | | Em space |   | | Thin space |   | | Standard space |  | | New Line (Return) | 
 | | Tab Character | 	 | +---------------------+-----------+

How wide is a space character?

There are four common widths for space characters:

  1. Standard-width space. This is also called «non-line breaking space» because it will not cause a line break (AKA carriage return).
  2. Em space. This is called «Em» because it’s as wide as the letter M is in whichever typeface you’re using. (If you’ve heard the term em-dash, this is a dash as wide as the letter M.)
  3. En space. This is called «En» because it’s as wide as the letter n is in your typeface.
  4. And finally, there’s «Thin space», which is the thinnest of all spaces.

What is the symbol for space in HTML?

The most commonly-used HTML entity is 

You can try throwing this text to force it to render a space.

For example, let’s say you want to put two spaces after a sentence, but something else in the website rendering engine is automatically removing one of the spaces. You may be able to type  to add two spaces.

Is space a non-ASCII character?

No. Space is an ASCII character. It’s ASCII value is 20, and you can type it like this: 

How do I make white space in HTML?

You may want to use CSS to center your HTML elements instead of hard coding spaces.

But if you just want a quick and dirty way to create whitespace, and push text around, you can use the same space character over and over like this:

[The text you want to add trailing whitespace to]      [the text you want to add trailing whitespace to]

What character looks like a space but isn’t?

There are two characters that look like spaces but aren’t:

  1. The New Line character – also known as the «carriage return». The HTML code for newline character is:
  2. The Tab Character, which is what you get when you press the tab button in a text field. The HTML code for Tab Character is:

I hope this tutorial has been helpful. Go forth and make space. 🚀

Источник

HTML Space – How to Add a Non-breaking Space with the   Character Entity

Kolade Chris

Kolade Chris

HTML Space – How to Add a Non-breaking Space with the   Character Entity

In HTML, you can’t create an extra blank space after the space ( ) character with the spacebar. If you want 10 blank spaces in your HTML code and you try to add them with the spacebar, you’ll only see one space in the browser.

Also, one or more of the words that are supposed to be together might break into a new line.

So, in this article, I will show you how to create any number of blank spaces you want in your code, and how to add a non-breaking space with the   character entity.

First, What Are Character Entities?

Character entities are reserved for displaying various characters in the browser.

For instance, the less than symbol ( < ) and greater than symbol ( >) are reserved for tags in HTML. If you want to use them in your code, HTML might mistake them for opening and closing tags.

If you want to use them as «greater than» and «less than», you need to use their respective character entities ( < and > ). Then you can safely display them in the browser.

How to Add Non-breaking Spaces in HTML with  

Since the browser will display only one blank space even if you put millions in your code, HTML has the   character entity. It makes it possible to display multiple blank spaces.

Without the   character entity, this is how your code would look:

 

Lemurs are primates found exclusively in the isolated island of Madagascar. Lemurs are primates just like apes and monkeys, but they evolved independently and are unique. The numbers of lemurs are dwindling due to poaching and other destructive human activities. Lemurs are worth more than $2 Trillion. In fact, no amount of money can ever buy one. So, protect lemurs!

I have added some CSS to make the HTML clearer and to make it easier to see what I’m trying to show:

without-nbsp

In the HTML code below, I inserted some   character entities to create multiple blank spaces:

 

Lemurs           are primates found exclusively in the isolated island of Madagascar. Lemurs are primates just like apes and monkeys, but they evolved independently and are unique. The numbers of lemurs are dwindling due to poaching and other destructive human activities. Lemurs are worth more than $2 Trillion. In fact, no amount of money can ever buy one. So,         protect lemurs!

one-space-with-nbsp

You can see there are 5 blank spaces between the first two words, and 4 between the antepenultimate and penultimate words. That’s because I inserted 5 and 4   characters, respectively.

Without the   character entity, that wouldn’t be possible.

What if you want a bunch of spaces in your code?

What if, for instance, you want 10 blank spaces in your code? Writing   10 times would be redundant and boring.

Instead, HTML provides the   character entity for 2 non-breaking spaces, and   for 4 non-breaking spaces.

 

Lemurs     are primates found exclusively in the isolated island of Madagascar. Lemurs are primates just like apes and monkeys, but they evolved independently and are unique. The numbers of lemurs are dwindling due to poaching and other destructive human activities. Lemurs are worth more than $2 Trillion. In fact, no amount of money can ever buy one. So,     protect lemurs!

In the code above, I inserted 5 blank spaces between the first two words by using   once (4 spaces) and   once (1 space). Then I used 2 &ensp entities between the antepenultimate and penultimate words. So, the number of blank spaces remain the same as in the first example:

mutiple-blanks-pace

Why would you need a non-breaking space in your code?

Sometimes, HTML might break up words that are supposed to be together into another line – for example, initials, units, dates, amount of money, and more.

The   character entity prevents this from happening. When you insert the   character between such words, it will render a space and will never let any of the words break into a new line.

In the HTML code below, I have some information about lemurs – the beautiful primates found in Madagascar:

  

Lemurs are primates found exclusively in the isolated island of Madagascar. Lemurs are primates just like apes and monkeys, but they evolved independently and are unique. Th numbers of lemurs are dwindling due to poaching and other destructive human activities. Lemurs are worth more than $2 Trillion. In fact, no amount of money can ever buy one. So, protect lemurs!

I have some CSS to make it clearer and show what I’m trying to show:

breaking-space

The result looks like this:

You can see that the $2 Trillion breaks, which does not look good as it might confuse the reader.

The   character entity forces the two words together:

 

Lemurs are primates found exclusively in the isolated island of Madagascar. Lemurs are primates just like apes and monkeys, but they evolved independently and are unique. Th numbers of lemurs are dwindling due to poaching and other destructive human activities. Lemurs are worth more than $2 Trillion. In fact, no amount of money can ever buy one. So, protect lemurs!

non-breaking-space

Conclusion

You have seen that with the   ,   , and the   character entities, you can display blank spaces in the browser. This isn’t possible just using the spacebar key.

You can also use the   character entity in specific places to prevent words that should stay together from breaking into the next line.

Thank you for reading, and keep coding.

Источник

Читайте также:  Чем отличается оператор break от оператора continue php
Оцените статью