将元素放置在div的底部

pfg*_*pfg 2 html css css-position

有谁知道一种将页脚像position: fixed元素一样固定在div底部的方法。我发现的所有方法都将其粘贴在开始视图的底部或底部,因此您必须一直向下滚动才能看到它们。

我试过将父项设置为position:relative,子项设置为position: absolute,也使用display: table-cellvertical-align: bottom,但是在滚动时都不会将其固定在原处,而是在div的底部或默认视图的底部将其保留为静态。

.a{
  position:relative;
  background-color:#ccc;
  width:500px;
  min-height: 150px;
  max-height: 150px;
  overflow-y: scroll;
  float:left;
  padding:8px;
}

.footer{
  position:absolute;
  bottom:0;
  left:0;
  width:100%;
  background-color:#666;
  color:#fff;
}
Run Code Online (Sandbox Code Playgroud)
<div class="a">
    Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />
    <div class="footer">Some text</div>
</div>
Run Code Online (Sandbox Code Playgroud)

Sha*_*aun 7

您不是真的,不是您想要的方式,并且需要使用jquery来保持位置不变。

最简单的解决办法是使容器包装整个区域。

.outer {
    position:relative;
    width:500px;
}

.a{
  background-color:#ccc;
  min-height: 150px;
  max-height: 150px;
  overflow-y: scroll;
  padding:8px;
}

.footer{
  position:absolute;
  bottom:0;
  left:0;
  width:100%;
  background-color:#666;
  color:#fff;
}
Run Code Online (Sandbox Code Playgroud)

  <div class="outer">
    <div class="a">
       Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />
    </div>
    <div class="footer">Some text</div>
  </div>
Run Code Online (Sandbox Code Playgroud)