Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying an element similar to p inside a span?

Tags:

html

css

I have a span with an inculded p, like this:

<span>
    <p>this is the p</p>
</span>

This is not allowed according to the HTML5 specs. Now, my HTML is more complex than the above and I need something similar to a p which I can use instead. Which tag could I use or which element can I change using css so it behaves like a p?

like image 584
user1856596 Avatar asked Aug 28 '13 15:08

user1856596


People also ask

Can we put P tag inside span?

This is not allowed according to the HTML5 specs.

What elements can go inside a span?

The span element is an inline element, which should contain only other inline elements and no block elements. From the spec: Generally, block-level elements may contain inline elements and other block-level elements.

Can you have a P inside a div?

In HTML 4, the DIV element cannot be inside another block-level element, like a P element. However, in HTML5 the DIV element can be found inside and can contain other flow content elements, like P and DIV.

How do you add a style to a span tag?

How to style text with the span tag. If you want to makes some particular text or any other content different from the rest, you can wrap it in a span tag, give it a class attribute, then select it with the attribute value for styling.


1 Answers

p has a meaning.

If your content matches p’s definition, you should use p. Then you should use a div instead of a span (unless there is no other suitable candidate):

<div>
  <p>…</p>
</div>

If your content doesn’t match p’s definition, you shouldn’t use p. Then you could use span instead of p (if there is no other suitable candidate):

<span>
  <span>…</span>
</span>

span and div don’t have meanings. So you only have to consider where they are placed syntactically (block vs. inline). You can swap them without losing/changing any semantics:

<div>
  <span>…</span>
</div>

<div>
  <div>…</div>
</div>

Note that all this doesn’t have anything to do with how these elements are styled. You can style a block-level element with CSS so that it appears as inline and vice-versa: display:inline; resp. display:block;.

like image 65
unor Avatar answered Sep 22 '22 10:09

unor