943,982 Members | Top Members by Rank

Ad:
Nov 28th, 2007
0

XHTML empty DIV tags

Expand Post »
Hey all,
I have a div tag in which is a basic place holder, calling CSS to clear some floats of previous div tags. They are currently being used like

HTML and CSS Syntax (Toggle Plain Text)
  1. <div id="clear"></div>

Does my code validate better, or is it better practice to use

HTML and CSS Syntax (Toggle Plain Text)
  1. <div id="clear" />
instead?
Will any browsers have trouble if I do it the second way?
Thanks
Sage
Last edited by sagedavis; Nov 28th, 2007 at 3:09 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 6
Junior Poster in Training
sagedavis is offline Offline
86 posts
since Nov 2007
Nov 28th, 2007
1

Re: XHTML empty DIV tags

I'm not sure what would happen in your case, but you can try it here and see what it does

http://www.w3schools.com/xml/tryit.a...ttprequest_js1

I suggest leaving it as you originally have it, it's not doing any harm that way and it is sure to work with all browsers.
Reputation Points: 10
Solved Threads: 3
Light Poster
CertGuard is offline Offline
48 posts
since Jul 2007
Dec 3rd, 2007
0

Re: XHTML empty DIV tags

You could always just add clear:both to the next div under the floats and that will clear them as well.
Reputation Points: 37
Solved Threads: 1
Posting Whiz in Training
cmills83 is offline Offline
249 posts
since Jun 2004
Dec 4th, 2007
0

Re: XHTML empty DIV tags

The second way does not validate. The div tag is not self-closing. A tag is defined as being either a tag pair, or self-closing. It can't be both.

Many browsers totally ignore a pair of tags with no contents.
Reputation Points: 730
Solved Threads: 181
Nearly a Senior Poster
MidiMagic is offline Offline
3,314 posts
since Jan 2007
Dec 10th, 2007
0

Re: XHTML empty DIV tags

Do not use id to identify the div you're using to clear the floats, because you might be using it several times in your document and your mark-up won't be validated. In fact, you do not even need to use the div to clear them. Use a <span class="clr"></span>

<style>
.clr
{
clear:both;
display:block;
line-height:1px;
font-size:1px;
}
</style>

and that should do the work. I thought of doing it myself, but i'm not getting time to update my website, still using divs to clear float ;-)
Reputation Points: 10
Solved Threads: 1
Newbie Poster
vishwebdesign is offline Offline
13 posts
since Nov 2007
Jun 17th, 2008
0

Re: XHTML empty DIV tags

Now I am completely confused about the empty div tag. I do know that the div creates a line break before and after its rendering but have read where a <br /> element is styled inline with clear:both.

I've seen recommendations to clear floats, using the empty div tag:
HTML and CSS Syntax (Toggle Plain Text)
  1. <div style="clear:both" />
And not closing the div tag.

While within another implementation of the same template, used elsewhere, I see the technique applied below:
HTML and CSS Syntax (Toggle Plain Text)
  1. <div style="clear:both; line-height:1px;"> &nbsp;</div>
Which is correct, although I just read in this topic that the first code example herein will not validate? Or is there an easier way?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
igmuska is offline Offline
1 posts
since Jun 2008
Jun 18th, 2008
0

Re: XHTML empty DIV tags

HTML and CSS Syntax (Toggle Plain Text)
  1. <div style="clear:both; line-height:1px;"> &nbsp;</div><div style="clear:both; line-height:1px;"> &nbsp;</div>
this one wrong means your styles write in div ID properties and write div tag like this
HTML and CSS Syntax (Toggle Plain Text)
  1. <div id="clear"></div>
and use in your clear
Reputation Points: 12
Solved Threads: 34
Posting Whiz
sreein1986 is offline Offline
306 posts
since May 2008
Jun 18th, 2008
0

Re: XHTML empty DIV tags

You can run into some issues with IE7 making a break in your page if you are not turning off the overflow. This is a common method to clear floated elements.

HTML and CSS Syntax (Toggle Plain Text)
  1. .clearfix:after {
  2. content:".";
  3. display:block;
  4. height:0;
  5. clear:both;
  6. visibility:hidden;
  7. }
  8. .clearfix {display:inline-block;}
  9. /* Hide from IE Mac \*/
  10. .clearfix {display:block;}
  11. /* End hide from IE Mac */

Place the class "clearfix" inside the container div that has floated elements. This way, you don't have any breaks in your layout.

HTML and CSS Syntax (Toggle Plain Text)
  1. <div class="clearfix">
  2. <div class="floatedDiv">
  3. This is a floated div
  4. </div>
  5. </div>
Reputation Points: 15
Solved Threads: 4
Junior Poster in Training
rexibit is offline Offline
55 posts
since Jun 2008
Jun 19th, 2008
0

Re: XHTML empty DIV tags

It might depend on the content-type. Application/xhtml+xml would probably render it correctly either way, but IE would not display such a file (at all...it would ask you to open or save it).

If you were using text/html, then it would probably recognize the empty tag as a div tag that's not closed. Or this might just be an IE thing, IDK.

Back to the first tag: not having anything in between the opening and closing tags might also cause some problems.
Reputation Points: 4
Solved Threads: 3
Newbie Poster
icorey is offline Offline
16 posts
since Jun 2008
Jun 19th, 2008
0

Re: XHTML empty DIV tags

xhtml does not allow empty tag pairs.

You can put something between the tags (such as an nbsp), and then overwrite it with the script.
Reputation Points: 730
Solved Threads: 181
Nearly a Senior Poster
MidiMagic is offline Offline
3,314 posts
since Jan 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in HTML and CSS Forum Timeline: CSS template overlapping
Next Thread in HTML and CSS Forum Timeline: Navigation misaligned in mozilla





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC