Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use <span> instead <p>?

Tags:

html

As the question indicates, if I have some text that I want to add in the HTML then when should I use <p> and when should I use <span>?

like image 450
Affar Avatar asked Dec 15 '09 15:12

Affar


2 Answers

You should keep in mind, that HTML is intended to DESCRIBE the content it contains.

So, if you wish to convey a paragraph, then do so.

Your comparison isn't exactly right, though. The more direct comparison would be

When to use a <div> instead of a <p>?

as both are block level elements.

A <span> is inline, much like an anchor (<a>), <strong>, emphasis (<em>), etc., so bear in mind that by it's default nature in both html and in natural writing, that a paragraph will cause a break before and after itself, like a <div>.

Sometimes, when styling things — inline things — a <span> is great to give you something to "hook" the css to, but it is otherwise an empty tag devoid of semantic or stylistic meaning.

like image 96
Messy Avatar answered Oct 03 '22 09:10

Messy


Semantically, you use <p> tags to indicate paragraphs. <span> is used to apply CSS style and/or class(es) to an arbitrary section of text and inline elements.

like image 41
cletus Avatar answered Oct 03 '22 09:10

cletus