将同级元素一个一个地放置

发布于 2024-11-02 09:23:11 字数 1281 浏览 0 评论 0原文

我有一个包含多行的 TABLE 。在每一行中,一个 TD 元素包含多个 DIV 元素,这些元素:

  1. 可以具有任何维度,但所有这些元素(在一个 TD 内)都具有相同的像素尺寸
  2. 必须放置在另一个
  3. 包含 TD 的顶部,需要根据子 DIV 元素调整大小
  4. 一个 DIV 将显示在 时间(静态)
  5. 从一种衰落变为另一种衰落的 为使用(因此其中两个将同时显示),
  6. 同时显示的数量永远不会超过两个

这是此类表格的简化示例

<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:

  1. can have any dimension but all of them (within one TD) have the same pixel dimension
  2. have to be positioned one on top of the other
  3. containing TD needs to resize according to child DIV elements
  4. One DIV will be displayed at a time (statically)
  5. when changing from one to the other fading will be used (so two of them will be displayed at the same time)
  6. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

緦唸λ蓇 2024-11-09 09:23:11

你事先知道div的大小吗?如果是这样,你可以给包装器 div 那些尺寸:

<td>
    <div class="wrapper">
       <div>1</div>
       <div>2</div>
       <div>3</div>
    etc...

如果你不知道尺寸,那么,由于你已经在使用 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:

<td>
    <div class="wrapper">
       <div>1</div>
       <div>2</div>
       <div>3</div>
    etc...

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文