【问题标题】:Div to fill the rest of pageDiv 填充页面的其余部分
【发布时间】:2015-06-09 07:19:29
【问题描述】:

我正在尝试在页脚之前填充我的其余内容。我在这里找到了许多页面,其中的解决方案似乎合乎逻辑,但似乎不适用于我的页面。我无法将页脚的位置状态更改为固定或绝对。 我目前有一个 id 为“bottomfix”的 div。我尝试了固定和归零的解决方案,但这也不起作用。

我邀请您查看我的biopage,我正在处理的 css 是标题为 style.css 的最后一张表。

【问题讨论】:

  • 这不能仅使用 CSS 完成。你需要 JS。如果您不关心 div 的大小,只想让页脚粘在底部,请使用 Google“CSS 粘性页脚”或类似内容。
  • 重复在此处查看解决方案stackoverflow.com/questions/18915550/…
  • @WouterFlorijn 你可以使用 CSS3 属性 calc 我相信
  • @zgood 仅当您知道页面上所有其他内容的确切高度时才有效。即使你这样做了,在 CSS 中依赖精确的大小也是不好的做法。
  • @WouterFlorijn 您只需要知道<header> 和<footer> 的高度就可以在这种情况下工作。但我同意这仅在页眉和页脚具有静态高度时才有效。但这就是 calc 函数的用途,所以如果可以的话,请使用它!

标签: html css


【解决方案1】:

我认为适用于您的布局的一种方法是使用 flexbox。我实际上是为我自己的一个项目(example) 这样做的。这是最好的解决方案,因为您的页脚和其他内容可以具有动态高度。

一般 HTML 结构:

<body>
  <main>
    Header and page content go here...
  </main>
  <footer>
    Footer...
  </footer>
</body>

重要的是要包含许多不同的前缀和后备,以使其在所有浏览器中都能正常工作(我在 Chrome、FF、IE 和 Safari 的多个版本中对其进行了测试)。

CSS:

body {
  display: box;
  display: -webkit-box;
  display: -moz-flex;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  min-height: 100vh;
  -webkit-flex-direction: column;
  -ms-flex-direction: column;
  flex-direction: column;
  -webkit-box-orient: vertical;
  -moz-box-orient: vertical;
  box-orient: vertical;
}

main {
  -webkit-box-flex: 1 1 auto;
  -moz-box-flex: 1 1 auto;
  -webkit-flex: 1 1 auto;
  -ms-flex: 1 1 auto;
  flex: 1 1 auto;
}

footer {
  margin-top: 32px; // Whatever space you want between content and footer.
  -webkit-box-flex: 0 1 auto;
  -moz-box-flex: 0 1 auto;
  -webkit-flex: 0 1 auto;
  -ms-flex: 0 1 auto;
  flex: 0 1 auto;
}

如果您想控制.bottomfix div 的大小,可以在main 元素中添加更多弹性框。

【讨论】:

  • 我不得不将这些东西添加到#page 但这个终于解决了!
【解决方案2】:

添加到您的#footer 类position: fixed; 然后添加位置bottom: 0px;,它将在底部

#footer {
    border-width: 5px;
    bottom: 0;
    padding: 6px 0 7px;
    position: fixed;
    width: 100%;
}

如果您希望仅当您的页面没有足够的内容来填充页面时才将页脚固定到页面底部,请将此脚本添加到 &lt;body&gt; 结束标记之前

<script type="text/javascript">
if(jQuery('header:first').height()+
   jQuery('main:first').height()+ jQuery('footer:first').height() >  jQuery(window).height()){ 
   $('#footer').css({position: 'fixed', bottom: '0px'});
}
</script>

这个脚本将在加载时检查您的页面是否需要固定页脚:)

或者你可以添加它

$(document).ready(function(){

  //code

});

【讨论】:

  • 这不是我整体设计的选项。如果你访问我的主页,你会明白为什么。
  • 所以如果有可以覆盖整个页面的内容你不需要它在底部?只有在有空白空间的情况下?
  • @newby JP:我不明白为什么这个或绝对底部:0;不行吗?虽然这并不漂亮,因为您有这么多嵌套元素,请在您的 html 和 body 上设置 100% 的最小高度。然后,您可以将绝对值与底部:0 一起使用。
  • 是的,它确实“工作”,但设计的重点是着陆页,是一个干净的联合国障碍图像。
  • @Senad 最后的添加看起来很棒!我要在哪个结束标记之前添加该脚本?
【解决方案3】:
#footer {
  border-top: 1px solid #000;
  background: #222;
  color: #7B7B7B;
  font-size: 13px;
  position: absolute;
  z-index: 100;
  clear: both;
  padding: 10px 0;
  bottom: 0;
}

这对我有用,我将位置设置为绝对并设置 bottom:0; 以强制 div 到浏览器底部

【讨论】:

    【解决方案4】:

    您可以尝试使用 CSS3 calc 函数。像这样:

    main{
        min-height: 100%; /*fallback if browser doesn't support calc*/
        min-height: ~"calc(100% - 330px)"; /*330px comes from 270px <header> plus 60px <footer>*/
    }
    

    您只需将 &lt;header&gt; 和 &lt;footer&gt; 高度值计算替换为您的。

    还要确保你有这样的东西:

    html, body{
        height:100%;
    }
    

    【讨论】:

    • 你添加了html, body { height: 100%; }
    • 在您的情况下,您可能还需要添加#page{ height: 100%; }
    • 好的,#page 似乎可以解决问题,但有点太好了。现在我什至无法到达我的页脚。
    【解决方案5】:

    试试这个。 66px 是页脚 + 页眉高度

    html, body, #page, #bottomfix, main > div {
        height: 100%;
    }
    
    main {
        height: calc(100% - 66px);
    } 
    

    【讨论】:

    • @zgood 我试过你的例子,但对我来说 min-height 不起作用
    • 这很奇怪,我在几个现场项目中使用该代码,它似乎正在工作。哦,好吧,重点是使用calc,它可以防止您需要维护任何额外的javascript。很好的解决方案;)
    • 在简历页面上效果很好,但它会在我的主页上删除我的内容
    • 如果内容比较多,你想让页脚往下走吗?您不能总是将所有内容都放在一个页面中,并且没有滚动条的“粘性”页脚)
    • 我不完全理解你的问题
    猜你喜欢
    • 2017-12-26
    • 1970-01-01
    • 2016-06-13
    • 2018-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多