Register  |  Login



Earn money by sharing your knowledge through Quomon's revenue sharing program

Question

Status: Closed Points: 50 Time: 00:12 - Oct 09, 2006  

jgivoni

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?

Categories:

Answer Discussion
Tutorials

 

Q&A System for Websites and Corporate Collaboration

Advertisement

  • Generates significant organic traffic for websites
  • Saves companies money, resources, and time

rcastagna

Date:: Oct 09, 2006

Time:: 21:21

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

jgivoni

Date:: Oct 09, 2006

Time:: 21:41

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.

jgivoni

Date:: Oct 09, 2006

Time:: 21:47

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.

rcastagna

Date:: Oct 09, 2006

Time:: 22:16

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.

jgivoni

Date:: Oct 09, 2006

Time:: 23:45

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.

JavaDemiGodFromHell

Date:: Jun 19, 2007

Time:: 03:01

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

Question Answered

This question has been closed, 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 a Tutorial in the Tutorial Area.

Answer 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:

Forgot Your Password?

JavaDemiGodFromHell

Date:: Jun 19, 2007

Time:: 03:17

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 tutorial.


Login to rate this tutorial: Good  |  Bad

Answer 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:

Forgot Your Password?

Ask a Question

Have a new question? Ask!

You have 100 characters to use



Top browsers Experts

View More

Rank

Expert

Points

1.

rcastagna

220

2.

oracleofDelphi

100

3.

Garso22

75

4.

ashemann

50

5.

multani.sarbjit

50

6.

Dngrsone

25

7.

john2

25

8.

shashi.programmer

25

9.

xarcus

25

10.

dascope

10

Become an Expert

Register today to share your knowledge with the community and be recognized and rewarded for your contributions.


Register Here




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



Language Options

English:

www.quomon.com

Español:

www.quomon.es