0

我正在尝试创建一个底部固定页脚的模板。我已经成功地尝试了不同的方法,但现在我发现无法将二级内容 div 扩展到 100% 高度。我尝试过 Ryan Fait、David Walsh 并搜索其他方法。我知道这是一个常见问题,但在 stackoverflow 中我还没有看到解决方案。

你可以在这里看到一个例子:http: //jsfiddle.net/charlyta/hyfUe/

    * {
        margin: 0;
        padding: 0;
        }

    body, html {
        height: 100%;
        }

    #container {
        background: #f00; /* styling only */
        width: 100%;
        margin: 0 auto;
        position: relative;
        height: auto !important;
        min-height: 100%;
        height: 100%;



        }

    #content {
        padding-bottom: 100px;
        width: 980px;
        background-color: #FFF;
        margin: 0 auto;
        min-height: 100%;
        margin-top: -20px;
        -webkit-box-shadow: 0px 1px 3px rgba(27, 57, 50, 0.44);
        -moz-box-shadow:    0px 1px 3px rgba(27, 57, 50, 0.44);
        box-shadow:         0px 1px 3px rgba(27, 57, 50, 0.44);
        -moz-border-radius: 5px;
        border-radius: 5px;
        position: relative;
         max-height:100%;
        height:auto !important;
         height: 100%;
        overflow: hidden;

        }

    #footer {
        position: absolute;
        left: 0;
        bottom: 0;
        height: 100px;
        width: 100%;
        background: #0f0;
        }

        #header {

        height: 100px;
        width: 100%;
        background: #0f0;
        }

</style>

<div id="container">
<div id="header">
header
</div>
    <div id="content">
        Your content goes here
    </div>
    <div id="footer">
        Footer here
    </div>        
</div>
 </body>
4

3 回答 3

1

css的一些变化

* {
    margin: 0;
    padding: 0;
    }

#container {
    background: #f00; /* styling only */
    display:block;
    margin: 0 auto;
    min-height: 100%;
    }

#content {
    padding-bottom: 100px;
    width: 980px;
    background-color: #FFF;
    margin: 0 auto;
    min-height: 100%;
    margin-top: -20px;
    -webkit-box-shadow: 0px 1px 3px rgba(27, 57, 50, 0.44);
    -moz-box-shadow:    0px 1px 3px rgba(27, 57, 50, 0.44);
    box-shadow:         0px 1px 3px rgba(27, 57, 50, 0.44);
    -moz-border-radius: 5px;
    border-radius: 5px;
    position: relative;
     max-height:100%;
    height:auto !important;
     height: 100%;
    overflow: hidden;
    }

#footer {
    display:block;
    position: fixed; /* here is where the magic happens =D */
    left: 0;
    bottom: 0;
    height: 100px;
    width: 100%;
    background: #0f0;
    }

    #header {

    height: 100px;
    width: 100%;
    background: #0f0;
    }

如果要将页脚固定在底部,则页脚不能位于任何容器内,因此您的 html 应如下所示:

<div id="container">
    <div id="header">
    header
    </div>

    <div id="content">
        Your content goes here
    </div>
</div>

<div id="footer">
    Footer here
</div>

我想这就是你想要的。

于 2012-11-07T13:29:34.220 回答
1

编辑:

重试:

http://jsfiddle.net/hyfUe/12/

    #container {
        background: #f00; /* styling only */
        width: 100%;
        margin: 0 auto;
        /*position: relative;*/
        /*height: auto !important;*/
        /*min-height: 100%;*/
        height: calc(100% - 120px); /* MAGIC */
    }

    #content {
        border: 3px dashed silver; /* DEBUG */
        padding-bottom: 100px;
        width: 980px;
        background-color: #FFF;
        /*margin: 0 auto; /* REMOVED */
        /*min-height: 100%; /* REMOVED */
        margin-top: -20px;
        margin-bottom: 100px;
        -webkit-box-shadow: 0px 1px 3px rgba(27, 57, 50, 0.44);
        -moz-box-shadow:    0px 1px 3px rgba(27, 57, 50, 0.44);
        box-shadow:         0px 1px 3px rgba(27, 57, 50, 0.44);
        -moz-border-radius: 5px;
        border-radius: 5px;
        display: block;
        height: calc(100% - 170px) !important; /* MAGIC */
        }

    #footer {
        opacity: 0.5; /* DEBUG */
        position: absolute;
        left: 0;
        bottom: 0;
        height: 100px;
        width: 100%;
        background: #0f0;
        }
于 2012-11-07T13:11:21.210 回答
0

.container-for-fix-footer {
  display: flex;
  flex-direction: column;
  height: 100vh;
}
footer {
  height: 50px;
  flex-shrink: 0; /* this is what does the trick */
  background-color: black;
}
main {
  flex: 1 0 auto;
}
<div class="container-for-fix-footer">
  header
  <main class="content">
  content
  </main>
  <footer>FOOTER</footer>
</div>

于 2020-06-17T12:35:47.050 回答