Register  |  Login

Question Details    

   Question

Time: 00:12 - Oct 09, 2006     Asked by: jgivoni      Status: Answered      Points: 50   

Why can't I use <div/> instead of <div></div>?

Can anyone explain why these two do not always give the same results:

1.
<div style="background: #c0c0c0; width: 100px; height: 100px"></div>

2. XML empty-tag style:
<div style="background: #c0c0c0; width: 100px; height: 100px"/>

The second one works - until you put another one after it!

Aren't today's browsers supposed to understand the short notation?
Come on, they can even do XSLT (XML style sheet transformation) - and then it works perfectly!
Anything to do with selecting the right DOCTYPE?

Ask a New Question

Become a Quomon Expert

Current Categories

 

Similar Questions


   

Answer Discussion
Answer Discussion
Answer Summaries
Answer Summary
 
Hi there!

Good question. The reason why it may or may not work depending on the browser or what follows in the HTML will depend on the browser and their interpretation/implementation of the standards, commonly referred to as either "strict" or "quirks". There is a good site - http://www.quirksmode.org that can explain some of the nuances of the two different modes in the various popular browsers.

I have a return question...why use an "empty" <div> tag? This tag is supposed to be used as a container for other elements/items on the page. It looks from your code that you're using the <div> as a "block" to create space. You could achieve this with a 1px transparent .gif file contained in the <div> and that way it won't be affected by additional tags following the "empty" <div>.

I doubt you'll get reliable results trying to use the XHTML notation style in all the browsers, so you're probably going to end up changing back to a more "standard" style.

I hope this helps,
Ric

Expert:

rcastagna

Date:

Oct 09, 2006

Time:

21:21

 

Votes: Good (0) | Bad (0)
Login to rate this answer

Yeah, good return question :-)

Basically, my question was - of course - just out of curiosity, not because it was a big problem for me.
However, it becomes something of a problem at times when your code is not working and you have to figure out why. Since I felt, I was using the right notation, it bothered me that browsers didn't agree.

Expert:

jgivoni

Date:

Oct 09, 2006

Time:

21:41

 

Votes: Good (0) | Bad (0)
Login to rate this answer

But if I should answer your question with a more practical approach, I dare say that it is very handy to have empty div's - for instance to create blocks. Or to put something in them later (DHTML).

Another thing: Take the <script> tag in the <head> section:
<script type="text/javascript" src="javascript.js"/>

This would be the obvious way to do it - but it doesn't work either.

My problem is that I was used to doing so, when I used XSL. It worked fine.

Expert:

jgivoni

Date:

Oct 09, 2006

Time:

21:47

 

Votes: Good (0) | Bad (0)
Login to rate this answer

I've noticed that, too...

It seems that the <link /> tag will accept the "short" XHTML convention, but <script> won't. There must be something with the tag specification/standards, that won't allow something that should be a container to be short-handed. Of course, the obvious counter to that argument would be the <p /> tag. Oh, well...sometimes I really wonder what on earth is really going on with the "standards" these days.

Expert:

rcastagna

Date:

Oct 09, 2006

Time:

22:16

 

Votes: Good (0) | Bad (0)
Login to rate this answer

Funny thing is, my <div/> element validates to valid XHTML by http://validator.w3.org if I specify this doctype:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11...

Whereas it is NOT valid HTML.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd&qu...

But the browsers don't care which doctype I specify.

Expert:

jgivoni

Date:

Oct 09, 2006

Time:

23:45

 

Votes: Good (0) | Bad (0)
Login to rate this answer

In the SGML declaration of HTML, minimisation (SHORTTAG) is turned on (set to YES), whereas in the SGML declaration of XHTML it is turned off (set to NO). As a result, in HTML, <div/> translates to <div></div>>. If you have <body><div/></body>, this translates to <body><div></div>></body> where the orphan closed chevron is contained directly by the body-tag. As the HTML specification also forbids the body-tag from directly containing data, this will not validate.

For further details, have a read of: http://www.cs.tut.fi/~jkorpela/html/empt...

Hope this was helpful,
Mark

Expert:

JavaDemiGodFromHell

Date:

Jun 19, 2007

Time:

03:01

 

Votes: Good (0) | Bad (0)
Login to rate this answer

Question Answered

This question has been answered, and points have been rewarded to the following experts:

rcastagna: 25
jgivoni: 25

You're welcome however to comment or give additional information or if you wish, you have the ability to write an Answer Summary for this question by clicking on the "Answer Summaries" Tab.


In the SGML declaration of HTML, minimisation (SHORTTAG) is turned on (set to YES), whereas in the SGML declaration of XHTML it is turned off (set to NO). As a result, in HTML, <div/> translates to <div></div>>. If you have <body><div/></body>, this translates to <body><div></div>></body> where the orphan closed chevron is contained directly by the body-tag. As the HTML specification also forbids the body-tag from directly containing data, this will not validate.

For further details, have a read of: http://www.cs.tut.fi/~jkorpela/html/empt...

Click here to see the Answer Discussion that preceded this summary.

Expert:

JavaDemiGodFromHell

Date:

Jun 19, 2007

Time:

03:17

 
 

Login to rate this summary: Good  |  Bad

Respond to this question:

New User

  Email:

Upon submission of this form, you will automatically be registered as a Quomon user and we will send your login information to this address

Registered User

Username:

Password:


Forgotten Password

 

New User

  Email:

Upon submission of this form, you will automatically be registered as a Quomon user and we will send your login information to this address

Registered User

Username:

Password:


Forgotten Password
   

"Psst, Quomon is a great site. Pass it on."     Tell a Friend  |   Link To Us  |   Save to Delicious  |   Digg! Digg it


All Questions


Language Options

English:

www.quomon.com

Español:

www.quomon.es

Sponsors

Questions and Answers Software
Real Estate Postcards
Marketing Fulfillment