页面底部或内容的页脚,以较低者为准

我有以下结构:


<body>

    <div id="main-wrapper">

        <header>

        </header>

        <nav>

        </nav>

        <article>

        </article>

        <footer>

        </footer>

    </div>

</body>

我<article>使用javascript 动态加载内容。因此,<article>块的高度可以改变。


我希望<footer>当有很多内容时,块位于页面的底部,或者当只有几行内容时,在浏览器窗口的底部。


目前我可以做其中一个......但不是两个。


所以有人知道我怎么做 - 让它<footer>坚持到页面/内容的底部或屏幕的底部,取决于哪个更低。


翻过高山走不出你
浏览 506回答 3
3回答

杨魅力

Ryan Fait的粘性页脚非常好,但我发现它的基本结构缺乏*。Flexbox版本如果你足够幸运,你可以使用flexbox而不需要支持旧的浏览器,粘性页脚变得非常简单,并支持动态大小的页脚。使用flexbox将页脚粘到底部的技巧是让同一容器中的其他元素垂直弯曲。所需要的只是一个全高度的包装元素,display: flex并且至少有一个兄弟的flex值大于0:CSS:html,body {&nbsp; height: 100%;&nbsp; margin: 0;&nbsp; padding: 0;}#main-wrapper {&nbsp; display: flex;&nbsp; flex-direction: column;&nbsp; min-height: 100%;}article {&nbsp; flex: 1;}html,body {&nbsp; height: 100%;&nbsp; margin: 0;&nbsp; padding: 0;}#main-wrapper {&nbsp; display: -webkit-box;&nbsp; display: -ms-flexbox;&nbsp; display: flex;&nbsp; -webkit-box-orient: vertical;&nbsp; -webkit-box-direction: normal;&nbsp; &nbsp; &nbsp; -ms-flex-direction: column;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; flex-direction: column;&nbsp; min-height: 100%;}article {&nbsp; -webkit-box-flex: 1;&nbsp; &nbsp; &nbsp; -ms-flex: 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; flex: 1;}header {&nbsp; background-color: #F00;}nav {&nbsp; background-color: #FF0;}article {&nbsp; background-color: #0F0;}footer {&nbsp; background-color: #00F;}<div id="main-wrapper">&nbsp; &nbsp;<header>&nbsp; &nbsp; &nbsp;here be header&nbsp; &nbsp;</header>&nbsp; &nbsp;<nav>&nbsp; &nbsp;</nav>&nbsp; &nbsp;<article>&nbsp; &nbsp; &nbsp;here be content&nbsp; &nbsp;</article>&nbsp; &nbsp;<footer>&nbsp; &nbsp; &nbsp;here be footer&nbsp; &nbsp;</footer></div>如果你不能使用flexbox,我选择的基本结构是:<div class="page">&nbsp; <div class="page__inner">&nbsp; &nbsp; <header class="header">&nbsp; &nbsp; &nbsp; <div class="header__inner">&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </header>&nbsp; &nbsp; <nav class="nav">&nbsp; &nbsp; &nbsp; <div class="nav__inner">&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </nav>&nbsp; &nbsp; <main class="wrapper">&nbsp; &nbsp; &nbsp; <div class="wrapper__inner">&nbsp; &nbsp; &nbsp; &nbsp; <div class="content">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="content__inner">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="sidebar">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="sidebar__inner">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </main>&nbsp; &nbsp; <footer class="footer">&nbsp; &nbsp; &nbsp; <div class="footer__inner">&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </footer>&nbsp; </div></div>哪个不是很远:<div id="main-wrapper">&nbsp; &nbsp; <header>&nbsp; &nbsp; </header>&nbsp; &nbsp; <nav>&nbsp; &nbsp; </nav>&nbsp; &nbsp; <article>&nbsp; &nbsp; </article>&nbsp; &nbsp; <footer>&nbsp; &nbsp; </footer></div>让页脚粘住的技巧是将页脚锚定到其包含元素的底部填充。这要求页脚的高度是静态的,但我发现页脚通常具有静态高度。HTML:<div id="main-wrapper">&nbsp; &nbsp; ...&nbsp; &nbsp; <footer>&nbsp; &nbsp; </footer></div>CSS:#main-wrapper {&nbsp; &nbsp; padding: 0 0 100px;&nbsp; &nbsp; position: relative;}footer {&nbsp; &nbsp; bottom: 0;&nbsp; &nbsp; height: 100px;&nbsp; &nbsp; left: 0;&nbsp; &nbsp; position: absolute;&nbsp; &nbsp; width: 100%;}#main-wrapper {&nbsp; padding: 0 0 100px;&nbsp; position: relative;}footer {&nbsp; bottom: 0;&nbsp; height: 100px;&nbsp; left: 0;&nbsp; position: absolute;&nbsp; width: 100%;}header {&nbsp; background-color: #F00;}nav {&nbsp; background-color: #FF0;}article {&nbsp; background-color: #0F0;}footer {&nbsp; background-color: #00F;}<div id="main-wrapper">&nbsp; &nbsp;<header>&nbsp; &nbsp; &nbsp;here be header&nbsp; &nbsp;</header>&nbsp; &nbsp;<nav>&nbsp; &nbsp;</nav>&nbsp; &nbsp;<article>&nbsp; &nbsp; &nbsp;here be content&nbsp; &nbsp;</article>&nbsp; &nbsp;<footer>&nbsp; &nbsp; &nbsp;here be footer&nbsp; &nbsp;</footer></div>在页脚固定的情况下#main-wrapper,您现在需要#main-wrapper至少达到页面的高度,除非其子项更长。这是通过做#main-wrapper有一个min-height的100%。你还必须记住它的父母,html并且还body需要和页面一样高。CSS:html,body {&nbsp; &nbsp; height: 100%;&nbsp; &nbsp; margin: 0;&nbsp; &nbsp; padding: 0;}#main-wrapper {&nbsp; &nbsp; min-height: 100%;&nbsp; &nbsp; padding: 0 0 100px;&nbsp; &nbsp; position: relative;}footer {&nbsp; &nbsp; bottom: 0;&nbsp; &nbsp; height: 100px;&nbsp; &nbsp; left: 0;&nbsp; &nbsp; position: absolute;&nbsp; &nbsp; width: 100%;}html,body {&nbsp; height: 100%;&nbsp; margin: 0;&nbsp; padding: 0;}#main-wrapper {&nbsp; min-height: 100%;&nbsp; padding: 0 0 100px;&nbsp; position: relative;}footer {&nbsp; bottom: 0;&nbsp; height: 100px;&nbsp; left: 0;&nbsp; position: absolute;&nbsp; width: 100%;}header {&nbsp; background-color: #F00;}nav {&nbsp; background-color: #FF0;}article {&nbsp; background-color: #0F0;}footer {&nbsp; background-color: #00F;}&nbsp;<div id="main-wrapper">&nbsp; &nbsp;<header>&nbsp; &nbsp; &nbsp;here be header&nbsp; &nbsp;</header>&nbsp; &nbsp;<nav>&nbsp; &nbsp;</nav>&nbsp; &nbsp;<article>&nbsp; &nbsp; &nbsp;here be content&nbsp; &nbsp;</article>&nbsp; &nbsp;<footer>&nbsp; &nbsp; &nbsp;here be footer&nbsp; &nbsp;</footer></div>当然,你应该质疑我的判断,因为这段代码迫使页脚从页面底部掉下来,即使没有内容。最后一个诀窍是要改变由所使用的盒模型#main-wrapper,因此认为min-height的100%包括100px填充。CSS:html,body {&nbsp; &nbsp; height: 100%;&nbsp; &nbsp; margin: 0;&nbsp; &nbsp; padding: 0;}#main-wrapper {&nbsp; &nbsp; box-sizing: border-box;&nbsp; &nbsp; min-height: 100%;&nbsp; &nbsp; padding: 0 0 100px;&nbsp; &nbsp; position: relative;}footer {&nbsp; &nbsp; bottom: 0;&nbsp; &nbsp; height: 100px;&nbsp; &nbsp; left: 0;&nbsp; &nbsp; position: absolute;&nbsp; &nbsp; width: 100%;}html,body {&nbsp; height: 100%;&nbsp; margin: 0;&nbsp; padding: 0;}#main-wrapper {&nbsp; box-sizing: border-box;&nbsp; min-height: 100%;&nbsp; padding: 0 0 100px;&nbsp; position: relative;}footer {&nbsp; bottom: 0;&nbsp; height: 100px;&nbsp; left: 0;&nbsp; position: absolute;&nbsp; width: 100%;}header {&nbsp; background-color: #F00;}nav {&nbsp; background-color: #FF0;}article {&nbsp; background-color: #0F0;}footer {&nbsp; background-color: #00F;}&nbsp;<div id="main-wrapper">&nbsp; &nbsp;<header>&nbsp; &nbsp; &nbsp;here be header&nbsp; &nbsp;</header>&nbsp; &nbsp;<nav>&nbsp; &nbsp;</nav>&nbsp; &nbsp;<article>&nbsp; &nbsp; &nbsp;here be content&nbsp; &nbsp;</article>&nbsp; &nbsp;<footer>&nbsp; &nbsp; &nbsp;here be footer&nbsp; &nbsp;</footer></div>你有它,一个粘性页脚与你原来的HTML结构。只要确保footer's height等于#main-wrapper's padding-bottom,就应该设置好。*我指手画脚既成事实的结构的原因是因为它设置.footer,并.header在不同的层次级别的元素,同时增加不必要的.push元素。
打开App,查看更多内容
随时随地看视频慕课网APP