html – TABLE比包含DIV更宽

html – TABLE比包含DIV更宽,第1张

概述在下面的示例中,也可以使用 here,WebKit(Safari 5,Chrome 10.0.648.205)和Mozilla(FF 4)都保持DIV div围绕浏览器窗口的可见宽度,而TABLE与其内容一样宽。 我希望DIV可以像TABLE一样广泛,但是由于浏览器的行为是一致的,我怀疑这是一个功能而不是一个bug。 有趣的是,如果DIV设置为float:left,it does grow as 在下面的示例中,也可以使用 here,WebKit(Safari 5,Chrome 10.0.648.205)和Mozilla(FF 4)都保持div div围绕浏览器窗口的可见宽度,而table与其内容一样宽。

我希望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&nbsp;Cell</td><td>table&nbsp;Cell</td><td>table&nbsp;Cell</td><td>table&nbsp;Cell</td><td>table&nbsp;Cell</td><td>table&nbsp;Cell</td><td>table&nbsp;Cell</td><td>table&nbsp;Cell</td><td>table&nbsp;Cell</td><td>table&nbsp;Cell</td><td>table&nbsp;Cell</td><td>table&nbsp;Cell</td><td>table&nbsp;Cell</td><td>table&nbsp;Cell</td><td>table&nbsp;Cell</td><td>table&nbsp;Cell</td><td>table&nbsp;Cell</td><td>table&nbsp;Cell</td><td>table&nbsp;Cell</td><td>table&nbsp;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更宽所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1114001.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存