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
  • 9,702
  • 5
  • 16
  • 29
webworm
  • 9,413
  • 28
  • 96
  • 186

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
  • 210,355
  • 44
  • 377
  • 337
  • 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,053
  • 9
  • 8
1

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

Praveen Vijayan
  • 5,547
  • 2
  • 27
  • 25