I am a beginner in HTML and haven't started learning CSS yet. Is there any reason where one would prefer to use <p>
tag instead of <pre>
tag? I mean since <pre>
covers everything <p>
does and more.

- 349,597
- 67
- 533
- 578

- 330
- 1
- 3
- 12
4 Answers
I would use <p>
for paragraphs and <pre>
for pre-formatted text.
They are nothing like each other other than sharing the first letter :-)
From those links:
A paragraph is typically a run of phrasing content that forms a block of text with one or more sentences that discuss a particular topic, as in typography, but can also be used for more general thematic grouping. For instance, an address is also a paragraph, as is a part of a form, a byline, or a stanza in a poem.
The
pre
element represents a block of preformatted text, in which structure is represented by typographic conventions rather than by elements.
-
But what if I write a paragraph inside a pre-formatted text, like using 'enter to go the next paragraph... I apologize if it seeks like a silly question – kolman Nov 12 '16 at 05:38
-
@MeherwanGill Pre-formatted text isn't really intended to be anything other than a context switch from HTML to text. Think of `
` as useful for embedding text-only documents that might have character-based tables and what not.
– Brad Nov 12 '16 at 05:40
Both tags are different i.e. <p>
and <pre>
they consist of there few pre-defined properties. You will find more use of <p>
tag than <pre>
.
You could try that, no use of css
for now as you are working with HTML pre and p tags, css
is used for styling that tag.
<pre>
- is a pre-formated text
, so you get same output as you have assigned to your text, i.e. if there is space in text than same output will be seen on webpage.
<p>
This is Demo Text. This is Demo Text. This is Demo Text. This is Demo Text. This is Demo Text.
</p>
<pre>
This is Demo Text. This is Demo Text.
This is Demo Text. This is Demo Text. This is Demo Text.
</pre>
Default p and pre css properties,
p {
display: block;
margin-before: 1em;
margin-after: 1em;
margin-start: 0;
margin-end: 0;
}
pre {
display: block;
font-family: monospace;
white-space: pre;
margin: 1em 0;
}

- 8,455
- 2
- 22
- 25
The <p>
tag defines a paragraph.
Browsers automatically add some space (margin) before and after each <p>
element while, the <pre>
tag defines pre-formatted text.
Text in a <pre>
element is displayed in a font (usually Courier), and it preserves both spaces and line breaks.
<p>
tag takes the whole space available while <pre>
is usually displayed in fixed width.
-
1All text is displayed in a font :-) You may want to change that to "fixed-width font". – paxdiablo Nov 12 '16 at 05:46
The most noticeable visual distinction between <p>..</p>
and <pre>..</pre>
elements is that when a browser displays text content written inside <pre>
tags the text formatting of the HTML file itself will affect how that text is displayed in the browser.
i.e. when text is wrapped in <pre>
tags newlines and white space from the HTML file will be displayed in the final rendered result.
When a browser parses HTML white space and newlines outside, or inside tags (which includes <p>
tags) will be ignored by the parser (well the first space character between words will be recognised - then subsequent spaces ignored).
<pre>
is an exception to this normal parsing behaviour.
I.e. to show a new line in a <p>
you would need to specifically add a <br>
tag.
As mentioned in the comments below, the primary job of markup tags (HTML is markup) is to describe or classify the content inside, for example how much white space is contained in a HTML file would not necessarily be important to a text to speech screen reader, however whether text is contained inside <p>
tags is meaningful - it tells the screen reader this is part of the main body of text.
Therefore it is important that the tag used is appropriate and matched to its content, visual styling and other "downstream" processing should be considered secondary.
This is because these downstream processing tasks such visual styling, text-to-speech, search engine crawling and indexing etc, rely on the markup to be accurate.
All of these downstream processing tasks will be less reliable and problematic if the markup was not properly formed in the first place.

- 2,094
- 4
- 17
- 29
-
1No, that is not the most important distinction. The most important distinction is that they are used for differentiating types of content. Formatting can be handled with CSS. http://stackoverflow.com/a/219230/362536 – Brad Nov 12 '16 at 05:35
-
1@hobbs I get your argument, but I think it's important to be especially strict and clear about the difference between styling and semantic markup when someone is just starting out in HTML and clearly has no clue about this distinction. The person asking the question doesn't have an understanding yet. – Brad Nov 12 '16 at 05:49
` is for paragraph and `
– Abhijeet Kasurde Nov 12 '16 at 05:32