【问题标题】:Bootstrap 3 Flush footer to bottom. not fixedBootstrap 3 将页脚平齐到底部。不固定
【发布时间】:2014-02-14 23:00:22
【问题描述】:

我正在为我正在设计的网站使用 Bootstrap 3。

我想要一个像这个示例一样的页脚。 Sample

请注意,我不希望它 FIXED,所以 bootstrap navbar-fixed-bottom 不能解决我的问题。我只是希望它始终位于内容的底部并且具有响应性。

任何指南都将不胜感激。


编辑:

对不起,如果我不清楚。 现在发生的情况是,当内容主体没有足够的内容时。我的页脚向上移动,然后在底部留下一个空白区域。

这就是我现在的导航栏

<nav class="navbar navbar-inverse navbar-bottom" style="padding:0 0 120px 0">
        <div class="container">
            <div class="row">
                <div class="col-sm-4">
                    <h5 id='footer-header'> SITEMAP </h3>
                    <div class="col-sm-4" style="padding: 0 0 0 0px">
                        <p>News</p>
                        <p>contact</p>
                    </div>
                    <div class="col-sm-4" style="padding: 0 0 0 0px">
                        <p>FAQ</p>
                        <p>Privacy Policy</p>
                    </div>
                </div>
                <div class="col-sm-4">
                    <h5 id='footer-header'> xxxx </h3>
                    <p>yyyyyyyyyyyyy</p>
                </div>
                <div class="col-sm-4">
                    <h5 id='footer-header'> xxxxx </h3>
                    <p>uuuuuuuuuuuuuuu</p>
                </div>
            </div>
        </div>
    </nav>

CSS

.navbar-bottom {
min-height: 300px;
margin-top: 100px;
background-color: #28364f;
padding-top: 35px;
color:#FFFFFF;
}

【问题讨论】:

  • 您是否有正在处理的示例代码给您带来了问题?
  • 好像是普通的页脚,如果不想固定,可以更新样式。你遇到了什么问题?
  • 当它是 HTML 中的最后一件事时,它总是在底部。需要一个代码示例来理解问题所在的令人困惑的问题。
  • @davidpauljunior:那些是粘性页脚,不是我要找的

标签: html css twitter-bootstrap footer


【解决方案1】:

这里有一个来自引导程序的简化解决方案(您不需要创建新类):http://getbootstrap.com/examples/sticky-footer-navbar/

当您打开该页面时,右键单击浏览器并“查看源代码”并打开 sticky-footer-navbar.css 文件 (http://getbootstrap.com/examples/sticky-footer-navbar/sticky-footer-navbar.css)

你可以看到你只需要这个CSS

/* Sticky footer styles
-------------------------------------------------- */
html {
  position: relative;
  min-height: 100%;
}
body {
  /* Margin bottom by footer height */
  margin-bottom: 60px;
}
.footer {
  position: absolute;
  bottom: 0;
  width: 100%;
  /* Set the fixed height of the footer here */
  height: 60px;
  background-color: #f5f5f5;
}

对于这个 HTML

<html>
    ...
    <body>
        <!-- Begin page content -->
        <div class="container">
        </div>
        ...

        <footer class="footer">
        </footer>
    </body>
</html>

【讨论】:

  • 这(来自 Bootstrap 文档/示例)应该是公认的答案。
  • 对我来说这不起作用,因为我的页脚相当高。如果您只想要一个段落,但我的有几列,那就太好了 - 使用这种方法,当我缩小到较小的尺寸时,我最终会在页脚下留出一个边距
  • 这不是 Bootstraps 粘页脚解决方案吗?我不认为OP是这个意思。对我来说,页脚很长,但页脚出现在屏幕底部,但没有出现在内容的底部(即内容上方)。我不得不改变 Position: absolute;到相对(在 .footer 类中)以使其正常工作。
  • 如果该主题的标题中没有“未修复”,这将是正确答案...
  • 这仅适用于固定大小的页脚,我更喜欢@ChristopherTan 提出的 Philip Walton 的解决方案,它超级纤薄且与页脚的高度无关
【解决方案2】:

请参见下面的示例。如果页面内容较少,这会将您的页脚固定在底部,如果页面内容较多,则其行为类似于普通页脚。

CSS

* {
    margin: 0;
}
html, body {
    height: 100%;
}
.wrapper {
    min-height: 100%;
    height: 100%;
    margin: 0 auto -155px; /* the bottom margin is the negative value of the footer's height */
}
.footer, .push {
    height: 155px; /* .push must be the same height as .footer */
}

HTML

<div class="wrapper">
  <p>Your website content here.</p>
  <div class="push"></div>
</div>
<div class="footer">
  <p>Copyright (c) 2008</p>
</div>

更新:新版本的 Bootstrap 演示了如何在不添加包装器的情况下添加粘性页脚。有关详细信息,请参阅 Jboy Flaga 的回答。

【讨论】:

  • 您的链接已损坏
  • 为什么wrapper中有两个height属性定义?
  • @HardlyNoticeable 一个是 min-width 以强制包装器,即使内容较少。
  • 仍然想知道为什么有两个高度属性定义。你没有回答@SurjithSM
  • @Erowlin 这是一个错误。你不需要两个高度属性。只有 min-height 就足够了,编辑了答案。
【解决方案3】:

使用 flexbox,因为您可以随意使用它。 bootstrap 4 提供的解决方案仍然在响应式布局中寻找重叠内容,例如:它会在移动视图中中断,我遇到的最巧妙的技巧是使用此处显示的 flexbox 解决方案演示:(https://philipwalton.github.io/solved-by-flexbox/demos/sticky-footer/)这样我们不必须处理现在已经过时的解决方案的固定高度问题...无论您使用哪种解决方案,此解决方案都适用于引导程序 3 和 4。

<body class="Site">
  <header>…</header>
  <main class="Site-content">…</main>
  <footer>…</footer>
</body>

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

.Site-content {
  flex: 1;
}

【讨论】:

  • 这是一个非常聪明的解决方案。我认为 CSS 提供的东西太多,我们甚至不知道如何充分利用它。
  • 我喜欢这个解决方案。太棒了。
【解决方案4】:

更新:这并不能直接完整地回答问题,但其他人可能会觉得这很有用。

这是响应式页脚的 HTML

<footer class="footer navbar-fixed-bottom">
     <div class="container">
     </div>
</footer>

对于 CSS

footer{
    width:100%;
    min-height:100px;    
    background-color: #222; /* This color gets inverted color or you can add navbar inverse class in html */
}

注意:在发布此问题时,上述代码行不会将页脚推到页面内容下方;但是当页面上的内容很少时,它会阻止您的页脚在页面中途爬行。有关将页脚推到页面内容下方的示例,请查看此处http://getbootstrap.com/examples/sticky-footer/

【讨论】:

  • 这实际上是一个糟糕的解决方案,因为页脚在它们相遇时会掩盖主要内容。您需要将主内容容器的padding-bottom 设置为等于页脚的高度。您需要在页面loadresize 事件以及页脚DOMSubtreeModified 上动态执行此操作。
【解决方案5】:

对于那些仍在苦苦挣扎的人,答案的解决方案不能完全按照你的意愿工作,这是我找到的最简单的解决方案。

包装您的主要内容并为其指定最小视图高度。 将 60 调整为您的风格所需的任何值。

<div id="content" style="min-height:60vh"></div>
<div id="footer"></div>

就是这样,而且效果很好。

【讨论】:

  • 我遇到了与所讨论的问题相同的问题。在这里尝试了几乎所有排名靠前的答案之后,这是唯一对我有用的答案。它适用于各种尺寸。我所做的唯一改变是“最小高度:70vh”。这取决于您网站的页眉和页脚大小。
  • 这应该更高,完全符合要求。
  • 这符合我的需要。只需调整 min-height 值,直到获得所需的结果。
  • 太好了,这是解决页脚放置问题最简单有效的方法!
【解决方案6】:

Galen Gidman 发布了一个非常好的解决方案,可以解决没有固定高度的响应式粘性页脚问题。你可以在他的博客上找到他的完整解决方案:http://galengidman.com/2014/03/25/responsive-flexible-height-sticky-footers-in-css/

HTML

<header class="page-row">
  <h1>Site Title</h1>
</header>

<main class="page-row page-row-expanded">
  <p>Page content goes here.</p>
</main>

<footer class="page-row">
  <p>Copyright, blah blah blah.</p>
</footer>

CSS

html,
body { height: 100%; }

body {
  display: table;
  width: 100%;
}

.page-row {
  display: table-row;
  height: 1px;
}

.page-row-expanded { height: 100%; }

【讨论】:

  • 这似乎与引导程序兼容,因此应该是公认的答案,恕我直言。这并不完美,因为似乎出现了水平滚动条。
  • 弗洛里安:我没有水平滚动条。
【解决方案7】:

对于纯 CSS 解决方案,在第二个 &lt;hr&gt; 之后滚动。第一个是最初的答案(在 2016 年给出)


上述解决方案的主要缺陷是它们的页脚固定高度
而这在现实世界中并不适用,在现实世界中,人们使用数以万计的设备,并且有在你最不期望的时候旋转它们的坏习惯,**噗!**页脚后面的页面内容!

在现实世界中,您需要一个函数来计算页脚高度并动态调整页面内容的padding-bottom 以适应该高度。 您需要在页面loadresize 事件以及页脚的DOMSubtreeModified 上运行这个小函数(以防您的页脚异步动态更新,或者它包含在交互时会改变大小的动画元素)。

这是一个概念证明,使用 jQuery v3.0.0 和 Bootstrap v4-alpha,但没有理由不能在每个较低版本上工作。

jQuery(document).ready(function($) {
  $.fn.accomodateFooter = function() {
    var footerHeight = $('footer').outerHeight();
    $(this).css({
      'padding-bottom': footerHeight + 'px'
    });
  }
  $('footer').on('DOMSubtreeModified', function() {
    $('body').accomodateFooter();
  })
  $(window).on('resize', function() {
    $('body').accomodateFooter();
  })
  $('body').accomodateFooter();
  window.addMoreContentToFooter = function() {
    var f = $('footer');
    f.append($('<p />', {
        text: "Human give me attention meow flop over sun bathe licks your face wake up wander around the house making large amounts of noise jump on top of your human's bed and fall asleep again. Throwup on your pillow sun bathe. The dog smells bad jump around on couch, meow constantly until given food, so nap all day, yet hiss at vacuum cleaner."
      }))
      .append($('<hr />'));
  }
});
body {
  min-height: 100vh;
  position: relative;
}

footer {
  background-color: rgba(0, 0, 0, .65);
  color: white;
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 1.5rem;
  display: block;
}

footer hr {
  border-top: 1px solid rgba(0, 0, 0, .42);
  border-bottom: 1px solid rgba(255, 255, 255, .21);
}
<link href="http://v4-alpha.getbootstrap.com/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="http://v4-alpha.getbootstrap.com/examples/starter-template/starter-template.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.2.0/js/tether.min.js"></script>
<script src="http://v4-alpha.getbootstrap.com/dist/js/bootstrap.min.js"></script>

<nav class="navbar navbar-toggleable-md navbar-inverse bg-inverse fixed-top">
  <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
  <a class="navbar-brand" href="#">Navbar</a>

  <div class="collapse navbar-collapse" id="navbarsExampleDefault">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Link</a>
      </li>
      <li class="nav-item">
        <a class="nav-link disabled" href="#">Disabled</a>
      </li>
      <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="http://example.com" id="dropdown01" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdown</a>
        <div class="dropdown-menu" aria-labelledby="dropdown01">
          <a class="dropdown-item" href="#">Action</a>
          <a class="dropdown-item" href="#">Another action</a>
          <a class="dropdown-item" href="#">Something else here</a>
        </div>
      </li>
    </ul>
    <form class="form-inline my-2 my-lg-0">
      <input class="form-control mr-sm-2" type="text" placeholder="Search">
      <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
    </form>
  </div>
</nav>

<div class="container">
  <div class="starter-template">
    <h1>Feed that footer - not a game (yet!)</h1>
    <p class="lead">You will notice the body bottom padding is growing to accomodate the height of the footer as you feed it (so the page contents do not get covered by it).</p><button class="btn btn-warning" onclick="window.addMoreContentToFooter()">Feed that footer</button>
    <hr />
    <blockquote class="lead"><strong>Note:</strong> I used jQuery <code>v3.0.0</code> and Bootstrap <code>v4-alpha</code> but there is no reason why it shouldn't work with lower versions of each.</blockquote>
  </div>
</div>

<footer>I am a footer with dynamic content.
  <hr />
</footer>

最初,我发布了这个解决方案here,但我意识到如果在这个问题下发布它可能会帮助更多人。

注意:我特意将$([selector]).accomodateFooter() 包装为一个jQuery 插件,因此它可以在任何DOM 元素上运行,因为在大多数布局中它不是$('body') 的@987654334 @ 需要调整,但某些页面包装元素带有 position:relative(通常是 footer 的直接父级)。


后期编辑(最初回答后 3 年以上):

在这一点上,我不再认为使用 JavaScript 在页面底部定位动态内容页脚是可以接受的。它可以单独使用 CSS 实现,使用 flexbox,闪电般快速,跨浏览器。

这里是:

// Left this in so you could inject content into the footer and test it:
// (but it's no longer sizing the footer)

function addMoreContentToFooter() {
  var f = $('footer');
  f.append($('<p />', {
      text: "Human give me attention meow flop over sun bathe licks your face wake up wander around the house making large amounts of noise jump on top of your human's bed and fall asleep again. Throwup on your pillow sun bathe. The dog smells bad jump around on couch, meow constantly until given food, so nap all day, yet hiss at vacuum cleaner."
    }))
    .append($('<hr />'));
}
.wrapper {
  min-height: 100vh;
  padding-top: 54px;
  display: flex;
  flex-direction: column;
}

.wrapper>* {
  flex-grow: 0;
}

.wrapper>main {
  flex-grow: 1;
}

footer {
  background-color: rgba(0, 0, 0, .65);
  color: white;
  width: 100%;
  padding: 1.5rem;
}

footer hr {
  border-top: 1px solid rgba(0, 0, 0, .42);
  border-bottom: 1px solid rgba(255, 255, 255, .21);
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>

<div class="wrapper">
  <nav class="navbar navbar-expand-lg navbar-light bg-light fixed-top">
  <a class="navbar-brand" href="#">Navbar</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>

  <div class="collapse navbar-collapse" id="navbarSupportedContent">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Link</a>
      </li>
      <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
          Dropdown
        </a>
        <div class="dropdown-menu" aria-labelledby="navbarDropdown">
          <a class="dropdown-item" href="#">Action</a>
          <a class="dropdown-item" href="#">Another action</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="#">Something else here</a>
        </div>
      </li>
      <li class="nav-item">
        <a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
      </li>
    </ul>
    <form class="form-inline my-2 my-lg-0">
      <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
      <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
    </form>
  </div>
</nav>
  <main>
    <div class="container">
      <div class="starter-template">
        <h1>Feed that footer - not a game (yet!)</h1>
        <p class="lead">Footer won't ever cover the body contents, as its not fixed. It's simply placed at the bottom when the page should be shorter using `min-height:100vh` on container and using flexbox to push it down.</p><button class="btn btn-warning" onclick="addMoreContentToFooter()">Feed that footer</button>
        <hr />
        <blockquote class="lead">
          <strong>Note:</strong> This example uses current latest versions of jQuery (<code>3.4.1.slim</code>) and Bootstrap (<code>4.4.1</code>) (unlike the one above).
        </blockquote>
      </div>
    </div>
  </main>
  <footer>I am a footer with dynamic content.
    <hr />
  </footer>
</div>

【讨论】:

    【解决方案8】:

    此方法使用最少的标记。只需将所有内容放在一个 .wrapper 中,它的 padding-bottom 和负 margin-bottom 等于页脚高度(在我的示例中为 100px)。

    html, body {
        height: 100%;
    }
    
    /* give the wrapper a padding-bottom and negative margin-bottom equal to the footer height */
    
    .wrapper {
        min-height: 100%;
        height: auto;
        margin: 0 auto -100px;
        padding-bottom: 100px;
    }
    .footer {
        height: 100px;
    }
    
    <body>
    
    <div class="wrapper">
      <p>Your website content here.</p>
    </div>
    <div class="footer">
       <p>Copyright (c) 2014</p>
    </div>
    
    </body>
    

    【讨论】:

      【解决方案9】:

      或者这个

      <footer class="navbar navbar-default navbar-fixed-bottom">
          <p class="text-muted" align="center">This is a footer</p>
      </footer>
      

      【讨论】:

        【解决方案10】:

        对于引导程序:

        <div class="navbar-fixed-bottom row-fluid">
          <div class="navbar-inner">
            <div class="container">
              Text
            </div>
          </div>
        </div>
        

        【讨论】:

        • 该问题明确指出navbar-fixed-bottom 不能解决问题。
        【解决方案11】:

        这些解决方案中没有一个完全适合我,因为我在页脚中使用了 navbar-inverse 类。但我确实得到了一个有效且无 Javascript 的解决方案。使用 Chrome 来帮助形成媒体查询。页脚的高度会随着屏幕调整大小而变化,因此您必须注意这一点并进行相应调整。您的页脚内容(我设置 id="footer" 来定义我的内容)应该使用 postion=absolute 和 bottom=0 将其保持在底部。宽度:100%。这是我的带有媒体查询的 CSS。您必须调整 min-width 和 max-width 并添加或删除一些元素:

        #footer {
          position: absolute;
          color:  #ffffff;
          width: 100%;
          bottom: 0; 
        }
        @media only screen and (min-width:1px) and (max-width: 407px)  {
            body {
                margin-bottom: 275px;
            }
        
            #footer {
                height: 270px; 
            }
        }
        @media only screen and (min-width:408px) and (max-width: 768px)  {
            body {
                margin-bottom: 245px;
            }
        
            #footer {
                height: 240px; 
            }
        }
        @media only screen and (min-width:769px)   {
            body {
                margin-bottom: 125px;
            }
        
            #footer {
                height: 120px; 
            }
        }
        

        【讨论】:

          【解决方案12】:

          这就是我使用 Flexbox 的方法:

          .body-class {
              display: flex;
              min-height: 100vh;
              flex-direction: column;
          }
          
          .body-content {
              flex: 1 0 auto;
              width: 100%;
          }
          
          .footer {
              width: 100%;
              height: 60px;
              background-color: #f5f5f5;
              flex: none;
          }
          

          来源:https://philipwalton.github.io/solved-by-flexbox/demos/sticky-footer/

          【讨论】:

            【解决方案13】:

            使用任何类并使其高度固定。它解决了我的问题。

            <div class="container"></div>
            <footer></footer>
            

            CSS:

            .container{
                min-height: 60vh;
            }
            

            【讨论】:

              猜你喜欢
              • 2014-10-17
              • 2013-09-25
              • 2022-01-10
              • 2015-02-02
              • 1970-01-01
              • 2017-02-01
              • 2017-04-04
              • 1970-01-01
              相关资源
              最近更新 更多