表格内的绝对定位

发布于 2024-11-26 07:50:51 字数 1482 浏览 2 评论 0原文

我需要在 td 内使用绝对定位来定位某些内容。为了解决 td 在将其设置为相对时未定义的事实,我在 td 内使用设置为相对的 div,然后在该 div 内将内部 div 设置为绝对。当我有内容填满 td 时,这一切都非常有效。当我将 td 的内容设置为不显示时,绝对定位就会全部搞砸。

有谁知道为什么会这样。

HTML:

<table>
    <tr>
        <td>
            <div class="relative">
                <div class='absolute'>
                    <p>A</p>
                </div>
            </div>
            <div class="slot"></div>
            <div class="slot"></div>
        </td>
        <td>
          <div class="relative">
             <div class='absolute'>
               <p>B</p>
           </div>
           </div>
           <div class="slot hidden"></div>
           <div class="slot"></div>
        </td>
    </tr>
</table>

和 CSS:

td{
    border:1px solid red;
    width:100px;
    height:60px;
    vertical-align:bottom;
}

.slot{
  width:100px;
  height:29px;
  background-color:#999;
  border:1px dashed blue;
}

.relative{
    position:relative;
}

.absolute{
    position:absolute;
    top:5px;
    left:5px;
}
.hidden{
    display:none;
}

以及实时版本: http://jsfiddle.net/HgEtQ/

在上面的小提琴中您可以看到第一个表格单元格工作正常。绝对定位的 div 位于正确的空间中。第二个隐藏了顶部插槽,现在绝对定位不再位于左上角。如果你把两个插槽都取出来,那么绝对定位就真的搞砸了。我需要将其绝对定位,因为某些元素将根据元素进行移动。

I need to position something with absolute positioning inside a td. To get around the fact that a td is undefined when setting it to relative, I use a div set to relative inside my td then inside that div I have an inner div set to absolute. This all works great when I have content filling up the td. When I set the content of the td to display none then the absolute positioning gets all screwed up.

Does anyone know why this would be.

HTML:

<table>
    <tr>
        <td>
            <div class="relative">
                <div class='absolute'>
                    <p>A</p>
                </div>
            </div>
            <div class="slot"></div>
            <div class="slot"></div>
        </td>
        <td>
          <div class="relative">
             <div class='absolute'>
               <p>B</p>
           </div>
           </div>
           <div class="slot hidden"></div>
           <div class="slot"></div>
        </td>
    </tr>
</table>

And CSS:

td{
    border:1px solid red;
    width:100px;
    height:60px;
    vertical-align:bottom;
}

.slot{
  width:100px;
  height:29px;
  background-color:#999;
  border:1px dashed blue;
}

.relative{
    position:relative;
}

.absolute{
    position:absolute;
    top:5px;
    left:5px;
}
.hidden{
    display:none;
}

And a live version: http://jsfiddle.net/HgEtQ/

In the fiddle above you can see the first table cell works correctly. The absolutely positioned div is in the correct space. The second one has hidden the top slot and now the absolute positioning is not in the top left corner anymore. If you take out both slots then it really screws up the absolute positioning. I need to positioning it absolute because some of the elements will be shifted depending on the element.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

柳若烟 2024-12-03 07:50:52

这里发生了一些事情。

你有这样的:

td {
    /*...*/
    vertical-align:bottom;
}

这会将单元格的内容推到底部。

此外,绝对定位的元素从正常流程中删除,因此它不会影响其父级的高度:

它完全从正常流程中删除(对后来的兄弟姐妹没有影响)。绝对定位的盒子为正常流程子代和绝对(但不是固定)定位的后代建立了一个新的包含块。

特别是,这意味着您的 div.relative 元素的高度为零,但它们仍然具有左上角,因此您的绝对定位偏移量锚定在某处。

然后你有

并且 hidden 会从布局中删除

,这样你就可以有效地就是这样:
<div class="relative"></div> <!-- Height zero -->
<div class="slot"></div>     <!-- Height 29px -->

vertical-align: Bottom 结合意味着您的第二个 div.absolute 将位于距 div.slot< 顶部 5px 的位置/code> 即 25px从表格单元格的底部。

第一个单元格工作正常,因为两个可见的 div.slot 元素将 div.relative 向右推到单元格的顶部,然后是 div.absolute code> 距离 div.relative 顶部 5 像素,即距离表格单元格顶部 5 像素。

不幸的是,就浏览器而言,将 position:relative 添加到 有点狡猾,因此您需要一些技巧来正确定位,同时保持 <代码>垂直对齐:底部。您可以像这样重新构造

<td>
    <div class="relative">
        <div class='absolute'>
            <p>A</p>
        </div>
    </div>
    <div class="nonsense">
        <div class="slot"></div>
        <div class="slot"></div>
    </div>
</td>

CSS 如下所示:

td{
    border:1px solid red;
    width:100px;
    height:60px;
    vertical-align: top;
}

.slot{
    width:100px;
    height:29px;
    background-color:#999;
    border:1px dashed blue;
}

.relative {
    position:relative;
}
.nonsense {
    height: 62px; /* td[height] + 2 for the borders */
    display: table-cell;
    vertical-align: bottom;
}

.absolute{
    position:absolute;
    top:5px;
    left:5px;
}
.hidden{
    display:none;
}

实例:http://jsfiddle.net/ambigously/aV4nT/

或者你可以使用 可见性:隐藏

隐藏
生成的框是不可见的(完全透明,没有绘制任何内容),但仍然影响布局。此外,如果元素的后代具有“visibility:visible”,则它们将是可见的。

而不是 .hidden 类的 display: none

.hidden {
    visibility: hidden;
}

这将使两个 div.slot 元素占用空间并影响布局,但只有将看到第二个。

实例:http://jsfiddle.net/ambigously/RcdNh/

There are a couple things going on here.

You have this:

td {
    /*...*/
    vertical-align:bottom;
}

That will push the content of the cells to the bottom.

Also, an absolutely positioned element is removed from the normal flow so it won't contribute to its parent's height:

It is removed from the normal flow entirely (it has no impact on later siblings). An absolutely positioned box establishes a new containing block for normal flow children and absolutely (but not fixed) positioned descendants.

In particular, this means that your div.relative elements have a height of zero but they will still have an upper left corner so your absolute positioning offsets are anchored somewhere.

Then you have <div class="slot hidden"> and the hidden removes the <div> from the layout so you effectively have just this:

<div class="relative"></div> <!-- Height zero -->
<div class="slot"></div>     <!-- Height 29px -->

That combined with the vertical-align: bottom means that your second div.absolute will be positioned 5px from the top of the div.slot and that is 25px from the bottom of the table cell.

The first cell works fine because the two visible div.slot elements push the div.relative right to the top of the cell, then the div.absolute is positioned 5px from the top of the div.relative and that is 5px from the top of the table cell.

Unfortunately, adding position: relative to a <td> is a bit dodgy as far as browsers go so you'll need some hackery to get your positioning right while keeping vertical-align: bottom. You could re-structure the <td>s like this:

<td>
    <div class="relative">
        <div class='absolute'>
            <p>A</p>
        </div>
    </div>
    <div class="nonsense">
        <div class="slot"></div>
        <div class="slot"></div>
    </div>
</td>

And the CSS like this:

td{
    border:1px solid red;
    width:100px;
    height:60px;
    vertical-align: top;
}

.slot{
    width:100px;
    height:29px;
    background-color:#999;
    border:1px dashed blue;
}

.relative {
    position:relative;
}
.nonsense {
    height: 62px; /* td[height] + 2 for the borders */
    display: table-cell;
    vertical-align: bottom;
}

.absolute{
    position:absolute;
    top:5px;
    left:5px;
}
.hidden{
    display:none;
}

Live example: http://jsfiddle.net/ambiguous/aV4nT/

Or you could use visibility: hidden:

hidden
The generated box is invisible (fully transparent, nothing is drawn), but still affects layout. Furthermore, descendants of the element will be visible if they have 'visibility: visible'.

instead of display: none for your .hidden class:

.hidden {
    visibility: hidden;
}

This will leave both div.slot elements taking up space and affecting the layout but only the second one will be seen.

Live example: http://jsfiddle.net/ambiguous/RcdNh/

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