Log in

View Full Version : Divs question



Mehok
02-07-2008, 08:27 PM
is it common now if you have a div which is not going to contain anything to be done like this



<div id="" />


instead of this



<div id=""> </div>

BLiZZaRD
02-08-2008, 12:09 AM
Not if you want it to validate.

If there is no CSS involved, then there is no need for empty id either. An empty div, while not exactly bad mark up will through a warning though. It is best to use as such:



<div>&nbsp;</div>

jscheuer1
02-08-2008, 03:23 AM
/>

is not any part of HTML.

Mehok
02-08-2008, 10:33 AM
There is CSS but no text or content form the actual html side of things

BLiZZaRD
02-08-2008, 06:48 PM
Well if you have CSS set up for an empty div, then you can use the class or ID still the same



<div id="empty">&nbsp;</div>

jscheuer1
02-08-2008, 10:14 PM
There is nothing wrong with:


<div></div>

or with:


<div id="whatever"></div>

or with:


<div class="whatever"></div>

regardless of if 'whatever' is defined or not. If you want no class or id associated with the empty tag, use the first option. The only reason that you would need anything in there would be if you are going to use DOM level 2 javascript to replace it. Like:


documentGetElementById('whatever').firstChild.nodeValue='Hello World';

For simple layout purposes though, an empty tag is fine, and may be styled however you like.

BLiZZaRD
02-08-2008, 10:21 PM
There is nothing wrong with:


technically.. no there isn't, except that with correct validating code it does produce a warning. empty elements such as <p> or <div> are not Strict INvalid, but the warning is there for a reason. Adding a space to the div does nothing to the layout and eliminates the warning :)

jscheuer1
02-09-2008, 04:16 AM
This validates (W3c - verbose output):


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>
<body>
<div id="bull"></div>
</body>
</html>

No warning. Excuse the id, I picked it before I read your post. Another thing, a space shouldn't be a layout problem (except perhaps in IE 6 - maybe even 7 - line height is often given even when there is only a space), but a nbsp entity will be unless the style is set to display:none;. An empty div with its clear property set can often be very advantageous in dealing with prior floats in a container with background, in which case its display property should not be none.

BLiZZaRD
02-09-2008, 04:59 AM
My apologies.. I should have been more clear. HTML 4.01 Strict valid coding does NOT discern empty elements. It is the mark up validation WITH Tidy that will through the error ("Trimming empty <div>" )

As stated, it isn't a problem, but some people care about such things, which is why I mentioned that adding the space will eliminate all of that. :D

jscheuer1
02-09-2008, 07:11 AM
AFAIK, Tidy isn't a validator. It's a tool for cleaning up your code, right? Empty tags can often be left over from some content that got moved or removed. If that's the only reason for them being there, they should go. I imagine Tidy warns about them for that very reason, and that it is only a warning because there could be a valid reason (as mentioned in my previous post) for their being there.

BLiZZaRD
02-09-2008, 06:57 PM
Yes tis true, but even the W3C is adding Tidy results to their validation ;)

jscheuer1
02-09-2008, 07:50 PM
Yes tis true, but even the W3C is adding Tidy results to their validation ;)

Not to their validation results, at least not as I understand it. It is only a clearly labeled option:


Clean up Markup with HTML Tidy

I take that to mean exactly what it says. Tidy will not create valid markup out of invalid markup in all, perhaps not even in most cases. It is only a tool (I believe created in association with and promoted on the W3c site, BTW) to clean up messy code. Checking that option seems to do nothing though. That's either because I have very good coding habits, or because the code is simply 'cleaned up' before being validated.

In any case, it has nothing to do with formal validation.

BLiZZaRD
02-10-2008, 05:01 AM
I think it is your very good clean coding habits :)

jscheuer1
02-10-2008, 05:12 AM
I think it is your very good clean coding habits :)

There are those who would beg to differ. I did a test later - don't know why I didn't think of this sooner - using my little demo page from this thread with the empty division. I checked verbose, Tidy, and show source, and there was no mention of the empty division, and it still appeared in the 'show source' view. From this I conclude that either this feature of the validator hasn't been implemented yet, empty divisions are OK by it, or some other cause. I did click on 'Help with Options' - it doesn't even mention the Tidy option. One other thing I did notice was that when I made my selections of the options, then validated, the page came back with all of my selected options still selected - except for the Tidy option.

Mehok
02-10-2008, 02:31 PM
this discussion has turned into if there is a class or not

that is not what i even asked

but so far out of it is

basically just use
<div id=""> </div>

instead of

<div id="" />

jscheuer1
02-10-2008, 03:22 PM
Um, no. But you can just use:


<div></div>

It really depends though, on why you want to use it at all. By itself, it will have no effect on the layout or appearance of the page other than to provide a line break if none was there - just like using:


<br>

would do. However, div may be styled and/or scripted to provide layout and other things for your page that the br tag cannot do.

tech_support
02-11-2008, 05:50 AM
But please don't use:


<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>

Use this:


<div style="margin-top:50px;"></div>