/ factorpad.com / tech / html5 / reference / html-p-tag.html
An ad-free and cookie-free website.
Beginner
The <p>
tag is so easy to learn
because it has no attributes unique to it alone. Of course global
attributes apply, as they do with all elements.
Another simplification is that in HTML5, paragraph styling is set
with Cascading Style Sheets (CSS) instead of inside each
<p>
element.
The <p>
tag sits inside the
<body>
element, which means it
is in the visible part of the document.
Its purpose is to create a block of text that shares the same styling
as other <p>
elements on the
page.
As a block-level element, the paragraph element takes up the whole space of its parent element after starting on a new line. This behavior is unlike inline-element that start anywhere in a line.
Normally, the <p>
element holds
other inline elements and are as much about setting the structure, or
the "flow" of a document, as they are about displaying text.
For the <p>
tag in HTML5 it is
advised to use a closing
</p>
tag.
Because many HTML authors often forget the closing
</p>
tag, most browsers will
render the document properly without it.
However, since it is easier to close this tag instead of learn the
rules where it need not be closed, it is better to close it. Many
document validators, like the
W3C Markup Validation Service (opens in a new browser)
are more strict than browsers and will warn you to close this
<p>
element.
The text visible on the web page sits between the open and close tags.
The <p>
element includes no
unique arguments other than global arguments.
Horizontal whitespace is limited to one character in most
browsers, so use the <pre>
tag
if you wish to preserve whitespace. Vertical whitespace is trimmed.
HTML code input as above will output the paragraphs shown below.
Carriage returns, extra spaces and text that wraps across several lines will be trimmed within a paragraph element. This paragraph demonstrates how spacing between two paragraphs can look, depending on CSS settings. Notice extra space between each paragraph? As for coding style, multi-line paragraphs are often coded with opening and closing tags on different lines like this. On single lines this format is okay. |
As with all tags, Global Attributes can always be set.
The <p>
element doesn't
have any of its own attributes. That said, the
id
global attribute often is used to
identify an element by name.
Each user-agent (browser) has its own stylesheet per se. This dictates
how that browser styles each element by default. The Chrome browser
styles the <p>
element using the
following specifications.
The following HTML code will create a paragraph block with the style
attributes specified in a CSS document. Here we also assign the global
id
attribute and call it "opening".
Type or copy the lines above into a text file with the .html extension and open it in a browser to see the result.
The id
attribute can be used to create
a hyperlink to that section using an
<a>
tag. See
a tag to learn
about how to anchor a hyperlink.
Also using JavaScript the developer can make automated changes to this named "opening" paragraph based on an action taken by the user, like pushing a button to replace text.
The <p>
tag sits within the body
of the HTML document.
See what else you can learn for free at our YouTube Channel. For reminders follow @factorpad on Twitter or join our no-spam email list.
<meta>
tag, click Back.<title>
, click Next./ factorpad.com / tech / html5 / reference / html-p-tag.html
A newly-updated free resource. Connect and refer a friend today.