添加新元素时调整子元素的大小
Make child element resize when adding new elements
我有一个有多个子div 的父元素。一个是"内容"区域,将始终可见,并包含可滚动的材料部分。我的问题是我无法根据包含多少其他div 元素来正确调整大小。我显然可以手动调整它的大小,但我的问题是我有一些动态元素可以添加到父元素中,这些元素将向下推"内容"部分。
有没有办法让内容区域使用 CSS 调整自身大小?还是我必须使用 JS 实现?下面是一个基本(没有动态元素)发生的情况示例:
JSFiddle here 编辑:(更新以包含我希望它的样子)
<div id="mytest">
Test
<div id="topper">
Other Test
</div>
<div id="rollit">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</div>
</div>
编辑:我的问题是我不希望"rollit"扩展父div,我希望"rollit"重新调整自己以适应。这部分是关键,因为我的代码中的"topper"部分实际上是动态添加的。简而言之,无论如何,"mytest"div 都应该保持相同的大小,topperdiv 是稍后添加的,它应该(目前不)缩小"rollit",以便"rollit"仍然适合父div。
将此样式添加到mytest
:
display: table;
将两个div
添加到mytest
,具有以下类:
.row1 {
display: table-row;
height: 0px;
}
.row2 {
display: table-row;
}
row2
将包含您要适合的内容(rollit
)。 row1
将包含其余内容。
因为您要在 mytest
上设置高度,所以它的行将尝试按比例扩展以适应该高度。 height: 0px
导致row1
仅占用其内容所需的高度。
rollit
上的overflow
样式可防止row2
超出mytest
的范围。
片段:
#mytest {
display: table;
height: 150px;
border: 1px solid #000;
background: yellow;
}
.row1 {
display: table-row;
height: 0px;
}
.row2 {
display: table-row;
}
#topper {
height: 30px;
}
#rollit {
height: 100%;
width: 200px;
overflow: auto;
}
<div id="mytest">
<div class="row1">
Test
<div id="topper">
Other Test
</div>
</div>
<div class="row2">
<div id="rollit">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</div>
</div>
</div>
像这样的东西
#mytest {
border: 1px solid #000;
}
#topper {
height: 30px;
}
#rollit {
width: 50%;
overflow: auto;
height:200px;
}
基本上,不要为要自动扩展的元素设置height
。
http://jsfiddle.net/gaby/yLa6v2qk/5/
相关文章:
- 调整<音频>元素
- 可以在调整窗口大小时重新调整stellar.js的元素偏移量
- 可滚动元素,如何在调整大小时更改顶部偏移量
- 如何获得一个进度元素,它被设置为在窗口调整大小时调整大小
- jQuery在使用css定位的元素上调整大小;无法在chrome中正确调整大小
- 在调整大小和加载时移动 DOM 元素.仅适用于调整大小
- 拖动和调整 CSS 转换元素的大小
- jQuery 可调整大小:如何在调整大小拖动期间获取重叠元素
- 调整父元素大小时,文本会移动
- 如何使用可调整大小的元素重叠其他元素
- 如何在使用react时动态调整元素大小
- 在单独的容器中拖放,并调整放置的元素的位置
- HTML5画布中的可拖动和可调整大小元素
- 根据其他调整大小的html元素定位背景图像
- HTML DIV 显示无元素调整大小
- 有实时元素调整大小的例子吗?
- 将Object元素调整为其容器元素的大小
- HTML演示幻灯片,元素调整大小以保持比例
- jQuery mouseout事件后的元素调整大小
- 将Javascript元素调整为浏览器大小