Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

span or div

Status
Not open for further replies.

amiw

Programmer
Apr 1, 2003
113
GB
Here's a snippet of code, should I use the span or div element?

.....................
<table>
<tr>
<td colspan="2"><span class="header">Directory</span>
</td></tr>
.................
 
Afaik, the only difference between SPAN and DIV is the default behaviour.

SPAN is the width of it's content, and DIV is the width of it's parent element.

You can edit the properties of each to clone the other.

--Glen :)

Memoria mihi benigna erit qui eam perscribam
 
Span is inline, div is a block level element. Depends what you want to do. If you are defining a division, you should use div, if you are defining a special word in context you should use span. By the looks of it, you should use one of the h1-6 elements or skip the inner element entirely and go with th.
 
Out of interest, what do you call this?

<span style="display:block"></span>

--Glen :)

Memoria mihi benigna erit qui eam perscribam
 
Hi Vragabond, that sounds good advice.
I might go with a H1.

Stormbind what does this mean -
SPAN is the width of it's content, and DIV is the width of it's parent element - have you an example?
 
This is changing the semantics of the element. Come think of it, you could do the following:
Code:
<strong style="display: table;">
 <body style="display: table-row;">
  <p style="display: table-cell;">Cell 1,1</p>
  <a style="display: table-cell;">Cell 1,2</a>
 </body>
 <em style="display: table-row;">
  <body style="display: table-cell;">Cell 2,1</body>
  <table style="display: table-cell;">Cell 2,2</table>
 </em>
</strong>
But I don't think it would really be advisable. Yes you can change the behaviour of every element and style it yourself, but it is hardly worth the trouble or smart for that matter.
 
amiw, what Glen said is really simple way of saying what I said. Span is an inline element -- just like text, images, inputs, bolds and such. As such, span encompasses text that it sorrounds and styles this text but does not affect other elements around it. Div is a block level element, meaning that it creates a block where used. If you would use div in a middle of the line, text in a div would start in a new line, since div made a new block. Similarly, the text following will be displayed below this invisible block. With span, the breaks would not occur, since it is inline and does not create this block. Hope it makes sense.
 
Vragabond thanks, yes it does.
 
should I use the span or div element?
Probably neither. If you're identifying a header in your document there are elements intended to do just that. You can then use CSS to style them how you want them. I'd probably go with this:
Code:
<table>
<tr>
<th colspan="2">Directory
</th></tr>
though using a <h1> (or h2, h3, etc.) may also be appropriate in your context.

-- Chris Hunt
Webmaster & Tragedian
Extra Connections Ltd
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top