0

When using a DIV element with a style of clear:both to clear a previous float, is it necessary to use a start and end tag to the DIV element? Here is an example ...

Is this OK?

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

or should it be this?

<div style="clear:both;"></div>
braX
  • 11,506
  • 5
  • 20
  • 33
webworm
  • 10,587
  • 33
  • 120
  • 217

3 Answers3

2

You need the closing tag, or browsers will not think the tag is closed.

However, why would you even want to use <div style="clear:both;"></div>? That involves adding an extra unsemantic div for no reason.

There are better ways to contain/clear floats:

  • Use overflow: hidden on the the element that contains your floats.
  • Or, use a clearfix such as the "micro clearfix".
thirtydot
  • 224,678
  • 48
  • 389
  • 349
  • 1
    Both will work but `overflow: hidden` will become a pain in the arm for grids and such. That's why grid usually come with said microfix, which works wonders. – elclanrs Feb 14 '12 at 15:55
1

If you're working in XHTML it is OK to use the <div />, if you're working in HTML 4.X you should add the </div>

Tim D'Haene
  • 1,183
  • 9
  • 8
1

Its not valid in HTML 4 & also in HTML5. Valid in XHTML. May chances of intercepting wrongly by browsers.

Praveen Vijayan
  • 6,521
  • 2
  • 29
  • 28