[英]div inherit width & height and scroll
How can i make a DIV inherit its Width and Height from its container without a fixed defined height and width, cover the whole area inside the container by default and scroll instead of increasing its height or width . 如何使DIV从其容器继承其Width和Height而没有固定的定义高度和宽度,默认情况下覆盖容器内部的整个区域并滚动而不是增加其height或width。 eg
例如
<td style="width:400px; ">
<div style="overflow:scroll; ">
</div>
</td>
This DIV should cover 400px and when the content exceeds from this width it should scroll. 该DIV应该覆盖400像素,并且当内容超出此宽度时,它应该滚动。 The container could be a TD or SPAN.
容器可以是TD或SPAN。 One approach might be to have a CSS class with just a defined width and apply on both container and the div, But, i dont know where else this DIV would be used and what is the width of its container.
一种方法可能是使CSS类具有定义的宽度,并同时应用于容器和div,但是,我不知道此DIV还将在其他地方使用以及其容器的宽度是多少。
Edit 1 By setting max-width to 100px i get what i desired but otherwise it increases the width of the container. 编辑1通过将max-width设置为100px,我得到了我想要的,但否则它增加了容器的宽度。 It is probably because the container have width in %.
可能是因为容器的宽度百分比。 It looks i need to find the initial width of the container and then set DIV's max-width.
看来我需要找到容器的初始宽度,然后设置DIV的最大宽度。 Is it?
是吗?
Edit 2 There is a grid inside this Div. 编辑2该Div内有一个网格。 When the max-width is defined in the style of the DIV, it reflects the behavior i need when it is set via JS the width is OK when page loads but when a link is clicked to load the data inside grid it looses max-width property.
当以DIV的样式定义max-width时,它反映了我通过JS设置时需要的行为,当页面加载时宽度是确定的,但是当单击链接以将数据加载到网格内时,它将失去max-width属性。
<script type="text/javascript">
var w = document.getElementById("divgrid").style.maxWidth;
if (w == null || w == NaN || w <= 0 || w.toString() == '')
w = document.getElementById("divgrid").offsetWidth;
document.getElementById('divgrid').style.maxWidth = w + 'px';
</script>
width:100%; height:100%; overflow:scroll;
You picked two interesting container elements, since spans have a default display
value of inline
and tables have a default value of table
. 您选择了两个有趣的容器元素,因为Span的默认
display
值为inline
而table的默认值为table
。 Neither respect the overflow
property, so to create scrollbars around a div element you need to set them to display:block
. 都不考虑
overflow
属性,因此要在div元素周围创建滚动条,需要将它们设置为display:block
。 Using your above example: 使用上面的示例:
HTML 的HTML
<table>
<tr>
<td>
<div>...lots of text here...</div>
</td>
</tr>
</table>
CSS 的CSS
table{
height:100px;
width:100px;
display:block;
overflow:scroll;
}
use overflow-x:scroll
in table
or td
在
table
或td
使用overflow-x:scroll
eg 例如
<table style="width:400px; overflow-x:scroll;">
<tr>
<td><div>content</div></td>
</tr>
</table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.