Home βͺ HTML Tutorials βͺ HTML Paragraph Tag Explained Learn How to Use p Tag with Examples
HTML Paragraph Tag Explained | Learn How to Use '<p>' Tag with Examples !
The HTML '<p>' tag is one of the most commonly used elements for structuring text content. It defines a paragraph in HTML and is used to group sentences or text blocks together.
In this tutorial, we'll cover:
The '<p>' tag in HTML stands for "paragraph." It is used to enclose a block of text, creating a paragraph. The content inside the '<p>' tag is separated from the rest of the content by a space above and below the paragraph.
html syntax β
<p>This is a paragraph of text.</p>
Important: The '<p>' tag automatically adds space before and after the paragraph, but not inside the text itself.
The '<p>' tag always requires both an opening and a closing tag.
output π
This is a paragraph.
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
The closing tag is mandatory in HTML5, though in some cases, browsers might render content even without it (e.g., in cases of improper nesting). The '<p>' tag is a 'block-level element'.
<!DOCTYPE html>
<html>
<body>
<p>This is a paragraph. It takes up the full width of the container and starts a new line.</p>
</body>
</html>
output π
This is a paragraph. It takes up the full width of the container and starts a new line.
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
In the example above, the paragraph will start on a new line and expand to the full width of the container.
The '<p>' tag has a few attributes that help in styling or controlling its behavior, but they are used sparingly. Here's a breakdown:
This attribute was used to align the text within the paragraph.
<!DOCTYPE html>
<html>
<body>
<p align="left">This text is left aligned.</p>
<p align="center">This text is centered.</p>
<p align="right">This text is right aligned.</p>
</body>
</html>
output π
This text is left aligned.
This text is centered.
This text is right aligned.
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Note: The 'align' attribute is deprecated in HTML5. Use CSS to align text instead.
The 'class' attribute is used to apply CSS styles to the paragraph.
<!DOCTYPE html>
<html>
<body>
<p class="highlight">This paragraph has a class applied for styling.</p>
</body>
</html>
output π
This paragraph has a class applied for styling.
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
The 'id' attribute is used to uniquely identify the paragraph element for CSS styling or JavaScript manipulation.
<!DOCTYPE html>
<html>
<body>
<p id="intro">This paragraph has a unique id.</p>
</body>
</html>
output π
This paragraph has a unique id.
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
The 'style' attribute allows you to apply inline CSS directly to the '<p>' tag.
<!DOCTYPE html>
<html>
<body>
<p style="color: blue;">This paragraph text is blue.</p>
</body>
</html>
output π
This paragraph text is blue.
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
> 'Note:' It's better to use external CSS instead of inline styles for maintainability.
Here's a practical example of how to use the '<p>' tag in HTML:
<!DOCTYPE html>
<html>
<head>
<style>
.highlight {
color: red;
font-weight: bold;
}
</style>
</head>
<body>
<p>This is a normal paragraph of text.</p>
<p class="highlight">This paragraph has a class applied, making it bold and red.</p>
<p id="unique" style="font-size: 18px;">This paragraph has a unique id and inline styling.</p>
</body>
</html>
output π
This is a normal paragraph of text.
This paragraph has a class applied, making it bold and red.
This paragraph has a unique id and inline styling.
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
In this example:
The '<p>' tag in HTML is essential for structuring your content and making it readable. Itβs easy to use, but understanding its behavior as a block-level element and how to use attributes effectively will help you create well-structured, clean web pages.
Now, you can confidently use the '<p>' tag and its attributes in your HTML projects!
The '<p>' tag is used to define a paragraph of text content on a webpage.
It is a block-level element that starts on a new line and spans the full width.
Yes, you can use internal, external, or inline CSS to style it.
No, it should not contain other block-level tags like '<div>' or '<p>'.
Proper use of paragraphs improves content readability and user experience, helping your page rank better.