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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Difference between SPAN and DIV tags?

Status
Not open for further replies.

Jimuniguy

Technical User
Mar 6, 2002
363
0
0
GB
Hi,

Not a technical question as such, but whats the difference between the tags, why use one above another and is there anything i should know about them?

Cheers

James
 
SPAN affects only a piece of text while DIV is a block container and affects at least one line of text. //Daniel
 
The major thing:
DIV (like <P>) is a block-level element, while SPAN is inline element. Read in any online CSS guide what is the difference.

Span's implementation in IE sometimes seem to be the same as for block-level which is not correct.
Because of this many developers use spans instead of 'real' block-level elements and then wonder why does their page looks different in other browsers.

 
Here is an example:

Code:
<html>

<span style=&quot;background-color:lightblue&quot;> span </span>
<span style=&quot;background-color:lightgreen&quot;> span </span>
<span style=&quot;text-align:center;background-color:pink&quot;> span </span>

<div style=&quot;background-color:yellow&quot;> div</div>
<div style=&quot;background-color:green&quot;> div</div>
<div style=&quot;background-color:magenta&quot;> div</div>

<br>

<table width=&quot;300&quot; border=&quot;2&quot;>
  <tr>
    <td>
      <span style=&quot;text-align:center;background-color:yellow&quot;> span </span>
    </td>
  </tr>
  <tr>
    <td>
      <div style=&quot;text-align:center;background-color:yellow&quot;> div </span>
    </td>
  </tr>
</table>
</html>
You can put the code into a temporary html file, then view it.

As you can see on the page, &quot;div&quot; takes up an entire line, and &quot;span&quot; only takes up the space of the text.

One (narrow) example of when you might choose div over span is when you need to center text.

&quot;text-align:center&quot; centers text within a specificed area, as represented by the background color.

When the center attribute is applied to &quot;div&quot;, the text centers on the page/(in the container) as excepted. But when the center attribute is applied to &quot;span&quot;, the text remains (visually) unchanged.

I hope this helps, -Dave
 
As far as I know besides the fact that <DIV> causes a line break and <SPAN> doesn't, <DIV> can act over block level elements like <P> etc while <SPAN> can't
 
As far as I know besides the fact that <DIV> causes a line break and <SPAN> doesn't, <DIV> can act over block level elements like <P> etc while <SPAN> can't
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top