Skip to content Skip to sidebar Skip to footer

Why Does An Inline Div Behave Differently Than An Inline Span

I've tried to inline div but it does not work. It's hard to explain so see the link below: http://jsfiddle.net/CsS5v/1/

SDFDSDSFDSSFDAFASasf

Solution 1:

you shouldn't use a div inside of an element like that, that is why we have spans.

the <div> closes the <p> tag, because you don't need an ending </p> if it is followed by another block Element. so the <p> is closed and we are all aware that a <p> has a following line break added automatically.

A p element’s end tag may be omitted if the p element is immediately followed by an address, article, aside, blockquote, dir, div, dl, fieldset, footer, form, h1, h2, h3, h4, h5, h6, header, hr, menu, nav, ol, p, pre, section, table, or ul element, or if there is no more content in the parent element and the parent element is not an a element.

{Stolen link from another answer} w3 on Paragraph tags

please check this out on <p> vs <div>

and perhaps this as well

difference between div and span tag

Solution 2:

It looks like this has to do that a <div> can not be inside a <p>, but a <span> can be. So then the <div> get's pushed down because of the margin of the <p>.

The <span> stays inside the <p> so it is not pushed down by the margin of the <p>.

You can see this behavior clearly when using firebug.

Solution 3:

Look into an inspector (Firebug or something).

This is because browser closes while parsing HTML <p> element before <div> because semantics of HTML says that there should not be any <div> in <p

Solution 4:

You can't nest block level elements inside of a <p> - only phrasing content is permitted (which includes a <span> and all other elements in the inline formatting context)

If you inspect your example you'll see that the <div> is automatically removed from outside the paragraph (the <p> acts like a self-closing element) and you end up with two lines boxes (the inline <div> and the <em>) and an empty block level <p> on the next line.

Solution 5:

It is not valid to have a block-level element within a <p> so the browser may quite legitimately close the <p> and open a new one after the </div> when interpreting the markup.

w3 link

The p element

Post a Comment for "Why Does An Inline Div Behave Differently Than An Inline Span"