Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between <p>, <div> and <span> in HTML&XHTML?

Tags:

html

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

Can they be used interchangeably?

Because I am facing problem that, for <span> margin not working but for the <div> and <p> it's working..

like image 355
Dhamu Avatar asked May 08 '13 07:05

Dhamu


People also ask

What is the difference between div and span?

div in HTML. Span and div are both generic HTML elements that group together related parts of a web page. However, they serve different functions. A div element is used for block-level organization and styling of page elements, whereas a span element is used for inline organization and styling.

What is Span and P in HTML?

<span> is an inline tag, a <p> is a block tag, used for paragraphs. Browsers will render a blank line below a paragraph, whereas <span> s will render on the same line.


2 Answers

p and div elements are block level elements where span is an inline element and hence margin on span wont work. Alternatively you can make your span a block level element by using CSS display: block; or for span I would prefer display: inline-block;

Apart from that, these elements have specific semantic meaning, div is better referred for a block of content having different nested elements, p which is used for paragraphs, and span is nothing but an empty element, hence keeping SEO in mind, you need to use right tag for right thing, so for example wrapping the text inside div element will be less semantic than wrapping it inside a p

like image 194
Mr. Alien Avatar answered Sep 20 '22 09:09

Mr. Alien


A <p> should contain paragraghs of text, a <div> is to layout your page using divisions and a <span> allows markup to be styled slightly different, for example within a <p>

This is how they should be used semantically, the styling of them however using CSS is up to you.

like image 29
DavidB Avatar answered Sep 22 '22 09:09

DavidB