简体   繁体   English

当您有不同的高度页面时,如何使页脚位于所有屏幕尺寸的底部?

[英]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.

相关问题 如何使身体高至绝对定位的div高呢? 我需要在底部有页脚 - How to make body as tall as huge absolutely positioned div in it? I need to have footer at the bottom 如何在不同的屏幕尺寸上调整页脚 - how to adjust the footer on different screen sizes 如何在页面中使所有大小不同的图像具有相同的高度和宽度? - How to make all the images of different sizes the same height and width in a page? 调整 window 的大小时,如何避免屏幕底部出现空格? 如何使所有尺寸的 window 的空间相同? - how can i avoid spaces that come at the bottom of screen when resizing window? How to make the space same for all sizes of window? 如何将iFrame高度拉伸到所有屏幕尺寸 - How to stretch iFrame height to all screen sizes 我该怎么做<footer>内容短时贴在屏幕底部,内容长时贴在屏幕底部?</footer> - How can I make <footer> stick to the bottom of the screen when content is short, and to the bottom of content when content is long? 对于不同的屏幕,页脚在页面底部的位置不正确 - Footer is not positioned properly on the bottom of the page for different screens 如何在底部为页脚的所有屏幕上制作所有相同高度的卡片? - how do i make cards all same height on all screens with footer at the bottom? 当您调整浏览器窗口的高度时,页脚不在页面的最底部 - footer not at the very bottom of page when you resize the height of browser window 在底部制作高度可变的页脚 - Make footer, with variable height, at the bottom
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM