如何在正文和页脚之间获得固定边距

新手上路,请多包涵

我是 CSS 的新手,正在尝试设置一个页面,以便 在页面的主要内容(侧边栏/部分)和页脚(例如 120px)之间始终有一个固定的边距/空间, 这应该跨浏览器工作。

此外,如果页面上的内容非常少,则 页脚应始终至少出现在(可见)屏幕的底部

我通过应用类 footer 进行了多次尝试,包括以下内容,但总是忽略边距。

 .footer {
    color: #333;
    font-size: 11px;
    text-align: center;
    vertical-align: bottom
}
.footer:before {
    clear: both;
    display: block;
    height: 120px;
    min-height: 120px;
}
 <html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <!-- ... -->
    </head>
    <body>
        <nav>
            <!-- ... -->
        </nav>
        <section id="sidebar">
            <!-- ... -->
        </section>
        <section id="main">
            <!-- ... -->
        </section>
        <footer class="footer">
            <div>Some text</div>
        </footer>
    </body>
</html>

有人可以帮我弄这个吗?

此外,如果关于我的 HTML 有任何更改,请也让我知道。

原文由 keewee279 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 271
2 个回答

这应该有助于:

 html, body {
    height: 100%;
}
body {
    margin:0px;
    padding: 0px;
}
.wrap {
    height: auto;
    margin: 0 auto -80px; /* footer height + space */
    min-height: 100%;
    padding: 0 0 80px; /* footer height + space */
    box-sizing: border-box;
    overflow: auto;
}
.footer {
    background-color: #111111;
    color: #eeeeee;
    border-top: 1px solid red;
    height: 60px;  /* footer height */
    padding-top: 20px;
    display: block;
    margin-top: 20px; /* space between content and footer */
    box-sizing: border-box;
    position: relative;
    width: 100%;
}
 <body>
    <div class="wrap">
        <nav>
            <!-- ... -->
        </nav>
        <section id="sidebar">
            <!-- ... -->
        </section>
        <section id="main">
            <!-- ... -->

        </section>
    </div>
    <footer class="footer">
        <div>Some text</div>
    </footer>
</body>

原文由 AndreiDMS 发布,翻译遵循 CC BY-SA 3.0 许可协议

要在正文和页脚之间添加边距,只需将其添加到页脚部分的样式中: padding:20px 0px 0px 0px;

将页脚保持在底部更加复杂。为 css 尝试这样的事情:

 html, body {
    margin:0;
    padding:0;
    height:100%;
}

#wrapper{              /*create a div around whole html body
    min-height:100%;
    position:relative;
}

#main{
    padding-bottom:100px; /* Height of the footer element */
}

#footer {
    width:100%;
    height:100px;
    position:absolute;
    bottom:0;
    left:0;
    color: #333;
}

原文由 user4477886 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题