我希望DIV可以像TABLE一样广泛,但是由于浏览器的行为是一致的,我怀疑这是一个功能而不是一个bug。
有趣的是,如果DIV设置为float:left,it does grow as wide as the table。
任何解释?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> <html> <head> <title>Wide Table</title> <style> #container { background-color:blue; /* float:left;*/ } </style> </head> <body> <div id="container"> <table > <tr id="tr"> <td>Table Cell</td><td>Table Cell</td><td>Table Cell</td><td>Table Cell</td><td>Table Cell</td><td>Table Cell</td><td>Table Cell</td><td>Table Cell</td><td>Table Cell</td><td>Table Cell</td><td>Table Cell</td><td>Table Cell</td><td>Table Cell</td><td>Table Cell</td><td>Table Cell</td><td>Table Cell</td><td>Table Cell</td><td>Table Cell</td><td>Table Cell</td><td>Table Cell</td> </tr> </table> </div> </body> </html>
解决方法
所以,#container的宽度为< body>然后< table> #container内部的#container外部溢出。如果你把溢出:隐藏在#container上,你将剪辑< table>,overflow-x:auto; #container上会添加一个滚动条。
更新:就浮动元素而言,CSS3 spec有这样的说法:
The used value of ‘width’ is the computed value,unless that is ‘auto’,when used value is the shrink-to-fit width.
默认宽度为auto,因此shrink-to-fit是:
Calculation of the shrink-to-fit width is similar to calculating the width of a table cell using the automatic table layout algorithm. Roughly: calculate the preferred width by formatting the content without breaking lines other than where explicit line breaks occur,and also calculate the preferred minimum width,e.g.,by trying all possible line breaks.
您的< table>中没有任何换行符所以浮动的#container将占据其< table>的整个宽度。儿童。