html - CSS 问题 : fixed footer with width of 100% is too wide

标签 html css responsive-design

我正在尝试使用 % 而不是 px,它的行为有点奇怪。 为什么我的页脚继续向右(在元素“pagePlaceHolderInner”之外) 如果我将两个元素(“pagePlaceHolderInner”和“页脚”)都设置为某个 px 数字,它会起作用,但我想在这里使用 %。

代码:

        <html>
        <head>
        <style type="text/css">
        body #pagePlaceHolderOuter {
            background: #FFFFFF;
            height: 100%;
            left: 0;
            position: absolute;
            top: 0;
            width: 100%;
        }
        body #pagePlaceHolderOuter #pagePlaceHolderInner {
            background: none repeat scroll 0 0 yellow;
            height: 100%;
            margin: 0 auto;
            max-width: 1000px;
            min-width: 700px;
            width: 70%;
        }
        body #pagePlaceHolderOuter #pagePlaceHolderInner .footer {
            background: gray;
            bottom: 0;
            height: 20%;
            position: fixed;
            width: 100%;
        }
        </style>
        </head>
        <body>

        <div id="pagePlaceHolderOuter">
            <div id="pagePlaceHolderInner">
                <h1>Hi guys!</h1>
                <div class="footer"></div>
            </div>
        </div>

        </body>
        </html>

或检查jsfiddle

最佳答案

您不能按照您通常希望参照整个页面固定元素的方式将元素固定到容器中。相反,您可能必须手动为其指定与父级相同的尺寸,因为它不像普通的子级那样工作,而是像直接位于文档下方的元素一样工作

body #pagePlaceHolderOuter #pagePlaceHolderInner .footer {
    position: fixed;
    height: 100%;
    margin: 0 auto;
    max-width: 1000px;
    min-width: 700px;
    width: 70%;
    /* The rest of your code for this element */
}

Demo here

它继承父值的另一种方法是使其位置 absolute ,尽管这不是您想要的。为此,您还需要将 position:relative; 添加到它的容器中

body #pagePlaceHolderOuter #pagePlaceHolderInner {
    position:relative;
    /* The rest of your code for this element */
}
body #pagePlaceHolderOuter #pagePlaceHolderInner .footer {
    position:absolute;
    /* The rest of your code for this element */
}

Updated jsFiddle

关于html - CSS 问题 : fixed footer with width of 100% is too wide,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18432097/

相关文章:

html - 站点在 IE9 中显示的差异与今天的 IE8 不同

html - 碧 Jade 报告 : Aligning text as center in text field using HTML markup

css - 跨度类 "mark"中的背景颜色覆盖

CSS - 具有背景的响应式 div

javascript - 服务器端响应式网页设计

javascript - 下面这行 JavaScript 是如何工作的?

html - 媒体查询不会相互覆盖

html - 以内联 block 格式将标签/输入定位在一起

jquery - 响应式元素不会在 768 和 785 像素之间正确堆叠

html - Div 悬停而不是嵌套 - 响应式