Html 位置绝对 + 滚动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17656623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Position Absolute + Scrolling
提问by Chris Meek
With the following HTML
and CSS
随着以下HTML
和CSS
.container {
position: relative;
border: solid 1px red;
height: 256px;
width: 256px;
overflow: auto;
}
.full-height {
position: absolute;
top: 0;
left: 0;
right: 128px;
bottom: 0;
background: blue;
}
<div class="container">
<div class="full-height">
</div>
</div>
The inner div
takes up the full head of the container as desired. If I now add some other, flow, content to the container such as:
内部根据div
需要占据容器的整个头部。如果我现在向容器添加一些其他流内容,例如:
.container {
position: relative;
border: solid 1px red;
height: 256px;
width: 256px;
overflow: auto;
}
.full-height {
position: absolute;
top: 0;
left: 0;
right: 128px;
bottom: 0;
background: blue;
}
<div class="container">
<div class="full-height">
</div>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aspernatur mollitia maxime facere quae cumque perferendis cum atque quia repellendus rerum eaque quod quibusdam incidunt blanditiis possimus temporibus reiciendis deserunt sequi eveniet necessitatibus
maiores quas assumenda voluptate qui odio laboriosam totam repudiandae? Doloremque dignissimos voluptatibus eveniet rem quasi minus ex cumque esse culpa cupiditate cum architecto! Facilis deleniti unde suscipit minima obcaecati vero ea soluta odio cupiditate
placeat vitae nesciunt quis alias dolorum nemo sint facere. Deleniti itaque incidunt eligendi qui nemo corporis ducimus beatae consequatur est iusto dolorum consequuntur vero debitis saepe voluptatem impedit sint ea numquam quia voluptate quidem.
</div>
Then the container scrolls as desired, however the absolutely positioned element is no longer anchored to the bottom of the container but stops at the initial view-able bottom of the container. My question is; is there any way of having the absolutely positioned element be the complete scroll height of its container without using JS
?
然后容器根据需要滚动,但是绝对定位的元素不再锚定到容器的底部,而是停在容器的初始可见底部。我的问题是;有没有办法让绝对定位的元素成为其容器的完整滚动高度而不使用JS
?
采纳答案by giaour
You need to wrap the text in a div
element and include the absolutely positioned element inside of it.
您需要将文本包装在一个div
元素中,并在其中包含绝对定位的元素。
<div class="container">
<div class="inner">
<div class="full-height"></div>
[Your text here]
</div>
</div>
Css:
css:
.inner: { position: relative; height: auto; }
.full-height: { height: 100%; }
Setting the inner div's position to relative
makes the absolutely position elements inside of it base their position and height on it rather than on the .container
div, which has a fixed height. Without the inner, relatively positioned div
, the .full-height
div will always calculate its dimensions and position based on .container
.
将内部 div 的位置设置为relative
使其内部的绝对位置元素基于它的位置和高度,而不是基于.container
具有固定高度的div。如果没有内部的相对定位div
,.full-height
div 将始终基于 计算其尺寸和位置.container
。
* {
box-sizing: border-box;
}
.container {
position: relative;
border: solid 1px red;
height: 256px;
width: 256px;
overflow: auto;
float: left;
margin-right: 16px;
}
.inner {
position: relative;
height: auto;
}
.full-height {
position: absolute;
top: 0;
left: 0;
right: 128px;
bottom: 0;
height: 100%;
background: blue;
}
<div class="container">
<div class="full-height">
</div>
</div>
<div class="container">
<div class="inner">
<div class="full-height">
</div>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aspernatur mollitia maxime facere quae cumque perferendis cum atque quia repellendus rerum eaque quod quibusdam incidunt blanditiis possimus temporibus reiciendis deserunt sequi eveniet necessitatibus
maiores quas assumenda voluptate qui odio laboriosam totam repudiandae? Doloremque dignissimos voluptatibus eveniet rem quasi minus ex cumque esse culpa cupiditate cum architecto! Facilis deleniti unde suscipit minima obcaecati vero ea soluta odio
cupiditate placeat vitae nesciunt quis alias dolorum nemo sint facere. Deleniti itaque incidunt eligendi qui nemo corporis ducimus beatae consequatur est iusto dolorum consequuntur vero debitis saepe voluptatem impedit sint ea numquam quia voluptate
quidem.
</div>
</div>
回答by flaschbier
position: fixed;
will solve your issue. As an example, review my implementation of a fixed message area overlay (populated programmatically):
position: fixed;
将解决您的问题。例如,查看我对固定消息区域覆盖的实现(以编程方式填充):
#mess {
position: fixed;
background-color: black;
top: 20px;
right: 50px;
height: 10px;
width: 600px;
z-index: 1000;
}
And in the HTML
在 HTML 中
<body>
<div id="mess"></div>
<div id="data">
Much content goes here.
</div>
</body>
When #data
becomes longer tha the sceen, #mess
keeps its position on the screen, while #data
scrolls under it.
当比屏幕#data
更长时,#mess
保持其在屏幕上的位置,同时在屏幕#data
下方滚动。
回答by TorranceScott
So gaiour is right, but if you're looking for a full height item that doesn't scroll with the content, but is actually the height of the container, here's the fix. Have a parent with a height that causes overflow, a content container that has a 100% height and overflow: scroll
, and a sibling then can be positioned according to the parent size, not the scroll element size. Here is the fiddle: http://jsfiddle.net/M5cTN/196/
所以 gaiour 是对的,但如果您正在寻找一个不随内容滚动但实际上是容器高度的全高项目,这里是修复。有一个高度会导致溢出的父级,一个具有 100% 高度的内容容器和overflow: scroll
,然后可以根据父级大小而不是滚动元素大小来定位同级。这是小提琴:http: //jsfiddle.net/M5cTN/196/
and the relevant code:
和相关代码:
html:
html:
<div class="container">
<div class="inner">
Lorem ipsum ...
</div>
<div class="full-height"></div>
</div>
css:
css:
.container{
height: 256px;
position: relative;
}
.inner{
height: 100%;
overflow: scroll;
}
.full-height{
position: absolute;
left: 0;
width: 20%;
top: 0;
height: 100%;
}
回答by Tibor Udvari
I ran into this situation and creating an extra div was impractical.
I ended up just setting the full-height
div to height: 10000%; overflow: hidden;
我遇到了这种情况,创建一个额外的 div 是不切实际的。我最终只是将full-height
div设置为height: 10000%; overflow: hidden;
Clearly not the cleanest solution, but it works really fast.
显然不是最干净的解决方案,但它的工作速度非常快。