简体   繁体   English

CSS-将页脚贴到页面底部(因此只有在滚动后才可见)而没有html选择器?

[英]CSS- stick footer to bottom of page (so only visible after scroll) WITHOUT html selector?

Alright, Im having CSS problems here. 好的,我在这里遇到CSS问题。 My original question was CSS/html - make footer ONLY visible after scroll? 我最初的问题是CSS / html-仅在滚动后才显示页脚吗? stick to bottom just below visible page area? 坚持到可见页面区域下方的底部? and I achieved the effect I wanted on ONE page with this html: 并且使用此html在一个页面上实现了我想要的效果:

<div class="wrapper">
  <h1 class="headertext">Welcome back.</h1>
</div>
<div class="footer">Footer</div>

And CSS: 和CSS:

* {
  margin: 0;
}

html, body {
  height: 100%;
}

.wrapper {
  background: linear-gradient(to bottom right, #50a3a2 0%, #53e3a6 100%);
  min-height: calc(100% + 142px);
  margin-bottom: -142px;
}

.wrapper:after {
  display: block;
  content: "";
  height: 142px;
}

.footer {
  background-color: white;
  opacity: 0.5;
  height: 142px;
  text-align: center;
}

The problem is that the following: 问题是以下内容:

* {
      margin: 0;
    }

    html, body {
      height: 100%;
    }

which allows this to work has screwed up (really screwed up) every other page on my site and furthermore, doesnt have the same effect on the footer on these pages. 允许此功能的工作已经弄乱了(真的搞砸了)我网站上的所有其他页面,此外,这些页面上的页脚也没有得到相同的效果。 Im looking for a simpler method of sticking the footer to the absolute bottom (just past the page content) JUST using the class selectors, ie no html, body, or *. 我正在寻找一种简单的方法,只需使用类选择器即可将页脚固定在绝对底部(刚好超过页面内容),即不使用html,body或*。

Ive tried putting height: 100% and margin: 0 just in the wrapper and footer since these apply to all, but my footer either remains stuck to top of page or not visible at all. Ive尝试将height: 100%margin: 0仅放在包装器和页脚中,因为它们都适用于所有包装,但是我的页脚要么停留在页面顶部,要么根本看不到。

Here is the CSS Im working with at the moment: 这是目前正在使用的CSS Im:

.wrapper:after {

 height: 100%;
  height: 89px;
}

.wrapper {
     margin-bottom: -89px;
    background: #50a3a2;
    background: -webkit-linear-gradient(top left, #50a3a2 0%, #53e3a6 100%);
    background: linear-gradient(to bottom right, #50a3a2 0%, #53e3a6 100%);
    position: absolute;
    margin: 0;
    width: 100%;
    height: 100%;
    min-height: calc(100% + 89px);
    margin-top: 0px;

    z-index: -1;
    text-align: center;
}
.footer{

    position: absolute;
    bottom: 0px;
height: 89px;
    background-color: rgba(255, 255, 255, 0.7);
    opacity: 0.8;
    text-align: center;
    z-index: -3;
    color: white;
}

Is there any way to accomplish this? 有什么办法可以做到这一点? Again, I don't want a fixed footer, I want it to just regardless of amount of content on page stay stuck just BELOW content. 同样,我不希望有固定的页脚,我希望页面的内容量保持不变,而仅在内容以下。 Im desperate here - have a broken site at the moment 我在此绝望-此刻站点破碎

Will this work for you ? 这对您有用吗?

This is fully dynamic solution where the wrapper takes the remaining space left by the footer . 这是一种完全动态的解决方案,其中wrapper占用了footer剩余的剩余空间。

 html, body { margin: 0; } .container { min-height: 100vh; display: flex; flex-direction: column; } .wrapper { background: linear-gradient(to bottom right, #50a3a2 0%, #53e3a6 100%); flex: 1; padding: 10px; } .footer { background-color: white; opacity: 0.5; padding: 10px; text-align: center; } 
 <div class="container"> <div class="wrapper"> <h1 class="headertext">Welcome back.</h1> </div> <div class="footer">Footer</div> </div> 

If you by no means can use html, body { margin: 0; } 如果您绝不能使用html, body { margin: 0; } html, body { margin: 0; } , another option is using absolute positioning. html, body { margin: 0; } ,另一个选择是使用绝对定位。

 .container { position: absolute; left: 0; top: 0; right: 0; bottom: 0; overflow: auto; display: flex; flex-direction: column; } .wrapper { background: linear-gradient(to bottom right, #50a3a2 0%, #53e3a6 100%); flex: 1; padding: 10px; } .footer { background-color: white; opacity: 0.5; padding: 10px; text-align: center; } 
 <div class="container"> <div class="wrapper"> <h1 class="headertext">Welcome back.</h1> </div> <div class="footer">Footer</div> </div> 

If you can't use flex , use display: table , it has the same dynamic feature for a sticky footer though you need an extra wrapper as a row 如果您不能使用flex ,请使用display: table ,尽管行中需要一个额外的包装器,但它具有与footer相同的动态功能

 html, body { margin: 0; } .container { min-height: 100vh; display: table; width: 100%; } .row { display: table-row; height: 100%; } .row ~ .row { height: 1px; } .wrapper { display: table-cell; background: linear-gradient(to bottom right, #50a3a2 0%, #53e3a6 100%); padding: 10px; } .footer { display: table-cell; background-color: white; opacity: 0.5; padding: 10px; text-align: center; } 
 <div class="container"> <div class="row"> <div class="wrapper"> <h1 class="headertext">Welcome back.</h1> </div> </div> <div class="row"> <div class="footer">Footer</div> </div> </div> 

You can try to use flexbox trick here. 您可以在此处尝试使用flexbox技巧。 JSFiddle JSFiddle

CSS: CSS:

BODY {
    display: flex;
    display: flexbox;
    height: 100vh;
   -ms-flex-direction: column;
   flex-direction: column;
}

.wrapper {
    -ms-flex: 1 0 auto;
    flex: 1 0 auto;
}

If you don't wanna use Body selector you can add min-height: 100vh; 如果您不想使用Body选择器,则可以添加min-height: 100vh; to you .wrapper that will define its min-height as 100% of the viewport. 给您的.wrapper ,它将其最小高度定义为视口的100%。

.wrapper {
    min-height: 100vh;
}

Works fine for me even in IE10. 即使在IE10中,对我来说也很好用。

HTH. HTH。

This would be my approach: https://jsfiddle.net/Ltj0o802/ 这就是我的方法: https//jsfiddle.net/Ltj0o802/

 $("#add").on("click", function() { $("<p>Pellentesque habitant morbi tristique senectus.</p>").insertAfter("#add"); }); 
 html, body { height: 100%; margin: 0; padding: 0; } .wrapper { position: relative; min-height: 100%; background: gray; } .headertext { margin-top: 0; } .content { padding-bottom: 50px; } .footer { background: red; position: absolute; bottom: 0; left: 0; width: 100%; height: 50px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="wrapper"> <div class="content"> <h1 class="headertext">Welcome back.</h1> <p> <button id="add">Add Content</button> </p> </div> <div class="footer"> Footer </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM