【问题标题】:div with dynamic min-height based on browser window height具有基于浏览器窗口高度的动态最小高度的 div
【发布时间】:2011-11-23 12:54:43
【问题描述】:

我有三个div 元素:一个作为页眉,一个作为页脚,一个中心内容div
中心的div 需要随内容自动扩展,但我想要一个min-height,这样底部的div 总是至少到达窗口的底部,但在较长的页面上不会固定在那里.

例如:

<div id="a" style="height: 200px;">
  <p>This div should always remain at the top of the page content and should scroll with it.</p>
</div>
<div id="b">
  <p>This is the div in question. On longer pages, this div needs to behave normally (i.e. expand to fit the content and scroll with the entire page). On shorter pages, this div needs to expand beyond its content to a height such that div c will reach the bottom of the viewport, regardless of monitor resolution or window size.
</div>
<div id="c" style="height: 100px;">
  <p>This div needs to remain at the bottom of the page's content, and scroll with it on longer pages, but on shorter pages, needs to reach the bottom of the browser window, regardless of monitor resolution or window size.</p>
</div>

【问题讨论】:

    标签: viewport css


    【解决方案1】:

    只要找我的solution on jsfiddle,它是基于csslayout

    html,
    body {
      margin: 0;
      padding: 0;
      height: 100%; /* needed for container min-height */
    }
    div#container {
      position: relative; /* needed for footer positioning*/
      height: auto !important; /* real browsers */
      min-height: 100%; /* real browsers */
    }
    div#header {
      padding: 1em;
      background: #efe;
    }
    div#content {
      /* padding:1em 1em 5em; *//* bottom padding for footer */
    }
    div#footer {
      position: absolute;
      width: 100%;
      bottom: 0; /* stick to bottom */
      background: #ddd;
    }
    <div id="container">
    
      <div id="header">header</div>
    
      <div id="content">
        content<br/>content<br/>content<br/>content<br/>content<br/>content<br/>content<br/>content<br/>content<br/>content<br/>content<br/>content<br/>content<br/>content<br/>content<br/>content<br/>content<br/>content<br/>
      </div>
    
      <div id="footer">
        footer
      </div>
    </div>

    【讨论】:

    • 您注释掉了页脚的填充,我不得不将其重新添加以适应页脚区域。我最终使用了padding-bottom: 50px,因为我的页脚高 50 像素。
    • 这两个:height: auto !important; /* real browsers */; min-height: 100%; 就像一个魅力!谢谢。
    【解决方案2】:

    我发现这是由ryanfait.com 提供的。它实际上非常简单。

    为了在内容短于 window-height 时将页脚浮动到页面底部,或者当内容长于 window-height 时将页脚浮动到页面底部,请使用以下代码:

    基本 HTML 结构:

    <div id="content">
      Place your content here.
      <div id="push"></div>
    </div>
    <div id="footer">
      Place your footer information here.
    </footer>
    

    注意:除非绝对定位,否则不应在 '#content' 和 '#footer' div 之外放置任何内容。
    注意:不应在 '# 内放置任何内容push' div,因为它将被隐藏。

    还有 CSS:

    * {
      margin: 0;
    }
    html, body {
      height: 100%;
    }
    #content {
      min-height: 100%;
      height: auto !important; /*min-height hack*/
      height: 100%;            /*min-height hack*/
      margin-bottom: -4em;     /*Negates #push on longer pages*/
    }
    #footer, #push {
      height: 4em;
    }
    

    要使页眉或页脚跨越页面的宽度,您必须绝对定位页眉。
    注意:如果添加页宽标题,我发现有必要在#content 中添加一个额外的包装 div。外部 div 控制水平间距,而内部 div 控制垂直间距。我必须这样做,因为我发现 'min-height:' 仅适用于元素的主体,并为高度添加填充。

    *编辑:缺少分号

    【讨论】:

    • 为什么不同时包含一个演示链接并将其称为粘性页脚? ryanfait.com/sticky-footer
    • 对于html, body,最好使用min-height 而不是height,否则如果内容溢出窗口大小,它不会将背景拉伸到站点的全高
    • 这将显示页脚,即使页面内容长于屏幕。有时,这不是我们想要的行为。
    • 链接已失效,作为拥有该博客的网页设计师,他的灵魂安息。
    【解决方案3】:

    如果#top#bottom有固定的高度,你可以使用:

    #top {
        position: absolute;
        top: 0;
        height: 200px;
    }
    #bottom {
        position: absolute;
        bottom: 0;
        height: 100px;
    }
    #central {
        margin-top: 200px;
        margin-bot: 100px;
    }
    

    更新

    如果你想让#central 向下伸展,你可以:

    • 用父级背景伪造它;
    • 使用 CSS3(未得到广泛支持,很有可能)calc()
    • 或者可以使用javascript动态添加min-height

    calc():

    #central {
        min-height: calc(100% - 300px);
    }
    

    使用 jQuery,它可能是这样的:

    $(document).ready(function() {
      var desiredHeight = $("body").height() - $("top").height() - $("bot").height();
      $("#central").css("min-height", desiredHeight );
    });
    

    【讨论】:

    • 但这不会超出 #central 的内容......会吗?
    • 没有。它可以满足您的需求,但不会延伸#central。我会更新答案。
    【解决方案4】:

    根据浏览器窗口获取动态高度。使用 vh 而不是 %

    例如:将以下 height: 100vh; 传递给特定的 div

    【讨论】:

    • 这是不对的。显然,op 希望根据窗口的高度和某些其他约束来调整其内容的大小。
    【解决方案5】:

    正如在其他地方提到的,CSS 函数 calc() 在这里可以很好地工作。现在主要支持它。你可以这样使用:

    .container
    {
        min-height: 70%;
        min-height: -webkit-calc(100% - 300px);
        min-height: -moz-calc(100% - 300px);
        min-height: calc(100% - 300px);
    }
    

    【讨论】:

    • calc 方法真的很有用。感谢分享。
    【解决方案6】:

    无需 hack 或 js。只需将以下规则应用于您的根元素:

    min-height: 100%;
    height: auto;
    

    它会自动选择两者中较大的一个作为它的高度,也就是说如果内容比浏览器长,则为内容的高度,否则为浏览器的高度。这是标准的 CSS。

    【讨论】:

    • 当它的 div 位于另一个具有固定高度的 div 中时,它在这里不起作用。那是因为它 100% 是相对于父 div 而言的。
    【解决方案7】:

    您可能必须编写一些 JavaScript,因为无法估计页面所有用户的高度。

    【讨论】:

    • javascript 或 jquery 都可以。有些页面并没有那么长,并且在我较大的显示器上最大化,如果页脚实际上除了页面之外还占据了窗口(至少在较小的页面上),它看起来会更好。
    【解决方案8】:

    这很难做到。

    有一个min-height: css 样式,但它不适用于所有浏览器。您可以使用它,但最大的问题是您需要将其设置为 90% 或类似的数字(百分比),但顶部和底部 div 使用固定像素大小,您将无法协调他们。

     var minHeight = $(window).height() -
                     $('#a').outerHeight(true) -
                     $('#c').outerHeight(true));
    
    if($('#b').height() < minHeight) $('#b').height(minHeight);
    

    我知道ac 有固定的高度,但我宁愿测量它们以防它们以后发生变化。

    另外,我正在测量b 的高度(毕竟我不想做得更小),但是如果那里有没有加载的图像,高度可能会改变,所以要小心像这样。

    这样做可能更安全:

    $('#b').prepend('<div style="float: left; width: 1px; height: ' + minHeight + 'px;">&nbsp;</div>');
    

    这只是将一个具有正确高度的元素添加到该 div 中 - 即使对于没有它的浏览器,它也可以有效地充当最小高度。 (您可能希望将元素添加到您的标记中,然后只需通过 javascript 控制它的高度,而不是同样添加它,这样您可以在设计布局时考虑到它。)

    【讨论】:

    • 我熟悉 css 的 min-height: 属性以及 ie 不喜欢玩得很好(这就是我使用 chrome 的原因),但我知道您可以使用固定长度和 min-height:
    • 当然可以使用固定长度。但它不会根据屏幕大小动态缩放。
    • 好的,这一切都说得通。现在请原谅我,我还在学习理解javascript,更不用说写它了......我会把那个脚本放在哪里?
    • 天啊。我在评论中回答有点多。一方面使用 jQuery 库,而不仅仅是 javascript。我认为你应该先做一些谷歌搜索。有大量的 jQuery 和 javascript 教程网站。 (但是,基本上你希望 &lt;SCRIPT type="text/javascript"&gt;CODE GOES HERE&lt;/SCRIPT&gt; 放在 html 的标题部分。但是你需要 jQuery 库的第二个脚本标签才能工作。)
    • 好的,谢谢。 Javascript 和 jQuery 是我要研究的下一个列表……我想这暂时只能退居二线。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-07
    • 2012-07-08
    • 1970-01-01
    • 1970-01-01
    • 2010-12-07
    相关资源
    最近更新 更多