HTML Paragraphs

The HTML <p> tag is used to create paragraphs. For example,

<p>HTML is fun to learn.</p>

Browser Output

The <p> is used to create HTML paragraph.

As we can see, a paragraph starts with the <p> and ends with the </p> tag.

HTML paragraphs always start on a new line. To demonstrate this, let's create a webpage with two paragraphs.

<p>Paragraph 1: Short Paragraph</p>

<p>Paragraph 2: Long Paragraph, this is a long paragraph with more text to fill an entire line in the website.</p>

Browser Output

Multiple HTML paragraphs

The above HTML code contains two paragraphs. And each paragraph starts on a new line even though there is sufficient space after the first paragraph.

Note: By default, browsers automatically add whitespace (margin) above and below paragraphs. It is possible to change whitespace and other design aspects using CSS.

HTML Paragraphs and Spaces

Paragraphs automatically remove extra spaces and lines from our text. For example,

The paragraph tag removes      all extra spaces.
The paragraph tag also removes all extra lines.

Browser Output

HTML paragraph without extra spaces and new lines

Here, the output

  • remove all the extra spaces between words
  • remove extra lines between sentences

Note: It's possible to add extra spaces and lines in HTML, which we will discuss later in this tutorial.

Adding Line Breaks in Paragraphs

We can use the HTML line break tag, <br>, to add line breaks within our paragraphs. For example,

<p>We can use the <br> HTML br tag <br> to add a line break.</p>

Browser Output

<br> used to add line breaks inside an HTML paragraph

Note: The <br> tag does not need a closing tag like the <p> tag.

Pre-formatted Text in HTML

As we have discussed, paragraphs cannot preserve extra lines and space. If we need to create content that uses multiple spaces and lines, we can use the HTML <pre> tag.

The <pre> tag creates preformatted text. Preformatted texts are displayed as written in the HTML file. For example,

  This text  
    will be 
      in the same manner 
        as it is written

Browser Output

<pre> tag used to add pre-formatted text in a webpage

Other Elements Inside Paragraphs

It's possible to include one HTML element inside another. This is also true for <p> tags. For example,

We can use other tags like <strong>the strong tag to emphasize text</strong>

Browser Output

<strong> tag nested inside a HTML <p> tag

In the above example, we have used the <strong> tag inside the <p> tag.

Browsers automatically make the contents inside <strong> tags bold.

Paragraph is Block-level

The <p> tag is a block-level element. It starts on a new line and takes up the full width (of its parent element).

<p> tag is a block which takes up the entire horizontal space available

Note: Keep note of which elements are inline-level and which are block-level. It will be important later when we learn CSS.

Add Extra Space Inside Paragraphs

As discussed earlier, we cannot normally add extra empty spaces inside paragraphs. However, we can use a certain HTML entity called non-breaking space to add extra spaces. For example,

<p>Extra space &nbsp;&nbsp; inside a paragraph</p>

Browser Output

nbsp HTML entity used to add non breaking space

Here, &nbsp; is an HTML entity, which is interpreted as a space by browsers. This allows us to create multiple spaces inside paragraphs and other HTML tags.