【问题标题】:100% Min Height CSS layout100% 最小高度 CSS 布局
【发布时间】:2010-09-06 17:12:02
【问题描述】:

使元素的最小高度为 100% 的最佳方法是什么? 各种浏览器?

特别是如果您有一个带有固定heightheaderfooter 的布局,

如何使中间内容部分填充100% 之间的空间,而footer 固定在底部?

【问题讨论】:

  • 你可以考虑min-height: 100vh;。这会将高度设置为等于或大于屏幕大小vh: vertical height。更多信息:w3schools.com/cssref/css_units.asp.
  • 澄清一下,vh 代表 viewport height,因此您也可以使用 vw 代表 viewport widthvmin 代表最小的维度,viewport minimum
  • 此解决方案将在 chrome for Android 上产生不需要的结果(必须检查其他移动浏览器,如 Safari),因为 100vh 与 100% 不同。实际上,height 100% 对应的是屏幕高度减去屏幕顶部的地址栏,而 100vh 对应的是没有地址栏的屏幕高度。所以使用 100vh 不适用于 Android 的 chrome。您的页脚将在首屏下方的高度与浏览器地址栏的高度相匹配。
  • 您可以使用 Flexbox 实现这一点。 See example.
  • 现在,'100vh' 就像魅力一样发挥作用

标签: html css xhtml


【解决方案1】:

正如the height property in MDN docs 指定的那样,它不会被继承。因此,您需要将其设置为 100%。众所周知,任何网页都以 html 开头,然后是 body 作为子页面,您需要在两者中设置 heigth: 100%

html,
   body {
     height: 100%;
   }

<html>
  <body>

  </body>
</html>

并且任何标有身高 100% 的孩子都将是父母身高 100 的一部分。在上面的示例样式中,我们将 html 标签设置为 100%,它将是可用屏幕高度的全高。那么 body 是 100%,所以它也是全高,因为它的父级是 html。

【讨论】:

    【解决方案2】:

    可能是最短的解决方案(仅适用于现代浏览器)

    这一小块 CSS 使 "the middle content part fill 100% of the space in between with the footer fixed to the bottom":

    html, body { height: 100%; }
    your_container { min-height: calc(100% - height_of_your_footer); }
    

    唯一的要求是您需要有一个固定高度的页脚。

    例如对于这个布局:

        <html><head></head><body>
          <main> your main content </main>
          </footer> your footer content </footer>
        </body></html>
    

    你需要这个 CSS:

        html, body { height: 100%; }
        main { min-height: calc(100% - 2em); }
        footer { height: 2em; }
    

    【讨论】:

      【解决方案3】:

      试试这个:

      body{ height: 100%; }
      #content { 
          min-height: 500px;
          height: 100%;
      }
      #footer {
          height: 100px;
          clear: both !important;
      }
      

      内容 div 下方的 div 元素必须有 clear:both

      【讨论】:

        【解决方案4】:

        我正在使用以下一个:CSS Layout - 100 % height

        最小高度

        此页面的#container 元素的最小高度为 100%。那 方式,如果内容需要比视口提供的更高的高度, #content 的高度也迫使#container 变得更长。 #content 中可能的列然后可以用背景可视化 #container 上的图像; div 不是表格单元格,您不需要(或 想要)物理元素来创造这样的视觉效果。如果你是 还没有被说服;考虑摇摆不定的线条和渐变,而不是 直线和简单的配色方案。

        相对定位

        因为#container 有一个相对位置,所以#footer 会一直保持 在它的底部;因为上面提到的最小高度并不能阻止 #container 来自缩放,即使(或者更确切地说,尤其是当)#content 强制 #container 变得更长时,这也会起作用。

        底部填充

        由于它不再处于正常流程中,#content 的 padding-bottom 现在为绝对#footer 提供空间。这个填充是 默认情况下包含在滚动高度中,因此页脚将 切勿与上述内容重叠。

        稍微缩放文本大小或调整浏览器窗口的大小以进行测试 布局。

        html,body {
            margin:0;
            padding:0;
            height:100%; /* needed for container min-height */
            background:gray;
        
            font-family:arial,sans-serif;
            font-size:small;
            color:#666;
        }
        
        h1 { 
            font:1.5em georgia,serif; 
            margin:0.5em 0;
        }
        
        h2 {
            font:1.25em georgia,serif; 
            margin:0 0 0.5em;
        }
            h1, h2, a {
                color:orange;
            }
        
        p { 
            line-height:1.5; 
            margin:0 0 1em;
        }
        
        div#container {
            position:relative; /* needed for footer positioning*/
            margin:0 auto; /* center, not in IE5 */
            width:750px;
            background:#f0f0f0;
        
            height:auto !important; /* real browsers */
            height:100%; /* IE6: treaded as min-height*/
        
            min-height:100%; /* real browsers */
        }
        
        div#header {
            padding:1em;
            background:#ddd url("../csslayout.gif") 98% 10px no-repeat;
            border-bottom:6px double gray;
        }
            div#header p {
                font-style:italic;
                font-size:1.1em;
                margin:0;
            }
        
        div#content {
            padding:1em 1em 5em; /* bottom padding for footer */
        }
            div#content p {
                text-align:justify;
                padding:0 1em;
            }
        
        div#footer {
            position:absolute;
            width:100%;
            bottom:0; /* stick to bottom */
            background:#ddd;
            border-top:6px double gray;
        }
        div#footer p {
            padding:1em;
            margin:0;
        }
        

        对我来说很好。

        【讨论】:

        • 在那个例子中尝试在 IE7 中增加字体大小 (Ctrl + "+") 并且破坏了粘性页脚:(
        • 这个好像在 IE9 中不起作用...在其他 IE 中也起作用吗?
        【解决方案5】:

        将自定义高度设置为锁定到某处:

        body, html {
          height: 100%;
        }
        #outerbox {
          width: 100%;
          position: absolute; /* to place it somewhere on the screen */
          top: 130px;         /* free space at top */
          bottom: 0;          /* makes it lock to the bottom */
        }
        #innerbox {
          width: 100%;
          position: absolute;				
          min-height: 100% !important; /* browser fill */
          height: auto;                /*content fill */
        }
        <div id="outerbox">
          <div id="innerbox"></div>
        </div>

        【讨论】:

          【解决方案6】:

          这里是基于vhviewpoint height的另一种解决方案,详情请访问CSS units。它基于这个solution,它使用了flex。

          * {
              /* personal preference */
              margin: 0;
              padding: 0;
          }
          html {
              /* make sure we use up the whole viewport */
              width: 100%;
              min-height: 100vh;
              /* for debugging, a red background lets us see any seams */
              background-color: red;
          }
          body {
              /* make sure we use the full width but allow for more height */
              width: 100%;
              min-height: 100vh; /* this helps with the sticky footer */
          }
          main {
              /* for debugging, a blue background lets us see the content */
              background-color: skyblue;
          	min-height: calc(100vh - 2.5em); /* this leaves space for the sticky footer */
          }
          footer {
              /* for debugging, a gray background lets us see the footer */
              background-color: gray;
          	min-height:2.5em;
          }
          <main>
              <p>This is the content. Resize the viewport vertically to see how the footer behaves.</p>
              <p>This is the content.</p>
              <p>This is the content.</p>
              <p>This is the content.</p>
              <p>This is the content.</p>
              <p>This is the content.</p>
              <p>This is the content.</p>
              <p>This is the content.</p>
              <p>This is the content.</p>
              <p>This is the content.</p>
          </main>
          <footer>
              <p>This is the footer. Resize the viewport horizontally to see how the height behaves when text wraps.</p>
              <p>This is the footer.</p>
          </footer>

          单位是 vw , vh, vmax, vmin。基本上,每个单位等于视口大小的 1%。因此,随着视口的变化,浏览器会计算该值并进行相应的调整。

          您可以找到更多信息here:

          具体来说:

          1vw (viewport width) = 1% of viewport width
          1vh (viewport height) = 1% of viewport height
          1vmin (viewport minimum) = 1vw or 1vh, whatever is smallest
          1vmax (viewport minimum) = 1vw or 1vh, whatever is largest
          

          【讨论】:

            【解决方案7】:

            纯粹的CSS 解决方案 (#content { min-height: 100%; }) 可以在很多情况下工作,但并非在所有情况下都有效 - 尤其是 IE6 和 IE7。

            不幸的是,您需要借助 JavaScript 解决方案才能获得所需的行为。 这可以通过计算您的内容&lt;div&gt; 所需的高度并将其设置为函数中的 CSS 属性来完成:

            function resizeContent() {
              var contentDiv = document.getElementById('content');
              var headerDiv = document.getElementById('header');
              // This may need to be done differently on IE than FF, but you get the idea.
              var viewPortHeight = window.innerHeight - headerDiv.clientHeight;
              contentDiv.style.height = 
                Math.max(viewportHeight, contentDiv.clientHeight) + 'px';
            }
            

            然后您可以将此函数设置为onLoadonResize 事件的处理程序:

            <body onload="resizeContent()" onresize="resizeContent()">
              . . .
            </body>
            

            【讨论】:

              【解决方案8】:

              正如 Afshin Mehrabani 的回答中提到的,您应该将 body 和 html 的高度设置为 100%,但要在此处获取页脚,请计算包装器的高度:

              #pagewrapper{
              /* Firefox */
              height: -moz-calc(100% - 100px); /*assume i.e. your header above the wrapper is 80 and the footer bellow is 20*/
              /* WebKit */
              height: -webkit-calc(100% - 100px);
              /* Opera */
              height: -o-calc(100% - 100px);
              /* Standard */
              height: calc(100% - 100px);
              }
              

              【讨论】:

                【解决方案9】:

                为了让min-height 正确处理百分比,同时继承它的父节点min-height,诀窍是将父节点高度设置为1px,然后子节点的min-height 将正常工作。

                Demo page

                【讨论】:

                • BUT 如果演示中的 div 的内容比浏览器窗口高,向下滚动后背景将被剪切
                • @fider - 请提供一个显示您提到的问题的游乐场。
                • jsbin.com/nixuf/1/edit 向下滚动。我在下面的post 中找到了有用的信息
                【解决方案10】:

                首先,您应该在您的 content div 之后创建一个 divid='footer',然后简单地执行此操作。

                您的 HTML 应如下所示:

                <html>
                    <body>
                        <div id="content">
                            ...
                        </div>
                        <div id="footer"></div>
                    </body>
                </html>
                

                还有 CSS:

                ​html, body {
                    height: 100%;   
                }
                #content {
                    height: 100%;
                }
                #footer {
                    clear: both;        
                }
                

                【讨论】:

                  【解决方案11】:

                  我同意 Levik 的观点,因为如果您有侧边栏并希望它们填充空间以与页脚相遇,则父容器设置为 100%,您不能将它们设置为 100%,因为它们将是父容器高度的 100%同样,这意味着在使用 clear 功能时,页脚最终会被下推。

                  如果您的页眉高度为 50 像素,页脚高度为 50 像素,并且内容只是自动调整到剩余空间(例如 100 像素)并且页面容器是该值的 100%,那么可以这样想,其高度将为 200 像素.然后,当您将侧边栏高度设置为 100% 时,即使它应该紧贴在页眉和页脚之间,它也是 200 像素。相反,它最终是 50px + 200px + 50px 所以页面现在是 300px 因为侧边栏设置为与页面容器相同的高度。页面内容会有很大的空白。

                  我正在使用 Internet Explorer 9,这就是我在使用这种 100% 方法时所获得的效果。我还没有在其他浏览器中尝试过,我认为它可能适用于其他一些选项。但它不会是通用的。

                  【讨论】:

                    【解决方案12】:

                    只分享我用过的东西,效果很好

                    #content{
                            height: auto;
                            min-height:350px;
                    }
                    

                    【讨论】:

                      【解决方案13】:

                      你可以试试这个:http://www.monkey-business.biz/88/horizontal-zentriertes-100-hohe-css-layout/ 那是 100% 的高度和水平中心。

                      【讨论】:

                        【解决方案14】:

                        kleolb02 的答案看起来不错。另一种方式是sticky footermin-height hack 的组合

                        【讨论】:

                          猜你喜欢
                          • 2011-09-03
                          • 1970-01-01
                          • 2013-02-16
                          • 1970-01-01
                          • 1970-01-01
                          • 1970-01-01
                          • 2010-11-03
                          • 1970-01-01
                          • 1970-01-01
                          相关资源
                          最近更新 更多