views:

756

answers:

11

What is the difference between <p> and <div>?

Can they be used interchangeably? What are the applications?

+3  A: 

DIV is a generic block level container that can contain any other block or inline elements, including other DIV elements, whereas P is to wrap paragraphs (text).

bdl
+2  A: 

Think of DIV as a grouping element. You put elements in a DIV element so that you can set their alignments

Whereas "p" is simply to create a new paragraph.

Suraj Chandran
+2  A: 

<p> represents a paragraph and <div> represents a 'division', I suppose the main difference is that divs are semantically 'meaningless', where as a <p> is supposed to represent something relating to the text itself.

You wouldn't want to have nested <p>s for example, since that wouldn't make much semantic sense (except in the sense of quotations) Whereas people use nested <div>s for page layout.

According to Wikipedia

In HTML, the span and div elements are used where parts of a document cannot be semantically described by other HTML elements.

Chad Okere
+50  A: 

They have semantic difference - a <div> element is designed to describe a container of data whereas a <p> element is designed to describe a paragraph of content.

The semantics make all the difference. HTML is a markup language which means that it is designed to "mark up" content in a way that is meaningful to the consumer of the markup. Most developers believe that the semantics of the document are the default styles and rendering that browsers apply to these elements but that is not the case.

The elements that you choose to mark up your content should describe the content. Don't mark up your document based on how it should look - mark it up based on what it is.

If you need a generic container purely for layout purposes then use a <div>. If you need an element to describe a paragraph of content then use a <p>.

Note: It is important to understand that both <div> and <p> are block-level elements which means that most browsers will treat them in a similar fashion.

Andrew Hare
I wish I could give +2 just for that bolded sentence. I couldn't count the number of times I'd tried to drill that into the heads of the content producers at my previous job.
nickf
You're quite right, but your site won't explode if you don't use them properly. That said, if I saw another developer using `p`s where they should be using `div`s I'd scratch my head and think they were on crack.
Mark
+1, superb answer.
rahul
+1, *great* answer. I have to take issue with one sentence, though: "*If you need a generic container purely for layout purposes then use a `<div>`.*" This contradicts your earlier advice: "*Don't mark up your document based on how it should look - mark it up based on what it is.*" I would rather say: "If you need a generic container purely for layout purposes then make a PDF."
Jörg W Mittag
@Jörg - Good point! I have edited my answer to take that part out. That just goes to show how easy it can be to conflate semantics and presentation - even in a discussion that is focused on it! :)
Andrew Hare
My experience has been that if you want to layout some piece of content differently than some other piece of content, those two are semantically distinct and thus should be marked up indepedently *anyway*. If OTOH, they *aren't* semantically distinct, then they should probably be layed out together. In either case, there is no need to add an element for layout purposes, because in the first case it should have already been there to begin with and in the second case there is no layouting going on. But then again, I'm a purist and my sites always look like flashbacks to 1995.
Jörg W Mittag
+1, Good Answer.
waheed
+1  A: 

A p tag is for a paragraph, generally used for text. A div tag is for division, and generally used for creating sections of text.

Daniel A. White
+1  A: 

'p' is semantically used for text, paragraphs usually.

'div' is used for a block or area in a webpage. For example it could be used to make the area of a header.

The could probably be used interchangebly, but you shouldn't.

vectran
+13  A: 

<p> indicates a paragraph and has semantic meaning.

<div> is simply a block container for other content.

Anything that can go in a <p> can go in a <div> but the reverse is not true. <div> tags can have block-level elements as children. <p> elements cannot.

Tae a look at the HTML DTD.

<!ENTITY % inline "#PCDATA | %fontstyle; | %phrase; | %special; | %formctrl;">
<!ENTITY % block
     "P | %heading; | %list; | %preformatted; | DL | DIV | NOSCRIPT |
      BLOCKQUOTE | FORM | HR | TABLE | FIELDSET | ADDRESS">

<!ENTITY % flow "%block; | %inline;">

<!ELEMENT DIV - - (%flow;)*            -- generic language/style container -->
<!ELEMENT P - O (%inline;)*            -- paragraph -->
cletus
+3  A: 

The only difference between the two elements is semantics. Both elements, by default, have the CSS rule display: block (hence block-level) applied to them; nothing more (except somewhat extra margin in some instances). However, as aforementioned, they both different greatly in terms of semantics.

The <p> element, as its name somewhat implies, is for paragraphs. Thus, <p> should be used when you want to create blocks of paragraph text.

The <div> element, however, has little to no meaning semantically and therefore can be used as a generic block-level element — most commonly, people use it within layouts because it is meaningless semantically and can be used for generally anything you might require a block-level element for.

Link for more detail

Sam Rudolph
There's no need for that last sentence. Almost all the questions on this site could probably be googled for, but that's not the point. The point is to make a resource for developers with the same questions in the future.
nickf
ya,but he could have start a discussion with some relevant info ,any way will edit it
Sam Rudolph
+2  A: 

It might be better to see the standard designed by W3.org. Here is the address: http://www.w3.org/

A "DIV" tag can wrap "P" tag whereas, a "P" tag can not wrap "DIV" tag-so far I know this difference. There may be more other differences.

Hoque
+5  A: 

All good answers, but there's one difference I haven't seen mentioned yet, and that's how browsers render them by default. The major web browsers will render a <p> tag with margin above and below the paragraph. A <div> tag will be rendered without any margin at all.

ceejayoz
A: 

I dont know my answer will help or not.The previous code was

<p class='item'><span class='name'>*Scrambled eggs on crusty Italian ciabatta and bruschetta tomato</span><span class='price'>$12.50</span></p>

So I have to changed it to

<div class='item'><span class='name'>*Scrambled eggs on crusty Italian ciabatta and bruschetta tomato</span><span class='price'>$12.50</span></div>

was the easy fix.And the CSS for the above code is

.item{
position:relative;
border: 1px solid green;
height:30px;
}

.item .name{
position:absolute;
top:0px;
left: 0px;
}

.item .price{
position:absolute;
right: 0px;
bottom: 0px;
}

So div tag can contain other elements. P should not be forced to do that.

Kazi T Ahsan
I really don't know what you mean. “In the site”: which site? “…to make it work”: what does ‘work’ mean? What's the intended rendering?
Marcel Korpel
Hello Marcel,I have edited my reply.
Kazi T Ahsan
This sounds completely illogical to me: `p` elements *can* contain other inline elements (the HTML 5 spec talks of [phrasing elements (see 7.3)](http://dev.w3.org/html5/markup/common-models.html)), of which `span` is one of them, among many others. Moreover, this is not an answer to the OP's question: it doesn't tell anything about the difference between `p` and `div`. And please read [How do comment replies work?](http://meta.stackoverflow.com/questions/43019/how-do-comment-replies-work)
Marcel Korpel