Forum Moderators: open

Message Too Old, No Replies

Is this valid?

A new way to mark up div tags

         

Hester

9:53 pm on Mar 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A new layout I'm working on has a typical number of div tags in the code. Some divs are also nested. It's not easy to see which closing tag relates to which div, as all you see is
</div>
.

I've seen people put a comment after the closing tag so they know which div it is, like this:


<div id="header">
</div><!-- header -->

But this can look messy and adds to the bytes used for the page.

I think I have a better solution. One that's so simple, I don't know why I didn't think of it before! Just duplicate the id of the div in the closing tag:


</div id="header">

This seems to work fine in Mozilla 1.6, Opera 7.2 and IE6 SP1 on Windows XP. But is it valid! I don't mean does it validate, I mean is it correct use of code. Should the browser care if there is extra information in a closing tag? I also wonder if it crashes any browsers out there, or prevents a page from working!

Also, does it invalidate the XML structure of an XHTML document?

If there are no problems with my approach, I consider it a valuable solution to this problem.

grahamstewart

10:05 pm on Mar 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would say it is definitely not valid. IDs should be unique on a page and this is likely to confuse browsers. Also bear in mind that you probably don't want to specify an id on every div.

Why not just use indenting instead? Like this..


<body>
<div id="header">
<h2>Widgets</h2>
<div>
blah blah blah
</div>
</div>

<p>
blah blah blah
</p>

<ul>
<li>one</li>
<li>two</li>
<li>three</li>
</ul>
</body>

it will use less bytes than your suggestion (use TABs for the indents), it is easier to read and definitely won't make your code invalid.

encyclo

10:08 pm on Mar 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've never seen the technique used before, but I'm pretty sure it's not valid. What's more, a quick test with an XHTML document sent as
application/xhtml+xml
generates an "XML parsing error:not well-formed" message in Mozilla - so I wouldn't recommend it.

Back to the comment tags! ;)

asquithea

10:15 pm on Mar 6, 2004 (gmt 0)

10+ Year Member



No, it's valid in neither XML nor SGML, and no, you shouldn't do it. Read the specs if you doubt me.

ergophobe

11:38 pm on Mar 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




adds to the bytes used for the page.

Well, three bytes per instance. That's not much. If you change to
<div id="head">
</div><!-- head -->

you'll be one byte ahead!

Tom

PS - any perceived pun is purely accidental.

Hester

12:17 am on Mar 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I forgot about id being unique. Also I may confuse the DOM, not to mention a host of parsers.

Might another attribute be used? Perhaps anything can go in the tag.


</div (header)>

I realise I'm stepping outside the realms of normal coding here! :)

encyclo

1:06 am on Mar 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Perhaps anything can go in the tag

No, nothing should go in the closing tag unless you want to risk breaking the page. It's invalid, risky and simply bad practice. I understand your desire to identify closing tags, but really comments are the only way here.

ronin

2:42 pm on Mar 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Why not:

<div id="head">

[...]

<!-- /div id="head" -->
</div>

ricfink

3:13 pm on Mar 7, 2004 (gmt 0)

10+ Year Member



ronin's advice seems the best to me. I would probably put the comment like this:

</div><!--end header-->

either way you can use a regular expression to excise the commented closing tags before posting to the production server.