views:

420

answers:

2

I would expect this code to display a border around a table. It does in Internet Explorer but not in Firefox. In firefox it displays a horizontal line above the table. If I add anything else e.g. <br /> within the div the border is displayed correctly. It also works if I remove the align attributes.
What is the reason for this behaviour?

<body>  
  <div style="border-style: solid; border-width: 1px;  
    border-color: #A8A8A8; width: 100%">
    <table align="left" cellpadding="0" cellspacing="0" width="100%">
      <tr>
        <td align="center">
          Sample Text<br />
        </td>
      </tr>
    </table>
  </div>
</body>
+6  A: 

The table uses the deprecated align attribute. This has been replaced with the CSS float property and has the same effect.

Floating elements do not, by default, influence the height of their container (I believe this feature is not correctly implemented in IE if the Doctype triggers Quirks mode, this probably accounts for the difference in rendering you are seeing. Add a Standards mode triggering Doctype to avoid this and many other inconsistencies between browsers).

This is because they are designed to achieve effects such as shown at http://complexspiral.com/publications/containing-floats/

See http://www.ejeliot.com/blog/59 for a number of ways to cause containers to expand to cover floated context. I find the overflow: hidden technique generally is the best option.

David Dorward
+1, for recommending an option other than the typical *clearfix* solution. Also, I hadn't come across that one before, so thanks! =)
David Thomas
A: 

Put this after your tag

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

That will fix it.

Zoidberg