Skip to main content

How to Write a Paragraph in HTML

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Each <p> element is one paragraph. The browser puts space above and below it, and wraps its text to fit the window, ignoring the line breaks you typed in your code.

Syntax​

Syntax
<p><text></p>
PartWhat it does
<p>Starts a paragraph.
<text>The paragraph's text, which can include elements such as strong or a link.
</p>Ends the paragraph.

MDN's p page describes the element.

The Example​

The example has two paragraphs about opening hours. In the code, the second one is split over two lines, to show what the browser does with that line break.

Step 1: Write the Code​

Create a file named index.html in a text editor, such as Visual Studio Code:

index.html, an HTML page with two paragraphs: The office opens at 9:00., and The kitchen closes at 15:00., continued on the next line of code with Lunch is served from 12:00.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Opening hours</title>
</head>
<body>
<p>The office opens at 9:00.</p>
<p>The kitchen closes at 15:00.
Lunch is served from 12:00.</p>
</body>
</html>

Step 2: Open It in a Browser​

Double-click index.html. It opens in your browser:

The page in Chromium with two paragraphs, each on one line: The office opens at 9:00. and The kitchen closes at 15:00. Lunch is served from 12:00.

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.

What Happened​

  • Each p is its own block, with 16 pixels of space above and below it.
  • The line break in the second paragraph disappeared. The browser turned the break and the spaces after it into a single space, so the paragraph is one line, 19 pixels tall, the same as the first.

Common Mistakes​

MistakeWhat happensDo this instead
Pressing Enter in the code to start a new lineThe line break doesn't show on the pageUse br for a new line, or a new p for a new paragraph
Putting a div inside a pThe browser ends the paragraph early: <p>Start <div>block</div> end</p> became <p>Start </p><div>block</div> end<p></p>Use span inside a paragraph, or put the div outside it
Typing several spaces to push words apartThey collapse into one spaceUse CSS for spacing

Troubleshooting​

SymptomLikely causeFix
Text you typed on two lines shows on oneThe browser ignores line breaks in the codeAdd br where the line should break
An empty paragraph appears after a divThe div was inside a p, so the browser closed the p earlyMove the div out of the paragraph

FAQ​

How do I break a line without starting a new paragraph?​

Use <br>. In a test, Line one<br>Line two made the paragraph two lines, 37 pixels tall instead of 19. br has no closing tag.

How do I change the space between paragraphs?​

With CSS: the space comes from the paragraph's margin, which was 16 pixels above and below.

Does a p need its closing tag?​

Close it. Leaving it out is allowed in some places, but the closing tag shows exactly where the paragraph ends, for you and for the browser.

Conclusion​

Put each paragraph in its own p, use br for a line break inside a paragraph, and keep block elements such as div out of it. The browser takes care of the spacing and the wrapping.

Additional Resources​