【问题标题】:Wrapper height overflowing into footer包装高度溢出到页脚
【发布时间】:2016-06-21 22:23:39
【问题描述】:

我想要一个粘性页脚,但我也希望我的主 div 在高度的页脚之前结束,但是如果您的边距为负并且我不知道为什么,包装器不会像往常一样向上移动。即使有最小高度,我也应该能够将其向上移动。当我增加负边距时,什么也没有发生。基本上我只需要我的包装器是 100% 的高度减去页脚的高度。感兴趣的 div 是具有蓝色背景的 div。现在我的侧边栏非常好(或者至少应该如此),因为它在黑色页脚之前结束。

html {
    height: 100%;
}

body{
    height: 100%;
    margin:0;
    font-family: courier;
    font-size: 22px;
    color: white;
    background-color: #99ff33;
}


#wrapper{
    position: relative;
    margin-left: auto;
    margin-right: auto;
    width: 85%;
    background-color: blue;
    min-height: 100%;
    height: calc(100vh-130px);
    margin-bottom: -130px;
}

#wrapper:after{
    content: "";
    display:block;
}

#footer, #wrapper:after{
    height: 130px;
}


.wrap {
    margin: 0 auto;
    width: 100%;
    display: flex;
    align-items: center;
    flex-wrap: nowrap;
}

.sub {
    padding: 12px;
    width: 32%;
    height: 100px;
    color: white;
    border-right: solid white 1px;
}

.sub:last-child{
    border: 0px;
}

#sidebar{
    float:left;
    background-color: yellow;
    height:calc(100vh - 130px);
    width: 7.5%;
    border: 1px solid blue;
}

#footer {
    display: flex;
    height: 130px;
    width: 100%;
    background-color: black;
    clear: both;
}
<div id="sidebar"></div>
<div id="wrapper"></div>
<div id="footer">
   <div class="wrap">
      <div class="sub"></div>
      <div class="sub"></div>
      <div class="sub"></div>
   </div>
</div>

【问题讨论】:

    标签: html css wrapper


    【解决方案1】:

    你搞错方向了,傻鹅!

    #wrapper{
        position: relative;
        margin-left: auto;
        margin-right: auto;
        width: 85%;
        background-color: blue;
        min-height: 100%;
        height: calc(100vh-130px);
        margin-top: -130px;
    
    }
    

    margin-top vs margin-bottom

    【讨论】:

    • 哦,谢谢。好的,我将侧边栏设置为 100% 高度,而不是考虑顶部边距,但是如果您看一下这张图片,您会注意到在包装器和页脚之间有一条主体背景颜色。我应该添加多少来掩盖它?为什么会进来? snag.gy/C8RrtN.jpg
    • 啊,我找到了。这是div之后的伪。好吧,我会被诅咒的,你可以在没有这些的情况下做底部页脚。
    【解决方案2】:

    删除 min-height: 100%; 这将使其始终保持其父级的 100% 高度。在calc 函数中为您的数学添加一个空格并添加一个border 以使其与您的工具栏大小相同。我还删除了relative 位置。

    这是 #wrapper 的新 CSS:

    #wrapper{
        margin-left: auto;
        margin-right: auto;
        border: 1px solid blue;
        width: 85%;
        background-color: blue;
        height: calc(100vh - 130px);
    }
    

    还有jsfiddle

    更新:

    这是 MDN 中关于用空格包围 calc 的操作数的引述:

    注意:+ 和 - 运算符必须始终被空格包围。例如 calc(50% -8px) 的操作数将被解析为一个百分比后跟一个负长度,一个无效的表达式,而 calc(50% - 8px) 的操作数是一个百分比后跟一个减号和一个长度.更进一步, calc(8px + -50%) 被视为一个长度,后跟一个加号和一个负百分比。 * 和 / 运算符不需要空格,但为了保持一致性,允许并建议添加空格。

    您可以了解有关 calc here 的更多信息。

    另外,calc 不受高度支持,可能会发生变化。所以我不推荐使用它,或者至少有一个后备。

    【讨论】:

    • 这真的很奇怪。当我这样做时,我的整个侧边栏都消失了,你真正看到的只是绿色背景。侧边栏周围的蓝色轮廓是什么?这是我的新小提琴,但应该和你的一样:jsfiddle.net/pr96g63f
    • 侧边栏周围的蓝色轮廓来自您的原始代码。 #sidebar 的样式为 border: 1px solid blue;。这会在侧边栏周围创建蓝色“轮廓”。
    • 另外,你需要在height:calc(100vh - 130px); 时添加一个空格,所以如果你在设置#sidebar 样式时在小提琴中更改它,它应该看起来和我的一样。
    猜你喜欢
    • 1970-01-01
    • 2014-03-15
    • 1970-01-01
    • 2021-08-12
    • 2014-10-03
    • 1970-01-01
    • 2013-03-17
    • 2013-06-17
    • 1970-01-01
    相关资源
    最近更新 更多