<p>
HTML <p> Tag
The HTML <p> tag is a block-level element that is used to define a paragraph of text in a web page. It creates a new line before and after the element, taking up the full width of its parent container.This semantic tag is commonly used to structure text content on a web page, such as articles, blog posts, or product descriptions. You can add other HTML tags, such as <strong> or <em>, within the <p> tag to add emphasis or formatting to specific words or phrases within the paragraph.Properly structuring your text content with the <p> tag is important, as it can help improve the accessibility and usability of your web page. Screen readers and other assistive technologies rely on semantic tags like <p> to accurately interpret and present the content to users.
Syntax
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
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 <br />, <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
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:
CSS text-shadow property adds shadow to text. CSS text-align-last property sets the alignment of the last line of the text. CSS line-height property specifies the height of a line. CSS letter-spacing property defines the spaces between letters/characters in a text. CSS word-spacing property sets the spacing between words.