5 回答
TA贡献1798条经验 获得超3个赞
您需要添加的是
html, body{
height: 100%;
}
这应该可以解决问题
* {
margin: 0;
padding: 0;
}
body {
position: relative;
min-height: 100%;
}
html, body{
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>
TA贡献1802条经验 获得超5个赞
你唯一需要改变的是这个职位的价值。您正在寻找的正确位置是:固定
* {
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;
}
footer {
background-color: red;
position: fixed;
bottom: 0;
width: 100%;
height: 40px;
}
<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>
<footer>
This is my footer, it should always be at the bottom of the page.
</footer>
</body>
</html>
TA贡献1757条经验 获得超8个赞
固定页脚使用position: fixed
这是一个例子
* {
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: fixed;
right: 0;
bottom: 0;
left: 0;
z-index: 1030;
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>
TA贡献1865条经验 获得超7个赞
我建议你去读一下元素的位置。一切顺利。一个很小的更改可以在这里帮助您,只需更改MyFooter类的位置即可。IE
.MyFooter { position: fixed; }
TA贡献1785条经验 获得超4个赞
您可以使用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){
x = document.getElementsByTagName("BODY")[0];
h = window.innerHeight;
x.style = "height: " + h + "px;";
});
if(document.getElementById('main').offsetHeight > h) {
document.getElementById('footer').style = "bottom: auto;";
}
* {
margin: 0;
padding: 0;
}
.Placeholder
{
background-color: blue;
height: 100px;
width: 100%;
}
.MainContainer {
width: 100%;
padding: 0;
margin: 0;
background-color: green;
}
.MyFooter {
position: absolute;
bottom: 0px;
width: 100%;
background-color: red;
padding: 0;
margin: 0;
}
<body>
<div class='Header'>Header</div>
<div class="MainContainer" id="main">
<div class='Placeholder'></div>
</div>
<div class="MyFooter" id="footer">
This is my footer, it should always be at the bottom of the page.
</div>
</body>
这是代码,包含所有“占位符”:( bottom: auto;)
let h = window.innerHeight;
var x = document.getElementsByTagName("BODY")[0];
x.style = "height: " + h + "px;";
window.addEventListener('resize', function(event){
x = document.getElementsByTagName("BODY")[0];
h = window.innerHeight;
x.style = "height: " + h + "px;";
});
if(document.getElementById('main').offsetHeight > h) {
document.getElementById('footer').style = "bottom: auto;";
}
* {
margin: 0;
padding: 0;
}
.Placeholder
{
background-color: blue;
height: 100px;
width: 100%;
}
.MainContainer {
width: 100%;
padding: 0;
margin: 0;
background-color: green;
}
.MyFooter {
position: absolute;
bottom: 0px;
width: 100%;
background-color: red;
padding: 0;
margin: 0;
}
<body>
<div class='Header'>Header</div>
<div class="MainContainer" id="main">
<div class='Placeholder'></div>
<div class='Placeholder'></div>
<div class='Placeholder'></div>
<div class='Placeholder'></div>
<div class='Placeholder'></div>
</div>
<div class="MyFooter" id="footer">
This is my footer, it should always be at the bottom of the page.
</div>
</body>
- 5 回答
- 0 关注
- 175 浏览
添加回答
举报