我希望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>解决方法 默认情况下,块元素取其父元素的宽度,而不是其内容的宽度;另一方面,高度是相反的工作方式:块元素默认的高度内容,而不是父母的高度。事情以这种方式工作,因为HTML / CSS是围绕英文文本的通常的从上到下的布局构建的。
所以,#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>的整个宽度。儿童。
总结以上是内存溢出为你收集整理的html – TABLE比包含DIV更宽全部内容,希望文章能够帮你解决html – TABLE比包含DIV更宽所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)