代码之家  ›  专栏  ›  技术社区  ›  Basj

如果页面高度较短[重复],请确保页脚位于页面底部

  •  0
  • Basj  · 技术社区  · 5 年前

    我想要一个 footer :

    • 在页面底部,即使 main-container 高度很短,例如只有300像素高。在这种情况下,应该增加一个较大的垂直边距。
      可能是 height(viewport) - height(main-container) - height(header)

    • 在正常的身体流动中 <div id="main-container"> position: fixed position: absolute .

    • 如果 主容器 主容器 (与第1点相反)

    怎么做?

    #header { background-color: yellow; }
    #main-container { background-color: red; }
    #footer { background-color: green; }
    <div id="header">Header</div>
    <div id="main-container">
    Hello<br>
    World
    </div>
    <div id="footer">
    Bye-bye. <-- this should be on bottom even if main-container has only 2 lines
    </div>
    2 回复  |  直到 5 年前
        1
  •  1
  •   G-Cyrillus    5 年前

    实际上,很可能是 Fill remaining vertical space with CSS using display:flex

    你可以看看flex和flex增长。

    body {
    display:flex;
    flex-flow:column;
    min-height:100vh;
    margin:0;
    }
    #main-container {
    flex:1;
    }
    #footer {}
    
    
    /* = */
    #header { background-color: yellow; }
    #main-container { background-color: red; }
    #footer { background-color: green; }
    <div id="header">Header</div>
    <div id="main-container" contentEditable>
    Hello<br>
    World
    </div>
    <div id="footer">
    Bye-bye.
    </div>

    https://css-tricks.com/snippets/css/a-guide-to-flexbox/

        2
  •  1
  •   Basj    5 年前

    干得好

    #footer {  position: absolute; bottom: 0; width: 100%; height: 2rem; }
    #main-container { padding-bottom: 2rem; }
    <div id="main-container" contentEditable>
    Hello<br>
    World
    </div>
    <div id="footer">
    Bye-bye.
    </div>