HTML   发布时间:2022-04-14  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了html – 当页面高度使用CSS动态更改时,将页脚保持在页面底部大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
有很多主题,提供解决方案,以保持页脚在页面底部.但是,我正在努力让它发挥作用.

问题是页面可以动态地改变它的高度.

使用the current solution I’m using,页脚位于页面底部.但是当页面高度动态变化时,页脚将保持在其确切位置.

页脚的CSS如下:

#footer {
    position: absolute;
    right: 0;
    bottom: 0;
    left: 0;
}

html和body标签具有以下规则:

html,body {
    min-height: 100%;
    height: 100%;
}

请参阅下面的代码段以获取可视化演示(最适合在全窗口模式下使用)

$(document).ready(function() {
  
  var button = $("#addContent");
  var lorem = "<p>Proin cursus odio quis neque porttitor pretium. Duis cursus dolor mi,quis blandit eros Dictum vitae. Mauris tempus turpis non Leo commodo sagittis ac ac urna. Vivamus aliquet euismod posuere. Suspendisse at semper mauris. Phasellus blandit convallis Tincidunt. Maecenas elementum ullamcorper risus,a vestibulum ex accumsan sed. Nulla facilisi.</p><p>Proin cursus odio quis neque porttitor pretium. Duis cursus dolor mi,a vestibulum ex accumsan sed. Nulla facilisi.</p>";
  
  button.click(function() {
    
    $("main button").before(lorem);
    
  });
  
});
* {
  box-sizing: border-box;
}

body {
  margin: 0;
}

header {
  BACkground: #333;
  color: #fff;
  padding: 25px;
}

main {
  padding: 25px;
}

main h3 {
  margin-top: 0;
}

code {
  BACkground: #f1f1f1;
  padding: 0 5px;
}

footer {
  position: absolute;
  BACkground: #ededed;
  padding: 25px;
  color: #000;
  right: 0;
  bottom: 0;
  left: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<header>
  <p>Just a sample header</p>
</header>

<main>
  <h3>Some sample content</h3>
  <p>Click on the <code>button</code> to see what i mean.</p>
  <p>When the <code>heigth</code> of the page dynamically changes,the <code>footer</code> will stay at its exact position.</p>
  <button id="addContent">Click to add more content</button>
</main>

<footer>
  <p>The footer</p>
</footer>

如何让CSS知道高度变化?并将该页脚保留在文档的底部而不是它停留在窗口的底部?

@H_262_25@解决方法
首先,如果你想使用position:absolute,那么父元素应该是初始位置的其他元素,比如position:relative或者它将看起来第一个父元素是相对的位置.因此,如果你添加位置:相对于body,页脚将是动态的.

但绝对定位的元素已从文档流中完全删除,因此在这种情况下它将与其他元素重叠,但如果我们添加变换,我们可以解决:translateY(100%)

所以最后你会得到:

body {
    margin: 0;
    position: relative;
}

footer {
    position: absolute;
    BACkground: #ededed;
    padding: 25px;
    color: #000;
    right: 0;
    bottom: 0;
    left: 0;
    transform: translateY(100%);
}

大佬总结

以上是大佬教程为你收集整理的html – 当页面高度使用CSS动态更改时,将页脚保持在页面底部全部内容,希望文章能够帮你解决html – 当页面高度使用CSS动态更改时,将页脚保持在页面底部所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。