When should I use &nbsp in HTML, and how is it different from regular spaces?

I often see &nbsp used in HTML, and I understand it creates some whitespace, but I’m not exactly sure how it works or when it’s appropriate to use it.

Is &nbsp just a non-breaking space, or does it serve another purpose? In what scenarios is using &nbsp better than a regular space, for example, in layout, formatting, or preventing line breaks?

Would appreciate some examples or best practices for using   effectively in HTML.

One practical reason I use &nbsp in HTML is when I want two words to stick together without being split across lines. For example, if I’m writing a name like:

John Doe

This ensures that “John” and “Doe” appear on the same lin, even if they’re at the end of a paragraph or inside a table cell. It’s super helpful in UI labels or when formatting things like “Section 1” to avoid awkward breaks.

I’ve worked on emails and legacy systems where CSS can’t always be relied on. In those cases, &nbsp becomes a fallback trick for adding consistent horizontal spacing.

For instance, when spacing out inline elements:

<button>Yes</button>&nbsp;&nbsp;<button>No</button>

This gives a predictable gap between buttons without relying on CSS margin or padding. It’s not ideal for modern layouts, but &nbsp is still a useful tool in constrained environments.

When doing quick formatting in HTML with monospace fonts (like in code blocks or forms), I’ve found that regular spaces can sometimes collapse or get ignored depending on rendering quirks. That’s when I switch to &nbsp, because it forces the browser to treat the space as a solid character.

Also, if you have text like:

Total:&nbsp;$100.00

This keeps the dollar sign snug next to the label, avoiding any accidental wrap or layout shift. It’s all about ensuring readability and alignment, especially when pixel precision matters.