CSS使页面页脚保持在页面底部的最小高度,但不与页面重叠

Cav*_*rob 329 css footer

我有以下页面(deadlink :) http://www.workingstorage.com/Sample.htm,它有一个我无法坐在页面底部的页脚.

我想要页脚

  • 当页面很短并且屏幕未填满时,它会粘到窗口底部
  • 当有多个屏幕内容(而不是重叠内容)时,保持文档结束并正常向下移动.

CSS继承并迷惑我; 我似乎无法正确地改变它以在内容上放置最小高度或使页脚移到底部.

Jon*_*ley 325

一个简单的方法是让身体100%您的网页,用min-height100%了.如果页脚的高度不变,这可以正常工作.

给页脚一个负边距:

footer {
    clear: both;
    position: relative;
    height: 200px;
    margin-top: -200px;
}
Run Code Online (Sandbox Code Playgroud)

  • 这仅在您提前知道页脚高度时才有效.有时页脚会有动态内容,或者您​​构建框架.可变高度页脚的任何想法? (20认同)
  • 这不适用于动态内容. (6认同)
  • 这似乎有很多奇怪的技巧,下面有答案,这些答案很简单,不使用任何技巧。 (2认同)
  • 您将页面的100%变成正文是什么意思?身体的宽度和高度??? (2认同)
  • 别为我工作! (2认同)

vsy*_*ync 112

我开发了一种非常简单的方法来将页脚粘贴在底部,但作为最常用的方法,您需要调整它以适应页脚的高度.

查看演示

下面的方法通过在其::after上放置一个伪元素来使用"技巧",并将其body设置为具有页脚的精确高度,因此它将占据页脚完全相同的空间,因此当页脚absolute位于其上方时,看起来页脚真的占据了空间并消除了它的绝对定位的负面影响(例如,超越了身体的内容)

HTML(基本常用标记)

html, body{ height:100%; margin:0; }
header{ height:50px; background:lightcyan; }
footer{ height:50px; background:PapayaWhip; }

/* Trick */
body{ 
  display:flex; 
  flex-direction:column; 
}

footer{
  margin-top:auto; 
}
 
Run Code Online (Sandbox Code Playgroud)
<body>
  <header>Header</header>
  <article>Content</article>
  <footer>Footer</footer>
</body>
Run Code Online (Sandbox Code Playgroud)

以下方法允许动态页脚高度:

使用flexbox

html{ height:100%; }
body{ min-height:100%; padding:0; margin:0; position:relative; }
header{ height:50px; background:lightcyan; }
footer{ background:PapayaWhip; }

/* Trick: */
body {
  position: relative;
}

body::after {
  content: '';
  display: block;
  height: 50px; /* Set same as footer's height */
}

footer {
  position: absolute;
  bottom: 0;
  width: 100%;
  height: 50px;
}
Run Code Online (Sandbox Code Playgroud)
<body>
  <header>Header</header>
  <article>Content</article>
  <footer>Footer</footer>
</body>
Run Code Online (Sandbox Code Playgroud)

使用表格布局

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

header {
  height: 50px;
  background: lightcyan;
}

footer {
  height: 50px;
  background: PapayaWhip;
}


/**** Trick: ****/
body {
  display: table;
  width: 100%; 
}

footer {
   display: table-row;
}
Run Code Online (Sandbox Code Playgroud)
<body>
  <header>Header</header>
  <article>Content</article>
  <footer>Footer</footer>
</body>
Run Code Online (Sandbox Code Playgroud)

  • 另一个技巧是不要使用 `height:100vh` 尝试使用 `min-height:100vh` 在某些情况下,内容在视口上太大,而是将其设置为最小高度。 (7认同)
  • @SteveWaring - 谢谢.更新了这个答案,因为它已经很老了.现在我赞成使用`flexbox` (3认同)
  • 谢谢!flexbox解决方案实际上很简单,不使用hack或奇怪的固定大小或固定位置。 (2认同)
  • 使用了 Flexbox 解决方案,但在长页面上,正文不会随内容一起增长,因此我将 `body: { height: 100% }` 更改为 `body: { min-height: 100% }`。 (2认同)

Tam*_*Pap 40

一个非常简单的方法,适用于跨浏览器,这是:

http://matthewjamestaylor.com/blog/keeping-footers-at-the-bottom-of-the-page

HTML

<div id="container">
   <div id="header"></div>
   <div id="body"></div>
   <div id="footer"></div>
</div>
Run Code Online (Sandbox Code Playgroud)

CSS

html,
body {
   margin:0;
   padding:0;
   height:100%;
}
#container {
   min-height:100%;
   position:relative;
}
#header {
   background:#ff0;
   padding:10px;
}
#body {
   padding:10px;
   padding-bottom:60px;   /* Height of the footer */
}
#footer {
   position:absolute;
   bottom:0;
   width:100%;
   height:60px;   /* Height of the footer */
   background:#6cf;
}
Run Code Online (Sandbox Code Playgroud)

  • 你说简单吗? (8认同)
  • 如果您用两根手指调整窗口大小,则该方法不适用于 Android 版 Firefox (2认同)

m.s*_*tos 39

一个非常简单的 flex 解决方案,不假设元素的固定高度或改变位置。

HTML

<div class="container">
  <header></header>
  <main></main>
  <footer></footer>
</div>
Run Code Online (Sandbox Code Playgroud)

CSS

.container {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

main {
  flex: 1;
}
Run Code Online (Sandbox Code Playgroud)

浏览器支持

所有主流浏览器,IE11 及以下版本除外。

确保对适当的前缀使用Autoprefixer

<div class="container">
  <header></header>
  <main></main>
  <footer></footer>
</div>
Run Code Online (Sandbox Code Playgroud)
.container {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

main {
  flex: 1;
}
Run Code Online (Sandbox Code Playgroud)

  • 很有魅力!即使对于超过 4k 的分辨率,页脚也会粘在底部 (4认同)
  • 非常漂亮的解决方案。谢谢。 (2认同)
  • 主要浏览器 ⊅ IE11 (2认同)

Ras*_*him 35

使用 Flexbox 将页脚保持在底部

<div style="min-height:100vh; display:flex; flex-direction:column; 
            justify-content:space-between;">
 
     <div> <!-- Wrapper (Without footer) -->
   
        <header>
         I am a header.
        </header>
  
        <article>
        I am an article!
        </article>
   
    </div> <!-- End: Wrapper (Without footer) -->
 
 
     <footer>
     I am a footer.
     </footer>
 
</div>
Run Code Online (Sandbox Code Playgroud)

笔记

  • 确保<div>使用以下 CSS 样式将所有内容包装在一个或任何其他块级元素中:min-height:100vh; display:flex; flex-direction:column; justify-content:space-between;

  • 确保将除页脚元素之外的所有内容都包装在 a<div>或任何其他块级元素中。

  • 确保您使用<footer>或任何其他块级元素来包裹页脚。

代码说明

  • min-height: 100vh确保 body 元素至少拉伸到屏幕的整个高度

  • flex-direction: column在保留堆叠块元素方面保持正常文档流的行为(假设正文的直接子元素确实都是块元素)。

  • justify-content:space-between将页脚推到屏幕底部。


查看如何使用 Bootstrap 5 执行相同操作(将页脚保持在底部) -链接


Lia*_*iam 31

从IE7开始,您可以简单地使用

#footer {
    position:fixed;
    bottom:0;
}
Run Code Online (Sandbox Code Playgroud)

请参阅caniuse获取支持.

  • 当页脚内容超出内容时,我不希望页脚位于底部.我希望页脚在没有足够的时候粘到底部,然后在有超过屏幕的情况下正常向下扩展.粘性页脚解决了这个问题. (67认同)
  • 该解决方案是唯一将页脚打印到每一页的解决方案,而且非常简单!但我无法让内容与主要内容不重叠。有人有解决方案吗? (2认同)

N. *_*eux 29

我用它来将我的页脚粘到底部,它对我有用:

HTML

<body>
    <div class="allButFooter">
        <!-- Your page's content goes here, including header, nav, aside, everything -->
    </div>
    <footer>
        <!-- Footer content -->
    </footer>
</body>
Run Code Online (Sandbox Code Playgroud)

这是您必须在HTML中进行的唯一修改,将其添加div"allButFooter"类中.我用所有页面做了这些,那些那么短,我知道页脚不会粘到底部,而且页面也足够长,以至于我已经知道我必须滚动.我这样做了,所以我可以看到它在页面内容是动态的情况下工作正常.

CSS

.allButFooter {
    min-height: calc(100vh - 40px);
}
Run Code Online (Sandbox Code Playgroud)

"allButFooter"类有一个min-height依赖于视口的高度值(100vh指视口高度的100%)和页脚的高度,我已经知道了40px.

这就是我所做的一切,它对我来说非常有效.我没有在每个浏览器中尝试过它,只有Firefox,Chrome和Edge,结果就像我想要的那样.页脚粘到底部,您不必弄乱z-index,position或任何其他属性.我的文档中每个元素的位置是默认位置,我没有将其更改为绝对或固定或任何东西.

使用响应式设计

这是我想清楚的事情.这个解决方案具有相同的40px高的Footer,当我在使用响应式设计工作时没有像我预期的那样工作Twitter-Bootstrap.我不得不修改我在函数中减去的值:

.allButFooter {
    min-height: calc(100vh - 95px); 
}
Run Code Online (Sandbox Code Playgroud)

这可能是因为Twitter-Bootstrap它有自己的边距和填充,所以这就是我必须调整该值的原因.

我希望这对你们有用!至少,这是一个简单的尝试解决方案,它不涉及对整个文档进行大的更改.


San*_*eev 15

我使用的简单解决方案适用于IE8 +

在html上给出min-height:100%,这样如果内容少了,那么页面底部会占据完整的视口高度和页脚.当内容增加时,页脚会随着内容向下移动并保持坚持到底.

JS小提琴演奏演示:http://jsfiddle.net/3L3h64qo/2/

CSS

html{
  position:relative; 
  min-height: 100%;
}
/*Normalize html and body elements,this style is just good to have*/
html,body{
  margin:0;
  padding:0;
}
.pageContentWrapper{
  margin-bottom:100px;/* Height of footer*/
} 
.footer{
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height:100px;
    background:#ccc;
}
Run Code Online (Sandbox Code Playgroud)

HTML

   <html>
    <body>
        <div class="pageContentWrapper">
            <!-- All the page content goes here-->
        </div>
        <div class="footer">
        </div>
    </body>
    </html>
Run Code Online (Sandbox Code Playgroud)


Jos*_*tos 9

然而,另一个非常简单的解决方案就是这个:

html, body {
    height: 100%;
    width: 100%;
    margin: 0;
    display: table;
}

footer {
    background-color: grey;
    display: table-row;
    height: 0;
}
Run Code Online (Sandbox Code Playgroud)

的jsfiddle

诀窍是使用display:table整个文档,并display:table-rowheight:0页脚.

由于页脚是唯一具有显示效果的正文子table-row,因此它会在页面底部呈现.


Kar*_*yan 8

footer {
  margin-top:calc(5% + 60px);
}
Run Code Online (Sandbox Code Playgroud)

这很好用


Ben*_*son 7

需要注意的一件事是移动设备,因为它们以"不寻常"的方式实现了视口的想法:

http://developer.apple.com/library/ios/#documentation/AppleApplications/Reference/SafariWebContent/UsingtheViewport/UsingtheViewport.html#//apple_ref/doc/uid/TP40006509-SW25

因此,使用position: fixed;(正如我在其他地方推荐的那样)通常不是可行的方法.当然,这取决于你所追求的确切行为.

我使用过的,在桌面和移动设备上运行良好的是:

<body>
    <div id="footer"></div>
</body>
Run Code Online (Sandbox Code Playgroud)

body {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 0;
    margin: 0;
}

#footer {
    position: absolute;
    bottom: 0;
}
Run Code Online (Sandbox Code Playgroud)

  • 这在我的浏览器中不起作用(最近的Chromium,桌面) (4认同)

Aar*_*del 6

做这个

<footer style="position: fixed; bottom: 0; width: 100%;"> </footer>
Run Code Online (Sandbox Code Playgroud)

您还可以阅读所有现代浏览器都支持的flex

更新:我读了关于flex并试了一下.它对我有用.希望它能为你做同样的事情.这是我实现的方式.这里的主要部分不是ID,而是div

body {
    margin: 0;
    display: flex;
    min-height: 100vh;
    flex-direction: column;
}

main {
    display: block;
    flex: 1 0 auto;
}
Run Code Online (Sandbox Code Playgroud)

在这里,您可以阅读有关flex的更多信息https://css-tricks.com/snippets/css/a-guide-to-flexbox/

请记住,旧版本的IE不支持它.


Pao*_*ino 5

这被称为粘性页脚。一个谷歌搜索它来了一个很大的成果。我已经成功使用了CSS Sticky Footer。但是还有更多。

CSS:

* {
    margin: 0;
}
html, body {
    height: 100%;
}
.wrapper {
    min-height: 100%;
    height: auto !important;
    height: 100%;
    margin: 0 auto -4em;
}
.footer, .push {
    height: 4em;
}
Run Code Online (Sandbox Code Playgroud)

的HTML:

<html>
    <head>
        <link rel="stylesheet" href="layout.css" ... />
    </head>
    <body>
        <div class="wrapper">
            <p>Your website content here.</p>
            <div class="push"></div>
        </div>
        <div class="footer">
            <p>Copyright (c) 2008</p>
        </div>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

此代码的来源

  • 除了链接之外,此答案还需要其他一些细节,您能否详细说明? (9认同)
  • 尝试过-它不适用于我的特定CSS。 (3认同)

Cam*_*amo 5

我只是在这里回答类似的问题:

页脚位置固定在页面底部

我是Web开发领域的新手,我知道这已经得到了答案,但这是我发现的最简单的解决方法,我认为这有所不同。我想要一些灵活的东西,因为Web应用程序的页脚具有动态高度,因此我最终使用了FlexBox和spacer。

  1. 首先设置HTML和正文的高度
html, body {
    height: 100%;
    display: flex;
    flex-direction: column;
    margin: 0px;
}
Run Code Online (Sandbox Code Playgroud)

column在您需要添加标题,英雄或垂直对齐的任何内容的情况下,我假设我们的应用程序有这种行为。

  1. 创建间隔类
.spacer {
    flex: 1; 
}
Run Code Online (Sandbox Code Playgroud)
  1. 所以稍后在您的HTML上可能会像
<html>
  <body>
    <header> Header </header>
    Some content...
    <div class='spacer'></div>
    <footer> Footer </footer>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

您可以在这里使用它 https://codepen.io/anon/pen/xmGZQL


小智 5

为我工作。

#container{ 
  height:100vh; 
  margin:0; 
  display:flex; 
  flex-direction:column; 
}

#footer{
  margin-top:auto; 
}


<div id="container">
   <div id="header">header</div>
   <div id="body">body</div>
   <div id="footer">footer</div>
</div>
Run Code Online (Sandbox Code Playgroud)

#container{ 
  height:100vh; 
  margin:0; 
  display:flex; 
  flex-direction:column; 
}

#footer{
  margin-top:auto; 
}


<div id="container">
   <div id="header">header</div>
   <div id="body">body</div>
   <div id="footer">footer</div>
</div>
Run Code Online (Sandbox Code Playgroud)
#container{ 
  height:100vh; 
  margin:0; 
  display:flex; 
  flex-direction:column; 
}

#footer{
  margin-top:auto; 
}
Run Code Online (Sandbox Code Playgroud)