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

标签 html css footer

我是 CSS 的新手,我正在尝试设置一个页面,以便在页面的主要内容(边栏/部分)和页脚之间始终有一个固定边距/空间(例如 120 像素)这应该可以跨浏览器工作。
此外,如果页面上的内容非常少,页脚应始终至少出现在(可见)屏幕的底部

我通过应用 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 有任何更改,也请告诉我。

最佳答案

这应该有帮助:

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>

关于html - 如何在正文和页脚之间获得固定边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30556285/

相关文章:

html - 在两个 div 容器之间切换菜单

html - 连接并最小化CSS以进行生产

javascript - 如果选择在标签内,如何获取格式化文本

html - 为什么我的 div 之间有一个随机栏

css - 如何创建符合 CSS3 标准的样式表

css - Angular 14 动画触发器 "rootItem"已构建,并带有以下警告 : - The following provided properties are not animatable: overflow

jquery - IE 中页脚后的空白

html - 如何使用 html+css 设置带有 float 段落的页脚

html - 页脚文本在 IE10 中突出显示蓝色?

javascript - 如何在 Bootstrap Modal 上添加一个 Loading Spinner 动画