Javatpoint Logo
Javatpoint Logo

What is NBSP (Non-breaking Space)?

NBSP, or Non-Breaking Space, is a special tool used in HTML to add blank spaces in the text. It's also called a Hard Space or Fixed Space. NBSP is also used in word processing and programming to insert a space that will not be broken by word wrap. The purpose of NBSP is to prevent the browser from automatically breaking the line when it reaches the end, which may disrupt the flow and appearance of the text.

What is NBSP (Non-breaking Space)

In a word processor, when you type a sentence, the text will automatically wrap to the following line when it reaches the end of that line. However, there are some situations in which you should stop this from happening, such as when a phrase or a number should not be divided across two lines. In these situations, you can utilize NBSP to insert a space that will not be broken by the word wrap. This implies that the phrase or number will remain on the same line even if the line is too long to fit on the screen or page. This can come in handy in several situations, such as legal documents where certain words or clauses must remain together or programming code where a long string of letters must be shown in a particular way.

HTML entities are pre-defined characters reserved for coding purposes. For instance, "<" is an HTML entity representing the less than symbol in HTML. Similarly, NBSP is an HTML entity that adds blank spaces to text.

The blank spaces created using NBSP appear on both the source code and the browser, unlike the common spaces that only appear on the source code. Developers can add multiple blank spaces to text using NBSP, which may make the source code look cluttered and difficult to read.

However, there are alternatives to using NBSP, which can help make the source code cleaner and easier to read. These alternatives are known as whitespace characters, such as " " (space), "\t" (tab), and "\n" (newline). Using whitespace characters can achieve the same effect as NBSP without making the source code look messy.

Why is NBSP used?

Sometimes when we type on a computer, we need to add a space between words or sentences. However, when we look at our text on a website, the space might not look the same as when we typed it. To fix this problem, we can use NBSP.



What is NBSP (Non-breaking Space)

Another problem can arise when we want to keep two words together, such as a number and its unit, but the browser separates them into different lines on smaller devices. This can happen when the browser automatically breaks the line in the middle of a word, making the two words appear on separate lines.



What is NBSP (Non-breaking Space)

special character looks like a regular space but prevents the browser from splitting the words into different lines. We can use it to keep our content neat and easily read, even on different devices.

How to use NBSP?

NBSP is mostly used for two purposes: first, to add additional blank spaces, and second, to prevent browsers from providing automatic line breaks.

The following section will examine how nbsp can be utilized in these two scenarios independently.

1) To insert numerous blank spaces

Let's first see how your code will appear if you attempt to insert blank space without using Nbsp.



What is NBSP (Non-breaking Space)

It is evident from the source code's output that the browser skips over any blank spaces that are added with the spacebar.

Let's now see how we may display the content exactly as it appears in the source code.



What is NBSP (Non-breaking Space)

Here, the output demonstrates the addition of blank spaces in the source code using nbsp.

2) To avoid line breaks

First, let's examine how your code would appear when the browser automatically adds a line break while displaying the text on smaller screens.



What is NBSP (Non-breaking Space)

Here, the output demonstrates how the text would appear on smaller screens, with the line breaks supplied by the browser separating the words "$44" and "/week" into two lines.

Let's now examine how nbsp can be used to prevent this.



What is NBSP (Non-breaking Space)

In situations like these, NBSP is quite helpful since it ensures that the two words are printed on the same line, but with a blank space between them, at the point when it is applied.

HTML Entities for Adding Multiple Blank Spaces:

Using the "&nbsp" HTML entity to add multiple blank spaces in the content may not be ideal, as it can make the code look cluttered. In such cases, there are alternative HTML entities available that one can use instead. The following table lists some commonly used HTML entities that can be used to add multiple blank spaces based on the number of spaces required:

HTML Entity Number of spaces
&nbsp; 1 space
&ensp; 2 spaces
&emsp; 4 spaces


  • Prevents unintended line breaks: NBSP makes sure that every material is shown precisely the way the developer intended it to be, without any unnecessary line breaks. This is very helpful when displaying content that has to display numbers, units, or other characters side by side.
  • Ensures consistent formatting: Using NBSP ensures that content formatting is consistent across all devices and browsers, as it prevents automatic line breaks from occurring.
  • Improves accessibility: For visually impaired users who rely on screen readers, using NBSP can improve the readability of content and prevent it from being read in a confusing or disjointed manner.
  • Supports multilingual content: NBSP can be particularly useful when displaying content in languages that require certain characters to remain together, such as in Chinese or Japanese.
  • Provides greater control over content layout: By using NBSP, developers have greater control over the layout and spacing of their content, allowing them to achieve the desired appearance of their web pages.

Common Mistakes with NBSP

While using NBSP can be very helpful for web developers, some common mistakes can occur. Here are a few examples:

  • Using too many NBSPs: While NBSPs can be useful, too many can make your code look messy and difficult to read. To avoid this, only use NBSPs when necessary to achieve your content's desired appearance.
  • Using NBSPs instead of CSS: In some cases, developers may use NBSPs to achieve the desired spacing or layout of their content when CSS would be a better option. Using CSS allows for greater flexibility and control over the layout of your web pages, so it's important to use it when appropriate.
  • Using NBSPs for responsive design: While NBSPs can help prevent unintended line breaks, they should not be relied upon for responsive design. Instead, use CSS to display your content appropriately on different devices.
  • Need to use the correct syntax: To insert an NBSP into your HTML code, you must use the correct syntax: " ". Please use the correct syntax to avoid errors or unexpected results in your code.'

It's important to be mindful of when and how you use it to avoid these common mistakes when using NBSP. Use it sparingly, only, when necessary, to achieve the desired appearance of your content, and make sure to use CSS for more complex layouts and responsive designs. Additionally, always use the correct syntax when inserting NBSPs into your HTML code. By being mindful of these common mistakes, you can ensure your web pages look polished and professional.

Accessibility Issues with Using NBSP in HTML

While NBSP is a useful tool for formatting text in HTML, it can cause some accessibility issues if not used properly. These issues can affect the user experience, particularly for those with disabilities. Here are some potential issues to be aware of and how to avoid them:

Screen readers may not recognize NBSP: Screen readers are tools people with visual impairments use to navigate websites. However, screen readers may not recognize NBSP as a space, leading to issues with reading the text. To avoid this, testing your website using a screen reader and ensuring the content is still understandable is important.

Overuse of NBSP can affect readability: While NBSP can create more space between characters and words, overusing it can make the text more difficult to read. For example, using too many NBSPs in a row can create long, unwieldy lines of text that are hard to follow. To avoid this, use NBSP sparingly and only when necessary.

NBSP may not adjust to different screen sizes: As was already mentioned, NBSP can ensure that words stay on the same line and prevent problems with responsive design. A website's text may need to adjust when seen on a smaller device, like a phone or tablet, which could result in unsightly line breaks and unintelligible content. Testing your website on various screen sizes is essential to preventing this, and the use of NBSP should be adjusted accordingly.

Non-breaking spaces can be confusing for keyboard-only users: Websites that employ non-breaking spaces may require assistance for keyboard-only users, such as those who depend on assistive technology or have motor disabilities. This is because they will correctly understand crucial information because they need assistance to tab through the content. It's crucial to make sure the material can still be navigated with just a keyboard to prevent this.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA