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..
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.
<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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With