What is the difference between <p> and <div>?
Solution 1:
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.
Solution 2:
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.