tags:

views:

1833

answers:

12

What is the difference

<br style="clear:both;"/>

vs

<div style="clear:both;"/>

??

Also, I thought

<div style="clear:both;"/>

is a good way of clearing, but is

<div style="clear:both;"> </div>

a proper way?

+24  A: 

The difference is which other style attributes you inherit. Of course one inherits from <br> and the other from <div>.

Typically <div> has no special style implications other than display:block whereas <br> implies a line-break and some non-zero height (linked to the current font height).

But often (e.g. with the ubiquitous css-reset technique) there is approximentally no difference. Therefore, pick the one that makes the most semantic sense.

[UPDATE]

For closing tags, use <div></div> and not <div/>. Here's why.

Thanks to commentors Sebastian Celis and ephemient for the "closing tag" correction.

Jason Cohen
There is a difference with the closing tags. I would not recommend using the self-closing div tag. See http://dusan.fora.si/blog/self-closing-tags
Sebastian Celis
comment++ Unless you're in XHTML/XML, <div /> has a good chance of not meaning what you think it means.
ephemient
thanks guys; added you and your stuff to the answer.
Jason Cohen
I knew <div /> wouldn't work...just couldn't find the page when I needed it!
Darryl Hein
The real explanation for which <div /> isn't working is because he's sending text/html Content-Type headers. He's using pseudo-XHTML
Ionuț G. Stan
+1  A: 

Well, there is no difference, depending on inherited styles.

This links says some more, and recommends : http://www.positioniseverything.net/easyclearing.html

alexn
A: 

If you're writing (valid) XHTML you don't need to use the closing tag, but self closing div tags don't work in all browsers, so you should still use it. ie your example:

`<div style="clear:both;"> </div>`

This is valid XHTML (see html vs xhtml) but doesn't work in all browsers, so you should stick with the above:

<div style="clear:both;" />

also, For what it's worth the <br> tag is deprecated in favor of the <line> tag (see w3.org entry on the <br/> tag)

Jiaaro
No, I believe you were originally correct. While technically the spec says divs can be self-closing, some browsers do not like it.http://dusan.fora.si/blog/self-closing-tags
Sebastian Celis
hmm... So should I change it back? Also, This is an interesting thread on whether we should even USE xhtml: http://www.webmasterworld.com/forum21/12026.htm
Jiaaro
The entire situation is less than ideal thanks to some browser vendors and their refusal to fully support XHTML. I usually recommend that only tags which are always self-closed in XHTML be self-closed (img, br, input, etc). Everything else should be closed normally.
Sebastian Celis
I don't understand why we're getting downmodded :(
Jiaaro
A: 

This is what I always use:

<style type="text/css">
.clearing {
height: 0;
line-height: 0;
font-size: 0;
clear: both;
overflow:hidden;
}
</style>

And where I need a clearing:

<div class="clearing"></div>

You may also be interested in self-clearing containers: http://www.positioniseverything.net/easyclearing.html

EDIT: Added "overflow:hidden" per the suggestion from another answer poster.

Cory Larson
now i'm wondering why this got a down grade...
bLee
Maybe somebody likes their answer better and voted me down just for the heck of it. Oh well...
Cory Larson
+1  A: 

The only difference that I can think of here is that <div> is a block-level element, while <br> is an inline-level element.

But <br> actually behaves somewhat like a block-level element, other than the fact that it is effected by line-height and font-size CSS properties.

In my opinion, <br style="clear:both;"/> is the more proper way to put a line-break in your page, mostly because it is widely-accepted and easily identifiable by others who may not be familiar with your markup.

Terrapin
+7  A: 

This is the style that I use for clearing:

.Clear { clear: both; height: 0; overflow: hidden; }

Usage:

<div class="Clear"></div>

This will not take up any extra space in the page as the height is zero.

Internet Explorer has a strange idea that the content of each element has to be at least one character high, and another strange idea that each element should be as high as it's content. Using overflow: hidden keeps the content from affecting the size of the element.

Guffa
Why the downvote? If you don't leave a comment explaining why, it's useless.
Guffa
A: 

You do need to be careful about the / on the tag.

I had problems with the slash on the <script> tag terminated by <script language="javascript" src="MyScripts.js" /> way. Although, most xml compliant parsers would accept both.

<script> has to be terminated by </script>

A: 

.clear { clear: both; font-size: 0px; line-height: 0px; height: 0px; overflow:hidden; }

Usage

"br" sometimes has side-effects in Opera and IE browsers, so you should avoid using it

se_pavel
+2  A: 

<br /> will be deprecated in HTML5 so it's best to avoid using them wherever possible. If you need an element to clear previous floated elements then it's best to put it inside a div that has clear both rather than adding an empty <br /> above it. A div is more semantically correct.

Matthew James Taylor
"<br /> will be deprecated in HTML5 [...]." What? No! This is incorrect, http://www.whatwg.org/specs/web-apps/current-work/#the-br-element.
cic
A: 

<br /> is an inline element, where as <div> is a block element. Anyway, I personally prefer to use <hr class="clear">, I feel it is more semantically adequate.

Julien
A: 

All methods are bad. Use CSS to change the appearance of your page. There are many methods to accomplish the same with CSS. Such as giving "overflow: hidden;" on the parent element.

presario
A: 

You could also use..


with added CSS rules it can do the job, and it serves this purpose.

I think your answer is embedded into the site. I'm guessing that you suggested using hr tag :)
bLee