HTML Comments, Line Breaks and Spaces

Write an HTML comment as <!-- note -->; browsers skip everything between the markers. Force a line break inside text with <br>, and start a new paragraph with <p>. Browsers squeeze runs of spaces into one, so use &nbsp; for a single fixed space, or the CSS white-space property to keep the spacing you typed.

Quick reference
You wantUseNotes
A note in HTML<!-- note -->Visible in page source, never on screen
A note in CSS/* note */The only comment form CSS has
A line break in a line of text<br>Addresses, poems, lyrics
A new block of text<p> ... </p>Gives spacing you can style
One space that never collapses or wraps&nbsp;Between a number and its unit
Keep every space and newlinewhite-space: pre-wrapStill wraps long lines

Updated

Every case on this page shows the code and what the browser actually draws. Edit any example and the result updates, so you can see why a space vanished or a break did nothing before you change your own page.

HTML comments

A comment starts with <!-- and ends with -->. It can sit between elements or wrap whole blocks to switch them off while you test.

One-line and multi-line comments

Only the two paragraphs are drawn. The comments, including the one that wraps a whole paragraph, leave no trace on screen.

<!-- Opening hours come from the shop's sign -->
<p>Open Monday to Saturday.</p>
<!--
<p>Closed for the holidays.</p>
-->
<p>Sunday by appointment.</p>

Why comments cannot be nested

The first --> closes the outer comment early, so the leftover text and the final --> appear on the page.

<!-- outer start
  <!-- inner note -->
  this part leaks out
-->

Old conditional comments do nothing now

Only Internet Explorer 9 and older read these. Current browsers skip the warning entirely, so the old trick is safe to delete.

<p>Page content.</p>
<!--[if lt IE 9]>
<p>Please update your browser.</p>
<![endif]-->

Line breaks: <br>, <p> or CSS margin

Which to use
ElementMeaningUse it for
<br>A break inside one block of textAddresses, poems, lyrics
<p>A separate paragraphAny new thought or block of text
CSS margin or gapSpace between blocksLayout spacing that changes per screen size
<hr>A change of topicA themed break between sections

An address with <br>

The breaks are part of the address, so <br> is the correct choice here.

<address>
  Harbour Bakery<br>
  14 Quay Street<br>
  Galway H91 E2X4
</address>

Stacked <br> tags versus paragraphs with a margin

Both look similar, but the second version has real paragraphs and a gap you can change in one place.

<div>
  We deliver on weekdays.<br><br>
  Orders after 2pm ship the next day.
</div>
<hr>
<style>.spaced p { margin: 0 0 1.25rem; }</style>
<div class="spaced">
  <p>We deliver on weekdays.</p>
  <p>Orders after 2pm ship the next day.</p>
</div>

Adding spaces: entities

Space entities
EntityCharacterWidthTypical use
&nbsp;U+00A0 no-break spaceNormal space, never wraps10&nbsp;kg, page&nbsp;3
&ensp;U+2002 en spaceHalf an emGaps in lists or menus
&emsp;U+2003 em spaceOne emIndenting a single line
&thinsp;U+2009 thin spaceNarrowBetween digit groups in some styles

Keeping a value and its unit together

In the narrow box, the first line can wrap between 50 and km. The no-break space keeps the second pair together.

<div style="width: 9.5rem; border: 1px dashed #999; padding: 6px">
  <p>The trail is about 50 km long.</p>
  <p>The trail is about 50&nbsp;km long.</p>
</div>

Collapsed spaces versus entities

Five typed spaces turn into one. Each entity keeps its own width.

<p>A     B (five typed spaces)</p>
<p>A&nbsp;&nbsp;&nbsp;B (three &amp;nbsp;)</p>
<p>A&ensp;B (en space)</p>
<p>A&emsp;B (em space)</p>

Keeping spaces and newlines with CSS white-space

white-space values
ValueSpacesNewlinesWraps long lines
normalCollapsedCollapsedYes
nowrapCollapsedCollapsedNo
preKeptKeptNo
pre-wrapKeptKeptYes
pre-lineCollapsedKeptYes
break-spacesKept, and can wrapKeptYes

pre, pre-wrap and pre-line side by side

The same text in three boxes. pre keeps everything but runs off the edge, pre-wrap keeps everything and wraps, pre-line keeps only the line breaks.

<style>
  div { width: 12rem; border: 1px solid #ccc; margin: 0 0 8px; padding: 4px; font: 13px monospace; overflow: hidden }
</style>
<div style="white-space: pre">Item      Qty
Coffee beans, dark roast    2</div>
<div style="white-space: pre-wrap">Item      Qty
Coffee beans, dark roast    2</div>
<div style="white-space: pre-line">Item      Qty
Coffee beans, dark roast    2</div>

Showing text someone typed into a form

Notes from a textarea keep their line breaks when shown with pre-line, without the risk of turning their text into HTML.

<p style="white-space: pre-line">Please leave the parcel at the side gate.
The dog is friendly.
Ring twice.</p>

CSS comments, and why // breaks CSS

/* */ comments in a stylesheet

The commented-out colour is ignored and the green one applies.

<style>
  /* Brand colour for notices */
  .notice { color: #0a7d3c; /* color: red; */ font-weight: 600 }
</style>
<p class="notice">Your order has shipped.</p>

What // does to the next rule

The // line is read as part of a selector, so the whole .warning rule is thrown away and the text stays black.

<style>
  // warning colour
  .warning { color: #b42318; font-weight: 700 }
</style>
<p class="warning">This should be red, but it is not.</p>

10 common mistakes

Mistakes and fixes
MistakeWhat happensFix
Typing several spaces between wordsThey collapse into one&nbsp; or white-space: pre-wrap
Using <br><br> for paragraph spacingSpacing cannot adapt to screen sizeSeparate <p> elements and a margin
Nesting commentsText after the inner --> leaks onto the pageUse one comment, or delete the block
Writing // comments in CSSThe next rule is ignoredUse /* */
Writing <!-- --> in a JavaScript fileSyntax error or odd behaviourUse // or /* */ in JS
Leaving private notes in HTML commentsAnyone can read them in View SourceRemove them or strip comments in the build
Indenting text with rows of &nbsp;Breaks on small screenspadding-left or text-indent
Two hyphens inside a commentOlder validators flag it; it reads poorlyAvoid -- inside comment text
Writing <br> without a slash in JSXThe JSX compiler reports an errorWrite <br /> in React
Expecting newlines from a textarea to showThey collapse into spaceswhite-space: pre-line on the output

When a line break is the right tool

A <br> belongs where the line break is part of the content itself: the lines of a postal address, a verse of a poem, a signature block. In those places a reader would keep the break if they copied the text by hand.

Everywhere else, the break is layout, and layout belongs in CSS. Two paragraphs separated by <br><br> look fine until the design needs more space on desktop and less on a phone. Two <p> elements with a margin can change at any breakpoint, and screen readers announce them as separate paragraphs.

Why spaces collapse, and the four ways around it

The browser's default for normal text is white-space: normal, which merges every run of whitespace into one space and wraps lines where it needs to. That is why indentation in your source never shows on screen.

To keep spaces you can use entities (&nbsp; and the wider &ensp; and &emsp;), a <pre> element, or one of the white-space values pre, pre-wrap, pre-line and break-spaces. Entities suit one or two fixed gaps; the CSS values suit whole blocks of text such as code, poems or user-entered notes.

Comments are public

HTML comments ship to every visitor. They do not appear on the page, but they are in the source, in saved copies and in what search engines download. Never leave passwords, internal addresses, customer names or TODO notes about security holes in a comment.

Most build tools can strip comments for you. Vite and similar bundlers minify JavaScript and CSS comments away; HTML comments in your index file may need an HTML minifier step if you want them gone.

How it works

  1. Find the case that matches your problem in the sections below: comments, breaks, spaces or the white-space property.
  2. Edit the code on the left. The result on the right is a real browser rendering in a sandboxed frame, with scripts switched off.
  3. Copy the version that does what you need into your page.
  4. If spacing still looks wrong, check the ten mistakes near the end of the page; most collapsed-space problems are one of them.

Tips

Frequently asked questions

How do I comment out several lines of HTML?

Put <!-- before the first line and --> after the last. Everything between, across any number of lines, is ignored. You cannot nest one comment inside another, because the first --> ends the whole comment and the rest shows up as text.

Why does my HTML ignore extra spaces?

HTML treats any run of spaces, tabs and newlines in normal text as a single space. That lets you indent your source freely without changing the page. To keep several spaces, use &nbsp; entities or set white-space: pre-wrap on the element in CSS.

Is it <br>, <br/> or <br />?

All three work in HTML. <br> is the plain HTML form. The slash is optional and ignored by the HTML parser, but JSX in React requires the self-closing <br />.

Can I use // comments in CSS?

No. CSS only understands /* */. A line starting with // is not a comment: the parser treats it as broken code and throws away the declaration or even the whole rule that follows, which is why the bug can look random.

Do conditional comments still work?

No. Conditional comments such as <!--[if IE]> were only read by old versions of Internet Explorer and were dropped in Internet Explorer 10. Every current browser treats them as normal comments, so any markup inside them never appears.

Related: HTML links guide, CSS text styling playground, HTML viewer, Text to HTML converter, Markdown cheat sheet.

Start for free or see all free tools. We.Inc is free to start, with no card needed. AI building is on paid plans from $20 a month.

More in Guides

We.Inc is an AI-powered website builder you can resell under your own brand. Launch a branded client dashboard, bill on Stripe Connect, and deliver AI-generated websites in minutes. White-label plans start at $99 a month for 25 client sites, with a 7-day free trial and no per-site fees.

Product

Who It's For

Features

Resources

Company

View Sitemap