HTML <p> Tag

The <p> tag defines a paragraph of text. It is a block-level element and always starts on a new line. Before and after each paragraph, browsers add margin automatically. You can modify the margins using the CSS margin property .

If you need to just to move text to a new line use the <br> tag.

Syntax

The <p> tag comes in pairs. The content is written between the opening (<p>) and closing (</p>) tags. If the closing tag is omitted, it is considered that the end of the paragraph matches with the start of the next block-level element.

Spaces between the opening <p> tag and its content are ignored by the browser. In order to set an indent, use the CSS text-indent property .
The <p> tag cannot contain tables and other block-level elements.

Example of the HTML <p> tag:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
  </head>
  <body>
    <p>This is a paragraph</p>
  </body>
</html>

Result

Using the CSS

To align a text in a paragraph, instead of the obsolete align attribute, use the CSS text-align property.

Example of the HTML <p> tag used with the CSS text-align property:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      div.paragraph {
        text-align: center;
      }
    </style>
  </head>
  <body>
    <h1>Title of the document</h1>
    <div class="paragraph">
      <p>The text alignment to the center is set with CSS property text-align</p>
    </div>
  </body>
</html>

Example of the HTML <p> tag used with the <br> tag:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
  </head>
  <body>
    <p> Inside the paragraph, we can put the tag &lt;br /&gt;, <br> to transfer a part of the text to another line if necessary.</p>
    <p>Lorem ipsum, or lipsum as it is sometimes known, is dummy text used in laying out print, graphic or web designs. <br/> The passage is attributed to an unknown typesetter in the 15th century who is thought to have scrambled parts of Cicero's De Finibus Bonorum et Malorum for use in a type specimen book.</p>
  </body>
</html>

Attributes

Attribute Value Description
align right
left
justify
Defines the text alignment.
Not supported in HTML5

The<p> tag also supports the Global Attributes and the Event Attributes.

How to style <p> tag?

Common properties to alter the visual weight/emphasis/size of text in <p> tag:

  • CSS font-style property sets the style of the font. normal | italic | oblique | initial | inherit.
  • CSS font-family property specifies a prioritized list of one or more font family names and/or generic family names for the selected element.
  • CSS font-size property sets the size of the font.
  • CSS font-weight property defines whether the font should be bold or thick.
  • CSS text-transform property controls text case and capitalization.
  • CSS text-decoration property specifies the decoration added to text, and is a shorthand property for text-decoration-line, text-decoration-color, text-decoration-style.

Coloring text in <p> tag:

  • CSS color property describes the color of the text content and text decorations.
  • CSS background-color property sets the background color of an element.

Text layout styles for <p> tag:

  • CSS text-indent property specifies the indentation of the first line in a text block.
  • CSS text-overflow property specifies how overflowed content that is not displayed should be signalled to the user.
  • CSS white-space property specifies how white-space inside an element is handled.
  • CSS word-break property specifies where the lines should be broken.

Other properties worth looking at for <p> tag:


Browser support



Do you find this helpful?

Related articles