首页 > 解决方案 > 如何将页脚放在页面底部

问题描述

我的页脚位于页面底部,直到我在它之前添加了几个 div。我不确定为什么这会抛出我的代码。我不想使用 position: fixed 因为我希望它位于底部,但只有在向下滚动到时才能看到,例如此页面上的页脚。

            .gallerybox {
                border: 4px solid rgba(54, 215, 183, 1);
                width:30%;
                height:200px;
                float:left;
                margin-left:10px;
                margin-bottom:10px;
            }
            #footer {
                width:100%;
                height:100px;
                background-color:lightgray;
                bottom:0;
                left:0;
                position:relative;
            }
    <div id="holder">
        <div id="body">
            <p id="gallery">The Gallery</p>
            <div class="gallerybox"></div>
            <div class="gallerybox"></div>
            <div class="gallerybox"></div>
            <div class="gallerybox"></div>
            <div class="gallerybox"></div>
            <div class="gallerybox"></div>
            <br>
    <div id="footer">FOOTER</div>
        </div>
         </div>

标签: htmlcsspositionfooterdivision

解决方案


请不要使用float属性来定位多个div.

使用display: flex以尽可能最好的方式实现相同的目标。

我想这就是你想要的

.container{
  display:flex;
  flex-direction:column;
}
.gallery{
  display:flex;
  flex-wrap:wrap;
}
.gallerybox {
  border: 4px solid rgba(54, 215, 183, 1);
  width:30%;
  height:200px;
  margin-left:10px;
  margin-bottom:10px;
}
#footer {
  width:100%;
  height:100px;
  background-color:lightgray;
}
<div id="holder">
  <p>The Gallery</p>
        <div class="container">
          <div class="gallery">
            <div class="gallerybox"></div>
            <div class="gallerybox"></div>
            <div class="gallerybox"></div>
            <div class="gallerybox"></div>
            <div class="gallerybox"></div>
            <div class="gallerybox"></div>
          </div> 
          <div id="footer">FOOTER</div>
        </div>
</div>

我已包含该display: flex属性并删除了float: left创建问题的属性,还为 HTML 结构添加了一些细微的更改。

我建议您学习flexbox它将使使用 CSS定位和构建HTML 变得非常容易和易于理解。

请告诉我我是否有任何帮助:)


推荐阅读