tags:

views:

3032

answers:

3

Hi,

Is there any reason to use a <div style="display:inline-block"> instead of a <span> to layout a webpage?

Can I put content nested inside the span? What is valid and what isn't?

Thanks!

It's ok to use this to make a 3x2 table like layout?

<div>
   <span> content1(divs,p, spans, etc) </span>
   <span> content2(divs,p, spans, etc) </span>
   <span> content3(divs,p, spans, etc) </span>
</div>
<div>
   <span> content4(divs,p, spans, etc) </span>
   <span> content5(divs,p, spans, etc) </span>
   <span> content6(divs,p, spans, etc) </span>
</div>
+13  A: 

According to the HTML spec, <span> is an inline element and <div> is a block element. Now that can be changed using the display CSS property but there is one issue: in terms of HTML validation, you can't put block elements inside inline elements so:

<p>...<div>foo</div>...</p>

is not strictly valid even if you change the <div> to inline or inline-block.

So, if your element is inline or inline-block use a <span>. If it's a block level element, use a <div>.

cletus
yeah, you can style a span and make it behave just like a div
Dave
I tend to agree that `inline-block` has a closer relationship to `inline` than `block`.
Bob Aman
The original question asked about what is valid, and for validation, `<span>` and `<div>` are indeed different, as `<span>` is an inline element (valid within a `<p>`, for instance), while `<div>` is a block element (not valid within a `<p>`).
Brian Campbell
+7  A: 

If you want to have a valid xhtml document then you cannot put a div inside of a paragraph.

Also, a div with the property display: inline-block works differently than a span. A span is by default an inline element, you cannot set the width, height, and other properties associated with blocks. On the other hand, an element with the property inline-block will still "flow" with any surrounding text but you may set properties such as width, height, etc. A span with the property display:block will not flow in the same way as an inline-block element but will create a carriage return and have default margin.

Note that inline-block is not supported in all browsers. For instance in Firefox 2 and less you must use:

display: -moz-inline-stack;

which displays slightly different than an inline block element in FF3.

There is a great article here on creating cross browser inline-block elements.

moorej
-moz-inline-block does inline-block does not.
moorej
If you want it to display more like inline block in FF3 you should actually use inline-stack as well.
moorej
+1 for the very interesting link. There have been times that inline-block would have solved a number of problems.
Tom
A: 

I know this Q is old, but why not use all DIVs instead of the SPANs? Then everything plays all happy together.

Example:

<div> 
   <div> content1(divs,p, spans, etc) </div> 
   <div> content2(divs,p, spans, etc) </div> 
   <div> content3(divs,p, spans, etc) </div> 
</div> 
<div> 
   <div> content4(divs,p, spans, etc) </div> 
   <div> content5(divs,p, spans, etc) </div> 
   <div> content6(divs,p, spans, etc) </div> 
</div>
JMJ