“In this article, we’ll explore HTML paragraphs and how to use them with examples. In HTML, we define paragraphs using the `<p>` tag, which comes with both an opening and closing tag. Anything written between `<p>` and `</p>` is considered a paragraph. While some browsers may interpret a line as a paragraph even without the closing tag, it’s a good practice to always include `</p>` to ensure consistent and expected results.”Certainly, let’s rephrase it to make it more concise and clear:
“In this article, we’ll explore HTML paragraphs and how to use them with examples. In HTML, we define paragraphs using the `<p>` tag, which comes with both an opening and closing tag. Anything written between `<p>` and `</p>` is considered a paragraph. While some browsers may interpret a line as a paragraph even without the closing tag, it’s a good practice to always include `</p>` to ensure consistent and expected results.”
<p> Content </p>
Example 1: In this example, we are using the <p> tag that is used for paragraphs in HTML.
<!DOCTYPE html>
<html>
<body>
<h2>Welcome To GeeksforGeeks</h2>
<!– Use of <p> tag –>
<p>A computer science portal for geeks.</p>
</body>
</html>
Example 2: This example explains the HTML <p> tag.
<!DOCTYPE html>
<html>
<body>
<p>A Computer Science portal for geeks.</p>
<p>It contains well written, well thought articles.</p>
</body>
</html>
Key Points: When we look at the webpage, we see that there are few spaces added before and after a paragraph. HTML does this by default. Let’s look at a few properties of the paragraph tag:
As already mentioned, the<p>tag automatically adds space before and after any paragraph, which is basically margins added by the browser.
If a user adds multiple spaces, the browser reduces them to a single space.
If a user adds multiple lines, the browser reduces them to a single line.
By default, the display of the Paragraph element is set to “block” which you can change using CSS. This means that if you add another paragraph to the webpage the next paragraph will be inserted in the next line on the webpage.
Example: This example explains the HTML <p> tag having multiple lines.
<!DOCTYPE html>
<html>
<body>
<p>
This paragraph has multiple lines.
But HTML reduces them to a single line,
omitting the carriage return we have used.
</p>
<p>
This paragraph has multiple spaces.
But HTML reduces them all to a single
space, omitting the extra spaces and line we have used.
</p>
</body>
</html>
<br> tag: There is a way to let the HTML know where the browser needs to change the lines by using the <br> tag. These tags do not have any closing tag. So, just a single opening tag will change the line.
Syntax:
<br>
Example: This example explains the <br> tag inside the <p> tag to add the line-break.
<!DOCTYPE html>
<html>
<body>
<p>
This paragraph has multiple
<br />lines. But HTML reduces them
<br />to a single line, omitting
<br />the carriage return we have used.
</p>
</body>
</html>
Align attribute: The <p> tag specifically supports the alignment attribute and allows us to align our paragraphs in left, right, or center alignment.
<p align=”value”></p>
Example: This example explains the align attribute to align the content in the <p> tag.
<!DOCTYPE html>
<html>
<body>
<p align=”center”>
Welcome Geeks
</p>
<p align=”left”>
A Computer Science portal for geeks.
</p>
<p align=”right”>
It contains well written, well thought articles.
</p>
</body>
</html>
<pre> tag: We have seen how the paragraph tag ignores all the changes of lines and extra spaces within a paragraph, but there is a way to preserve this by the use of the <pre> tag. It also contains an opening and a closing tag. It displays a text within a fixed height and width and preserves the extra lines and spaces we use.
Syntax:
<pre> Content </pre>
Example: This example explains the use of the <pre> tag in the <p> tag.
<!DOCTYPE html>
<html>
<body>
<pre>
This paragraph has multiple
lines. But it is displayed
as it is unlike the paragraph
tag.
</pre>
<pre>
This paragraph has multiple
spaces. But it is displayed as it is unlike the paragraph tag.
</pre>
</body>
</html>
Not a member yet? Register now
Are you a member? Login now