Table 100% Width and Margins

While hacking aboooooot today I found the need for making a table behave like a regular block element. I have a section floated to the right of a table, and the table should occupy the rest of the available spot. Making the table’s width 100% would make it adjust it size according to its parent instead of the available place (.. with margin-right set to the width of the other element + whitespace).

The best solution I’ve found so far is to create a wrapper div around the table, and then setting the table width to 100%. This makes the table adjust its size according to the parent – which now is a block element.

  1. <style type="text/css">
  2. #wrapper
  3. {
  4.     margin-right: 300px;
  5. }
  6.  
  7. #right
  8. {
  9.     width: 250px;
  10.     float: right;
  11. }
  12.  
  13. table
  14. {
  15.     width: 100%;
  16. }
  17. </style>
  18.  
  19. <div id="right">
  20. asd
  21. </div>
  22. <div id="wrapper">
  23.     <table>
  24.         …
  25.     </table>
  26. </div>

Tags: , , , , ,

Leave a Reply