DIV vs SPAN

pmj7's picture

He has: 234 posts

Joined: Nov 2002

While I use both SPAN and DIV, I'm not sure I fully understand when one is better to use than the other. I read the W3C description, but I think there's more to it than that.

http://www.w3.org/TR/1998/REC-html40-19980424/struct/global.html#edef-SPAN

"The DIV and SPAN elements, in conjunction with the id and class attributes, offer a generic mechanism for adding structure to documents. These elements define content to be inline (SPAN) or block-level (DIV) but impose no other presentational idioms on the content. Thus, authors may use these elements in conjunction with style sheets, the lang attribute, etc., to tailor HTML to their own needs and tastes..."

Anyone have a good link to clear things up?

Thanks!

Peter

Touchup image processing applet
Pixel Development Web Design, Photography

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

No link, but this is pretty basic.

DIV is a block level element, as noted above. So is P, and TABLE. There are some special circumstances, but you can put P and TABLE into DIV, and DIV into TABLE, but not DIV into P (and P into TABLE, hygiene issues aside... Wink).

SPAN is an inline level element, like STRONG, A, EM, the deprecated B and I. It is meant to be used within P, TABLE, DIV but can NEVER EVER hold P, TABLE, or DIV.

Other than that, use them wherever, and however, you please.

Good convention indicates that DIV is used to contain discrete sections of the content (and can be nested, wheresas P cannot be). SPAN can also be nested, but if you find you're doing that a lot you may want to rethink your logic re: layout and markup.

dk01's picture

He has: 516 posts

Joined: Mar 2002

Try out:

for div:
http://www.zacker.com/10minguide/l06-1.html#Heading3

and

for span:
http://www.zacker.com/10minguide/l06-1.html#Heading4

Hope that helps. If you have more specific questions then feel free to ask.
-dk

They have: 9 posts

Joined: Nov 2002

By setting a background color for a DIV, the effect will be similar to that of setting the background color for a tablecell. Doing the same for SPAN will only set the bgcolor to the text, and not one pixel more/less.

I don't suffer from insanity. I enjoy every second of it.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.