html - 宽度固定为 100% 的位置忽略了 body 填充

标签 html css

我正在尝试制作一个页脚,该页脚跨越页面宽度的左侧和右侧减去 10px。我试图通过在 10px 的所有边上给 body 一个填充来做到这一点。在下面的代码中,页眉工作得很好,但页脚忽略了右侧的正文填充。为什么会这样,我该如何解决?

<!DOCTYPE html>
<html>
    <head>
        <style>
            div {
                padding:0;
                margin:0;
            }
            body {
                margin: 0;
                padding: 0 10px;
            }
            #header {
                height: 150px;
                width: 100%;
                margin: 0 auto;
                background: #333;
            }
            #footer {
                position: fixed;
                bottom: 5px;
                width: 100%;
                background: #f63;
                text-align: center;
            }
        </style>
    </head>
    <body>
        <div id="header"></div>
        <div id="footer">I am the footer!</div>
    </body>
</html>

最佳答案

你的页脚没有忽略 body padding,通过控制台查看那个元素大小,你会看到你的页脚宽度是窗口宽度的 100% + 左填充 10px + 右填充 10px。

您可以在 css 中使用 calc 函数:https://developer.mozilla.org/en-US/docs/Web/CSS/calc

#footer {
  width: calc(100% - 20px);
}

JSFiddle

关于html - 宽度固定为 100% 的位置忽略了 body 填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22159588/

相关文章:

html - 在 Emacs 中显示远程 HTML 的内容

css - 使 CSS 属性依赖于另一个属性

css - 如何在 Meteor-React 设置中使用 D3-force 渲染 D3?

javascript - 检测 Firefox 中的自动填充输入

javascript - 使用 Javascript 将 Bootstrap 下拉列表连接到对象并循环

javascript - 防止子 iFrame 获得焦点

javascript - 网页需要灵活的用户界面

html - 边界半径圆

html - ADF 不会更改为 CSS :checked state

html - 布局和 float div问题