【问题标题】:cover div adding a margin覆盖 div 添加边距
【发布时间】:2013-10-06 23:01:16
【问题描述】:

我有一个 #cover div 和以下 css

#cover {
    background-color: #FFFFFF;
    height: 100%;
    opacity: 0.4;
    position: fixed;
    width: 100%;
    z-index: 9000;
}

我希望它像这样覆盖整个(查看过的)页面

但是当我向下滚动时,我看到了这个

这是因为margin 被添加到顶部和左侧。

我尝试添加margin:-8 -8 8 8;,但没有成功。为什么???我该如何解决这个问题?

DEMO fiddle.

【问题讨论】:

标签: css compiler-construction margin


【解决方案1】:

你去设置一个位置。只需添加:

top: 0;
left: 0;

由于您没有发布完整的标记,我只能假设您的身体有一个边距或填充,导致您看到的转变。

【讨论】:

    【解决方案2】:

    简单的解决方法。

    将其添加到您的 CSS

    * /*Remove all the default margin and padding*/
    {
        margin: 0;
        padding: 0;
    }
    html, body /*so it'll work when you specify 100% height on element*/
    {
        height: 100%;
    }
    

    顺便说一句:使用单位(px/%)作为除 0 以外的边距/填充。

    这是Fiddle

    另外,您甚至不需要 fixed 的位置。查看 fiddle

    【讨论】:

    • 你没有读过这个问题,或者我错过了什么,试着做一个小提琴。
    • 你显然遗漏了一些东西。Here you Go.
    • 不,是(+1)我刚刚删除了margin:-8 -8 8 8;。就这些
    • 哦..对不起。另外:您可以在没有fixed 的情况下使用它(查看新小提琴)
    猜你喜欢
    • 2012-05-26
    • 2013-05-20
    • 1970-01-01
    • 2013-02-18
    • 2016-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多