Introduction
The <p>
element in HTML is used to define paragraphs in a web page. It represents a block of text with some separation from the surrounding content. The <p>
element automatically adds space above and below the enclosed text, making it visually distinct as a paragraph.
Here’s a simple example of using the <p>
element:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Paragraph Example</title>
</head>
<body>
<h1>Main Heading</h1>
<p>This is a paragraph of text. It can contain multiple sentences or lines of content. The <p> element is used to structure and separate text into meaningful blocks.</p>
<p>Another paragraph can follow. Each <p> element creates a new paragraph in the HTML document.</p>
</body>
</html>
Output :
In this example:
- Two
<p>
elements are used to create two separate paragraphs. - The text inside each
<p>
element is automatically formatted with space above and below it, making it visually distinct.
Key points about the <p>
element:
1. Text Separation: It is primarily used to separate and structure blocks of text into paragraphs.
2. Automatic Formatting: Browsers automatically add space above and below the text enclosed in a <p>
element, providing a clear visual separation from other content.
3. Whitespace Handling: Extra whitespace, such as line breaks and spaces, is typically normalized within a <p>
element. This means multiple consecutive spaces or line breaks are treated as a single space.
4. Nesting: The <p>
element cannot contain block-level elements, but it can contain inline elements and text.
5. CSS Styling: Developers can apply CSS styles to customize the appearance of paragraphs, adjusting aspects such as font size, line spacing, and margins.
<style>
p {
font-size: 16px;
line-height: 1.5;
margin-bottom: 10px;
}
</style>
In summary, the <p>
element is a fundamental HTML element for organizing and presenting text content in a structured manner on a webpage. It plays a key role in maintaining readability and visual hierarchy.