使页脚始终位于底部

我正在努力使我的网站的页脚始终位于网页的底部。无论网页内容太小而无法占据整个屏幕,还是内容太多需要滚动,页脚都应该始终位于屏幕底部......例如......我已经做了以下代码可帮助演示该问题...


问题是,虽然只有一个“占位符”div,但页脚只是被推到其下方(它应该位于页面的最底部,就像所有占位符都取消注释时发生的情况一样)。


我怎样才能实现这个目标?


* {

  margin: 0;

  padding: 0;

}


body {

  position: relative;

  min-height: 100%;

}


.Placeholder

{

  background-color: blue;

  height: 100px;

  width: 100%;

}


.MainContainer {

  width: 100%;

  padding: 0;

  margin: 0;

  background-color: green;

}


.MyFooter {

  position: absolute;

  bottom: 0;

  width: 100%;

  background-color: red;

  padding: 0;

  margin: 0;

}

<html>

  <head>

  </head>

  <body>

    <div class='Header'>Header</div>

    <div class="MainContainer">

      <div class='Placeholder'></div>

      <!-- Uncomment these to populate the container.

      <div class='Placeholder'></div>

      <div class='Placeholder'></div>

      <div class='Placeholder'></div>

      <div class='Placeholder'></div>

      !-->   

    </div>

    <div class="MyFooter">

      This is my footer, it should always be at the bottom of the page.

    </div>

  </body>

</html>


PIPIONE
浏览 177回答 5
5回答

呼如林

您需要添加的是html, body{height: 100%;}这应该可以解决问题* {&nbsp; margin: 0;&nbsp; padding: 0;}body {&nbsp; position: relative;&nbsp; min-height: 100%;}html, body{height: 100%;}.Placeholder{&nbsp; background-color: blue;&nbsp; height: 100px;&nbsp; width: 100%;}.MainContainer {&nbsp; width: 100%;&nbsp; padding: 0;&nbsp; margin: 0;&nbsp; background-color: green;}.MyFooter {&nbsp; position: absolute;&nbsp; bottom: 0;&nbsp; width: 100%;&nbsp; background-color: red;&nbsp; padding: 0;&nbsp; margin: 0;}<html>&nbsp; <head>&nbsp; </head>&nbsp; <body>&nbsp; &nbsp; <div class='Header'>Header</div>&nbsp; &nbsp; <div class="MainContainer">&nbsp; &nbsp; &nbsp; <div class='Placeholder'></div>&nbsp; &nbsp; &nbsp; <!-- Uncomment these to populate the container.&nbsp; &nbsp; &nbsp; <div class='Placeholder'></div>&nbsp; &nbsp; &nbsp; <div class='Placeholder'></div>&nbsp; &nbsp; &nbsp; <div class='Placeholder'></div>&nbsp; &nbsp; &nbsp; <div class='Placeholder'></div>&nbsp; &nbsp; &nbsp; !-->&nbsp; &nbsp;&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="MyFooter">&nbsp; &nbsp; &nbsp; This is my footer, it should always be at the bottom of the page.&nbsp; &nbsp; </div>&nbsp; </body></html>

慕后森

你唯一需要改变的是这个职位的价值。您正在寻找的正确位置是:固定* {&nbsp; margin: 0;&nbsp; padding: 0;}body {&nbsp; position: relative;&nbsp; min-height: 100%;}.Placeholder{&nbsp; background-color: blue;&nbsp; height: 100px;&nbsp; width: 100%;}.MainContainer {&nbsp; width: 100%;&nbsp; padding: 0;&nbsp; margin: 0;&nbsp; background-color: green;}footer {&nbsp;background-color: red;&nbsp;position: fixed;&nbsp;bottom: 0;&nbsp;width: 100%;&nbsp;height: 40px;}<html>&nbsp; <head>&nbsp; </head>&nbsp; <body>&nbsp; &nbsp; <div class='Header'>Header</div>&nbsp; &nbsp; <div class="MainContainer">&nbsp; &nbsp; &nbsp; <div class='Placeholder'></div>&nbsp; &nbsp; &nbsp; <!-- Uncomment these to populate the container.&nbsp; &nbsp; &nbsp; <div class='Placeholder'></div>&nbsp; &nbsp; &nbsp; <div class='Placeholder'></div>&nbsp; &nbsp; &nbsp; <div class='Placeholder'></div>&nbsp; &nbsp; &nbsp; <div class='Placeholder'></div>&nbsp; &nbsp; &nbsp; !-->&nbsp; &nbsp;&nbsp; &nbsp; </div>&nbsp; &nbsp; <footer>&nbsp; &nbsp; &nbsp; This is my footer, it should always be at the bottom of the page.&nbsp; &nbsp; </footer>&nbsp; </body></html>

陪伴而非守候

固定页脚使用position: fixed这是一个例子* {&nbsp; margin: 0;&nbsp; padding: 0;}body {&nbsp; position: relative;&nbsp; min-height: 100%;}.Placeholder{&nbsp; background-color: blue;&nbsp; height: 100px;&nbsp; width: 100%;}.MainContainer {&nbsp; width: 100%;&nbsp; padding: 0;&nbsp; margin: 0;&nbsp; background-color: green;}.MyFooter {&nbsp; &nbsp;position: fixed;&nbsp; &nbsp;right: 0;&nbsp; &nbsp;bottom: 0;&nbsp; &nbsp;left: 0;&nbsp; &nbsp;z-index: 1030;&nbsp; &nbsp;width: 100%;&nbsp; &nbsp;background-color: red;&nbsp; &nbsp;padding: 0;&nbsp; &nbsp;margin: 0;}<html>&nbsp; <head>&nbsp; </head>&nbsp; <body>&nbsp; &nbsp; <div class='Header'>Header</div>&nbsp; &nbsp; <div class="MainContainer">&nbsp; &nbsp; &nbsp; <div class='Placeholder'></div>&nbsp; &nbsp; &nbsp; <!-- Uncomment these to populate the container.&nbsp; &nbsp; &nbsp; <div class='Placeholder'></div>&nbsp; &nbsp; &nbsp; <div class='Placeholder'></div>&nbsp; &nbsp; &nbsp; <div class='Placeholder'></div>&nbsp; &nbsp; &nbsp; <div class='Placeholder'></div>&nbsp; &nbsp; &nbsp; !-->&nbsp; &nbsp;&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="MyFooter">&nbsp; &nbsp; &nbsp; This is my footer, it should always be at the bottom of the page.&nbsp; &nbsp; </div>&nbsp; </body></html>

鸿蒙传说

我建议你去读一下元素的位置。一切顺利。一个很小的更改可以在这里帮助您,只需更改MyFooter类的位置即可。IE.MyFooter {    position: fixed; }

九州编程

您可以使用bottom: 0和bottom: auto来解决此问题。但首先,您应该将正文的高度设置为 100%,以便在有一个“占位符”时页脚可以保留在页面底部。这是代码,只有一个“占位符”:( bottom: 0px;)let h = window.innerHeight;var x = document.getElementsByTagName("BODY")[0];x.style = "height: " + h + "px;";window.addEventListener('resize', function(event){&nbsp; &nbsp; x = document.getElementsByTagName("BODY")[0];&nbsp; &nbsp; h = window.innerHeight;&nbsp; &nbsp; x.style = "height: " + h + "px;";});if(document.getElementById('main').offsetHeight > h) {&nbsp; document.getElementById('footer').style = "bottom: auto;";}* {&nbsp; margin: 0;&nbsp; padding: 0;}.Placeholder{&nbsp; background-color: blue;&nbsp; height: 100px;&nbsp; width: 100%;}.MainContainer {&nbsp; width: 100%;&nbsp; padding: 0;&nbsp; margin: 0;&nbsp; background-color: green;}.MyFooter {&nbsp; position: absolute;&nbsp; bottom: 0px;&nbsp; width: 100%;&nbsp; background-color: red;&nbsp; padding: 0;&nbsp; margin: 0;}<body>&nbsp; <div class='Header'>Header</div>&nbsp; <div class="MainContainer" id="main">&nbsp; &nbsp; <div class='Placeholder'></div>&nbsp; </div>&nbsp; <div class="MyFooter" id="footer">&nbsp; &nbsp; This is my footer, it should always be at the bottom of the page.&nbsp; </div></body>这是代码,包含所有“占位符”:( bottom: auto;)let h = window.innerHeight;var x = document.getElementsByTagName("BODY")[0];x.style = "height: " + h + "px;";window.addEventListener('resize', function(event){&nbsp; &nbsp; x = document.getElementsByTagName("BODY")[0];&nbsp; &nbsp; h = window.innerHeight;&nbsp; &nbsp; x.style = "height: " + h + "px;";});if(document.getElementById('main').offsetHeight > h) {&nbsp; document.getElementById('footer').style = "bottom: auto;";}* {&nbsp; margin: 0;&nbsp; padding: 0;}.Placeholder{&nbsp; background-color: blue;&nbsp; height: 100px;&nbsp; width: 100%;}.MainContainer {&nbsp; width: 100%;&nbsp; padding: 0;&nbsp; margin: 0;&nbsp; background-color: green;}.MyFooter {&nbsp; position: absolute;&nbsp; bottom: 0px;&nbsp; width: 100%;&nbsp; background-color: red;&nbsp; padding: 0;&nbsp; margin: 0;}<body>&nbsp; <div class='Header'>Header</div>&nbsp; <div class="MainContainer" id="main">&nbsp; &nbsp; <div class='Placeholder'></div>&nbsp; &nbsp; <div class='Placeholder'></div>&nbsp; &nbsp; <div class='Placeholder'></div>&nbsp; &nbsp; <div class='Placeholder'></div>&nbsp; &nbsp; <div class='Placeholder'></div>&nbsp; </div>&nbsp; <div class="MyFooter" id="footer">&nbsp; &nbsp; This is my footer, it should always be at the bottom of the page.&nbsp; </div></body>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5