将同级元素一个一个地放置
我有一个包含多行的 TABLE
。在每一行中,一个 TD
元素包含多个 DIV
元素,这些元素:
- 可以具有任何维度,但所有这些元素(在一个
TD
内)都具有相同的像素尺寸 - 必须放置在另一个
- 包含
TD
的顶部,需要根据子DIV
元素调整大小 - 一个
DIV
将显示在 时间(静态) - 从一种衰落变为另一种衰落的 为使用(因此其中两个将同时显示),
- 同时显示的数量永远不会超过两个
这是此类表格的简化示例
<table>
<tr>
<td>Name</td>
<td>
<div>First</div>
<div>Second</div>
...
<div>Last</div>
</td>
<td>Additionals</td>
</tr>
<tr>
<td>Name</td>
<td>
<div>First</div>
<div>Second</div>
...
<div>Last</div>
</td>
<td>Additionals</td>
</tr>
...
</table>
明显的解决方案是设置
div
{
position: absolute;
}
这将自动定位 DIV
元素彼此重叠(根据流布局),但 TD
维度将保持不变,就好像它不包含子元素一样。溢出不会执行任何操作,因为子元素是绝对定位的。
td
{
overflow: auto;
}
我应该如何完成这项工作?
I have a TABLE
with multiple rows. Within each row one of the TD
elements contains several DIV
elements that:
- can have any dimension but all of them (within one
TD
) have the same pixel dimension - have to be positioned one on top of the other
- containing
TD
needs to resize according to childDIV
elements - One
DIV
will be displayed at a time (statically) - when changing from one to the other fading will be used (so two of them will be displayed at the same time)
- there will never be more than two of them displayed at the same time
This is a simplified example of such a table
<table>
<tr>
<td>Name</td>
<td>
<div>First</div>
<div>Second</div>
...
<div>Last</div>
</td>
<td>Additionals</td>
</tr>
<tr>
<td>Name</td>
<td>
<div>First</div>
<div>Second</div>
...
<div>Last</div>
</td>
<td>Additionals</td>
</tr>
...
</table>
The obvious solution would be to set
div
{
position: absolute;
}
This will automatically position DIV
elements on top of each other (according to flow layout), but TD
dimension will stay as if it contains no child elements. Overflow doesn't do anything because child elements are absolutely positioned.
td
{
overflow: auto;
}
How should I make this work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你事先知道div的大小吗?如果是这样,你可以给包装器 div 那些尺寸:
如果你不知道尺寸,那么,由于你已经在使用 JS,你可以获取包装器第一个子 div 的尺寸,然后设置通过 JS 的包装尺寸。
更新:
我能想到的用 CSS 单独完成此操作的唯一方法是让第一个 DIV 渲染两次。第一个实例设置为position:relative,然后它是重复的,像其他实例一样,可以设置为position:absolute。这个克隆就像一个垫片。如果您想确保它不会在视觉上显示(但仍占用适当的空间),您可以设置“可见性:隐藏”。然而,这样做的缺点是,您现在的页面上有重复的内容,从 SEO 和/或可访问性 POV 来看,这可能并不理想。
我可能会走 JS 路线并在页面加载后获取尺寸。
Do you know the size of the divs before hand? If so, you can just give a wrapper div those dimensions:
If you don't won't know the size, then, since you are already using JS, you could grab the size of the first child div of wrapper and then set the wrapper dimensions via JS.
UPDATE:
The only way I can think of doing this with CSS all by itself would be to have the first DIV render twice. The first instance set to position: relative, and then it's duplicate, like the rest, can be set to position: absolute. This clone then acts like a shim. You could set visibility: hidden if you want to make sure it doesn't show up visually (but still takes up the proper space). The drawback to this, however, is that you now have content duplicated on your page, which may not be ideal from an SEO and/or accessibility POV.
I'd probably go the JS route and grab the dimensions after the page loads.