CSS:想要位置:粘性地将元素放置在其父容器的底部/右侧

Jef*_*ews 5 css css-position

请参阅这个jsfiddle

我想要<div>带有innerText“嗨!” 始终出现在其包含父级的可见部分的底部/右侧。它不应随其父级的内容一起滚动。

HTML:

<table>
  <thead>
    <tr>
      <th style="width: 90%">Header 1</th>
      <th style="width: 10%">Header 2</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>
        <div id="flex-div">
          <div class="flex-item">1</div>
          <div class="flex-item">2</div>
          <div class="flex-item">3</div>
          <div class="sticky-bottom-right">Hi!</div>
        </div>
      </td>
      <td>more</td>
    </tr>
  </tbody>
</table>
Run Code Online (Sandbox Code Playgroud)

CSS:

table {
  table-layout: fixed;
  width: 100%;
}
table, th, td {
  border: 1px solid black;
  border-collapse: collapse;
}

#flex-div {
  display: flex;
  justify-content: space-between;
  overflow: auto;
  positiom: relative;
}

.flex-item {
  background-color: beige;
  border: 1px solid blue;
  height: 100px;
  padding: 30 px;
  text-align: center;
  min-width: 200px;
}
.sticky-bottom-right {
  font-weight: bold;
  position: sticky;
  right: 0;
}
Run Code Online (Sandbox Code Playgroud)

我尝试了position:fixed,但它是相对于整个视口而不是包含父级的。

Nao*_*omi 1

你的意思是这样的吗?

如果您在 .sticky-bottom-right 上使用position:absolute,那么它对于其内部的div来说将是绝对的,并且位置:relative。

你也有一个错字,positioniom而不是position。

table {
  table-layout: fixed;
  width: 100%;
}
table, th, td {
  border: 1px solid black;
  border-collapse: collapse;
}

#flex-div {
  display: flex;
  justify-content: space-between;
  overflow: auto;
  position: relative;
}

.flex-item {
  background-color: beige;
  border: 1px solid blue;
  height: 100px;
  padding: 30 px;
  text-align: center;
  min-width: 200px;
}
.sticky-bottom-right {
  font-weight: bold;
  position: absolute;
  right: 0;
  bottom: 0;
}
Run Code Online (Sandbox Code Playgroud)
<table>
<thead>
<tr>
  <th style="width: 90%">Header 1</th>
  <th style="width: 10%">Header 2</th>
</tr>
</thead>
<tbody>
  <tr>
    <td>
      <div id="flex-div">
        <div class="flex-item">1</div>
        <div class="flex-item">2</div>
        <div class="flex-item">3</div>
        <div class="sticky-bottom-right">Hi!
        </div>
        </div>
      </div>
    </td>
    <td>more</td>
  </tr>
</tbody>
</table>
Run Code Online (Sandbox Code Playgroud)

  • 我想要“嗨!” tp在那个角落一动不动。其他所有内容都应滚动到其后面。当您将鼠标悬停在示例上时,请查看出现在角落的“重新运行”按钮[此处](https://gedd.ski/post/position-sticky/)。 (2认同)
  • 谢谢你的努力。我想到了。我将便利贴从弹性盒中取出,并使其成为弹性盒的兄弟姐妹。我将 Flexbox 和 Sticky 包裹在外部 div 中,并将 Sticky 定位为绝对位置,底部:0,右侧:0。这是更新的 [fiddle](https://jsfiddle.net/2g80r8br/12/)。 (2认同)