473,386 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

DIV and SPAN

i was doing some DHTML stuff and suddenly i realised that DIV and SPAN are
both very similar. So far the only difference i realised is that DIV is a
block element while SPAN is an inline element by default when they are
created.

What else difference is there?
Jul 23 '05 #1
9 1335
[Cross-posted to alt.html; Follow-ups set to alt.html]

On Fri, 15 Oct 2004 00:14:08 +0800, Bern <x@x.com> wrote:
i was doing some DHTML stuff and suddenly i realised that DIV and SPAN
are both very similar. So far the only difference i realised is that DIV
is a block element while SPAN is an inline element by default when they
are created.
They are *always* block and inline, respectively. Applying CSS just makes
them render differently.
What else difference is there?


None, really. They are generic containers used to add some user-defined
structure.

<URL:http://www.w3.org/TR/html4/struct/global.html#h-7.5.4>

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2
Lee
Bern said:

i was doing some DHTML stuff and suddenly i realised that DIV and SPAN are
both very similar. So far the only difference i realised is that DIV is a
block element while SPAN is an inline element by default when they are
created.

What else difference is there?


It's annoying to read text in which the first person pronoun "I"
is not capitalized. It looks too much like an index variable.

You've identified the significant difference.
In some older browsers there were more differences, but as far as
I know, that's no longer true. The intent is that a <DIV> is a
major division of a page, which will naturally contain other elements,
while a <SPAN> is a relatively short span of text within a paragraph.

Jul 23 '05 #3
Ivo
"Lee" wrote
Bern said:
i was doing some DHTML stuff and suddenly i realised that DIV and SPAN
are both very similar. So far the only difference i realised is that DIV
is a block element while SPAN is an inline element by default when they
are created.
What else difference is there?


In some older browsers there were more differences, but as far as
I know, that's no longer true. The intent is that a <DIV> is a
major division of a page, which will naturally contain other elements,
while a <SPAN> is a relatively short span of text within a paragraph.


"The intent"? Really, the OP's question has a clearer answer in it than
this... DIV elements can contain other block elements, while SPAN elements
cannot, even when styled with span{display:block;}. Of course, "can" and
"cannot" depend on your criteria. The w3 validator pointed it out as a major
error on my little testpage just now, but IE has no problem displaying a
block level paragraph in my SPAN...
--
Ivo

Jul 23 '05 #4
Ivo wrote:
The w3 validator pointed it out as a
major error on my little testpage just now, but IE has no problem
displaying a block level paragraph in my SPAN...


Browsers typically perform error correction when authors make errors in
their markup. This is little more then guess work though, and different
browsers guess differently, so it is very unwise to depend on error
correction.

--
David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
Home is where the ~/.bashrc is
Jul 23 '05 #5
Lee
Ivo said:

"Lee" wrote
Bern said:
>i was doing some DHTML stuff and suddenly i realised that DIV and SPAN
>are both very similar. So far the only difference i realised is that DIV
>is a block element while SPAN is an inline element by default when they
>are created.
>What else difference is there?


In some older browsers there were more differences, but as far as
I know, that's no longer true. The intent is that a <DIV> is a
major division of a page, which will naturally contain other elements,
while a <SPAN> is a relatively short span of text within a paragraph.


"The intent"? Really, the OP's question has a clearer answer in it than
this... DIV elements can contain other block elements, while SPAN elements
cannot, even when styled with span{display:block;}. Of course, "can" and
"cannot" depend on your criteria. The w3 validator pointed it out as a major
error on my little testpage just now, but IE has no problem displaying a
block level paragraph in my SPAN...


I find my answer more clear and more complete. Opinions vary.

Jul 23 '05 #6
Ivo
"Lee" wrote
In some older browsers
Which ones?
there were more differences,
Like what?
but as far as I know, that's no longer true.
Since when?
The intent
Whose intent? And why only an intent?
is that a <DIV> is a major division of a page,
How major?
which will naturally contain other elements,
But just as naturally not necessarily, right?
while a <SPAN> is a relatively
Relative to what?
short span of text within a paragraph.

Not images and other inline elements?
I find my answer more clear and more complete. Opinions vary.


No offense, Lee. ;-)
--
Ivo

Jul 23 '05 #7
Lee
Ivo said:

"Lee" wrote
>> In some older browsers
Which ones?
>> there were more differences,
Like what?
>> but as far as I know, that's no longer true.
Since when?

None of the above questions have any importance at all to this question.
Giving that level of detail would have cluttered my answer, reducing the
readability and creating a net decrease in usefulness.

>> The intent
Whose intent? And why only an intent?


The intent of whoever first proposed the <DIV> and <SPAN> tags.
I'm not sure I understand the second question, but if my guess
as to its meaning is correct, they didn't bother to enforce the
usage according to their original intent. That would have added
unnecessary overhead and would have been counter-productive.

>> is that a <DIV> is a major division of a page,
How major?


There is no precise answer to that. Nor should there be.

>> which will naturally contain other elements,
But just as naturally not necessarily, right?


Right. An empty <DIV> is legal, but unnatural. The only
time I've ever used them is when they are intended to hold
other elements that will be created dynamically, in which
case their purpose is still to hold other elements.

>> while a <SPAN> is a relatively
Relative to what?


Relative to the paragraph, of course.

>> short span of text within a paragraph.
Not images and other inline elements?


That's correct. As I understand it, the original intent was
to delimit a span of text in order to specify a different
font or color, etc.
I find my answer more clear and more complete. Opinions vary.


No offense, Lee. ;-)


Tedious amounts of detail do not make an answer more clear, and
don't even necessarily make the answer more complete, in any
useful way.

Jul 23 '05 #8
VK
Leaving W3C academic language out:

div and span both have been created as "neutral containers": they allow to
mark a block of page without any pre-set formatting.
Naturally you can use any tags instead (if you want to be "not like
everybody"), for instance <address> for div and <b> for span, but then
you'll have to kill the associated formatting using CSS.
DIV and SPAN just more convenient (and also they have highest approval for
this purpose from W3C).

The major difference between them is that:
DIV is a neutral *non-transparent* container
SPAN is a neutral *transparent* container

Thus <DIV> cuts off any formatting flow before it, so the text in the block
has generic formatting, so you can start from the scratch.

<SPAN> doesn't cut the formatting flow, so the SPAN block has the same
formatting as the text around, but you can add some extra formating.
Jul 23 '05 #9
VK wrote:
div and span both have been created as "neutral containers": they allow to
mark a block of page without any pre-set formatting.
HTML doesn't do formatting. They mark a section of a page (a block only in
the case of div) without any meaning attached.
Naturally you can use any tags instead (if you want to be "not like
everybody"), for instance <address> for div and <b> for span, but then
you'll have to kill the associated formatting using CSS.
You'll also have to deal with the associated _meaning_.
The major difference between them is that:
DIV is a neutral *non-transparent* container
SPAN is a neutral *transparent* container
err?
Thus <DIV> cuts off any formatting flow before it, so the text in the
block has generic formatting, so you can start from the scratch.


Not true.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<title>Divs do not cut off formatting</title>
<style type="text/css">
div#first {
background-color: black;
color: white;
}
</style>
<div id="first">
First div element.
<div id="second">
Nested div element. <br>
Note that it is still white text on a black background?
No formatting has been cut off.
</div>
</div>

Good explanations as to the difference between div and span have already
been posted, so I will not repeat them.

--
David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
Home is where the ~/.bashrc is
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: deko | last post by:
How to change a link's color based on a php variable? I have a number of links on a page: <p><a href="page1.php">Page 1</a></p> <p><a href="page2.php">Page 2</a></p> <p><a...
13
by: Mikko Ohtamaa | last post by:
From XML specification: The representation of an empty element is either a start-tag immediately followed by an end-tag, or an empty-element tag. (This means that <foo></foo> is equal to...
5
by: Kathryn | last post by:
Good morning! I am having a problem with a span. I have items, of which I only want to show the first X characters on the screen. If the user prints the page, I want the entire item to print....
9
by: developer | last post by:
Does anyone know what is the way IE treats span tags(<span>) and table tags(<tr>, <td>)? Should the <span> tag be encolsed in tds and trs if it placed with other elements that are in a table? Can...
1
by: developer | last post by:
Does anyone know what is the way IE treats span tags(<span>) and table tags(<tr>, <td>)? Should the <span> tag be encolsed in tds and trs if it placed with other elements that are in a table? Can...
2
by: Alex | last post by:
On my page I have a lot string like this: <span onmouseover="callMe(this)" onmouseout="callMe(null)" >abc1</span> <span onmouseover="callMe(this)" onmouseout="callMe(null)" >abc2</span> <span...
5
by: ste.paoletti | last post by:
I have a problem with css I have a this xhtml code: <span> <span> <span/> <input type="radio"/> .... <span/> <input type="button" onclick ="var s=document.createElement('span');...
2
by: ricky | last post by:
Hello, If anyone could help me with this I would highly appreciate it. I've tried everything and nothing works. What I am trying to do is so damn basic and it's just frustrating that it seems...
1
by: msg2ajay | last post by:
hello , i have to pass a 'span' id to the javascript function'. how can i pass that. when i tried like below it is working!!! may the function is in the same page.... eg: ---- ...
5
by: Brent | last post by:
Take this small HTML fragment: span.theClass{float:left;width:100px;cursor:pointer;cursor:hand;} ------------------------ <div> <span id="1" class="theClass">&nbsp;<span> <span id="2"...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.