请参阅这个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,但它是相对于整个视口而不是包含父级的。
你的意思是这样的吗?
如果您在 .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)