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>
1
  • 1
    I would assume it would be necessary. It's important to terminate your tags.
    – Bry6n
    Feb 14, 2012 at 15:10

3 Answers 3

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".
1
  • 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, 2012 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>

1

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.