[英]How to make the footer positioned at the bottom of all screen sizes when you have different height pages?
I'm trying to make a sticky footer but for some reason(s) it is playing up. 我正在尝试使页脚变粘,但是由于某些原因,它正在播放。 I have few pages with different content height therefor when I set the footer on one of them it ruin the others. 当我在其中一个页脚上放置页脚时,会导致其他页面的内容高度不同。 How can I make a global style for all pages in order to have a sticky footer to the bottom of the pages and also remove any scrolling if there is not need for it as some of my pages require a scroll to reach the footer while the content is about have height. 如何为所有页面制作全局样式,以便在页面底部添加页脚,并在不需要滚动条的情况下将其删除,因为某些页面需要滚动才能到达页脚即将有身高。
As I test my site using a website called screen-fly to enable testing on all screen sizes I need a solution that can guarantee a responsive design on any screen size please. 当我使用一个名为screen-fly的网站来测试我的站点以启用所有屏幕尺寸的测试时,我需要一个可以保证在任何屏幕尺寸上都能做出响应设计的解决方案。
My Code is available on plunker , however I researched a lot and tried some solutions for styling. 我的代码可以在plunker上找到,但是我进行了大量研究,并尝试了一些样式解决方案。 Here are two of them 这是两个
Solution 1 :- 解决方案1:-
/* **************************************************/
/* One solution */
html, body {
height:100%;
min-height:100%;
}
/* navigation style*/
.navbar-brand {
margin: 0 auto;
color:white;
}
.navbar-inverse {
background: rgb(14, 78, 114);
}
.navbar-inverse .navbar-nav>li>a {
color: #8EE8CD;
}
/* Content style*/
.container.content {
margin-top: 100px;
}
/* footer style*/
.wrapper {
position:relative;
min-height:100%;
}
footer {
text-align: center;
position:absolute;
bottom:0px;
width:100%;
color: white;
background-color: rgb(14, 78, 114);
}
Solutoin 2:- 溶剂蛋白2:-
/* Another solution */
html, body {height: 100%;}
#wrap {min-height: 100%;}
#main {overflow:auto;
padding-bottom: 150px;} /* must be same height as the footer */
#footer {position: relative;
margin-top: -150px; /* negative value of footer height */
height: 150px;
clear:both;}
/*Opera Fix*/
body:before {
content:"";
height:100%;
float:left;
width:0;
margin-top:-32767px;
}
Here are some likns of the code. 这是一些代码形式。
plunker Editor: Plunker Editor plunker编辑器: Plunker编辑器
plunker full screen: Plunker Full Screen 全屏柱塞: 全屏柱塞
My site on screenfly: Screenfly Site 我在screenfly上的网站 : Screenfly网站
Use CSS calc()
function. 使用CSS calc()
函数。 According to your plunkr example give min-height to .container.content
instead to giving min-height
to html, body, wrapper
. 根据您的plunkr示例,将min-height赋予.container.content
而不是给min-height
赋予html, body, wrapper
。
Look at the updated Plunkr . 查看更新的Plunkr 。
Logic used to give min-height
: 用于给出min-height
逻辑:
.container.content {
min-height: calc(100vh - 140px);
}
In the above code: 在上面的代码中:
vh
: Viewport Height ( 100vh
gives total screen height) vh
:视口高度( 100vh
表示总屏幕高度) 140px
: 100px
for header + 20px
for footer + 20px
for top margin of footer. 140px
:页眉为100px
+页脚为20px
+页脚的上边距为20px
。 So we are just subtracting the rest of the div
s from total viewport height. 因此,我们只是从总视口高度中减去div
的其余部分。
Try overflow: hidden; 尝试溢出:隐藏; for body. 为身体。
This is a common problem many people try to solve. 这是许多人试图解决的普遍问题。 For the longest time it required either a compromise or some hacky CSS. 在最长的时间内,它要么需要妥协,要么需要一些hacky CSS。 Thankfully with the dawn of Flexbox it is actually rather easy. 幸运的是,随着Flexbox的兴起,它实际上相当容易。 Check the link below. 检查下面的链接。
https://philipwalton.github.io/solved-by-flexbox/demos/holy-grail/ https://philipwalton.github.io/solved-by-flexbox/demos/holy-grail/
This page goes through solving problems like yours with Flexbox and will walk you through it. 该页面介绍了如何使用Flexbox解决类似您的问题,并将逐步引导您。
Change footer style 更改页脚样式
position: absolute;
to 至
position: fixed;
Hope that is what you wanted to achieve :) 希望这就是您想要实现的目标:)
For those cases I would use Flexbox . 对于那些情况,我将使用Flexbox 。 This gives you the ability to completely remove your wrapper div .wrapper
这使您能够完全删除包装div .wrapper
To achieve what you want simply add the following code to your CSS 要实现您想要的功能,只需将以下代码添加到CSS中
body {
display: flex;
flex-direction: column;
}
and change your CSS of your footer div, in your case footer
to this 并更改您的页脚股利你的CSS,你的情况footer
这个
footer {
margin-top: auto;
text-align: center;
color: white;
background-color: rgb(14, 78, 114);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.