【问题标题】:Applying blur filter to background, doesn't keep my div centered将模糊滤镜应用于背景,不会让我的 div 居中
【发布时间】:2016-05-23 23:21:43
【问题描述】:

所以我在正在制作的网页上登录时有一个模糊的图像 div。

这是目前的样子:

现在,图像的模糊部分应该只在页面中间,我的尺寸如下面的 SCSS 所示:

//Variables
$background_image: url(../img/bg.png);
$background_fallback: #C0C0C0;

body {
    padding: 0; margin: 0;
    background-image: $background_image;
    background-position: 0px 0px;
    background-repeat: repeat;
    color: #333;

    animation: bg 40s linear infinite;

    min-height: 100%;
    width: 100%;
}

.login_container {
    margin: auto;
    overflow: auto;
    position: absolute;
    top: 0; left: 0; bottom: 0; right: 0;
    width: 40%;
    height: 60%;

    .login_container_bg {
        position: fixed;
        left: 0; right: 0; z-index: 1;
        display: block;
        background-image: $background_image;
        width: 100%;
        height: 100%;

        -webkit-filter: blur(2px);
        -moz-filter: blur(2px);
        -o-filter: blur(2px);
        -ms-filter: blur(2px);
        filter: blur(2px);
        animation: bg 40s linear infinite;
    }
    .login_container_inner {
        position: fixed;
        left: 0; right: 0;
        z-index: 2;
        margin-left: 20px; margin-right: 20px;
    }
}

@keyframes bg {
    from { background-position: 0 0; }
    to { background-position: 0 1000px; }
}

这是我使用的 HTML 结构:

<body>
    <div class="login_container">
        <div class="login_container_bg"></div>
        <div class="login_container_inner">
            Hello. It's me.
        </div>
    </div>
</body>

如您所见,背景的模糊部分扩展了页面的宽度。我希望模糊位只是内部的宽度和高度 - 应该是页面的 50% 和 50% (x, y)

【问题讨论】:

  • 您的 .login_container_inner 已经居中。请提供更多您期望的细节。
  • @SebastianWiteczek 如您所见,背景的模糊部分扩展了页面的宽度。我希望模糊位只是内部的宽度和高度 - 应该是页面的 50% 和 50% (x, y)

标签: html css sass


【解决方案1】:

元素的位置是视口的fixedleft:0,而不是容器。 - 固定 元素相对于浏览器窗口定位 http://www.w3schools.com/cssref/pr_class_position.asp

要么将其更改为绝对定位(相对于容器),要么将其相对于视口固定。

编辑:试试这样的。

HTML

    <div class="login_container_bg">
        <div class="login_container_inner">
            Hello. It's me.
        </div>
     </div>

CSS

.login_container {
margin: auto;
overflow: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
background-image: $background_image;

.login_container_bg {
    position: absolute;
    left: 50%; 
    top: 50%;
    transform:translateX(-50%) translateY(-50%);
    z-index: 1;
    display: block;
    width: 40%;
    height: 60%;

    -webkit-filter: blur(2px);
    -moz-filter: blur(2px);
    -o-filter: blur(2px);
    -ms-filter: blur(2px);
    filter: blur(2px);
    animation: bg 40s linear infinite;
}
.login_container_inner {
    position: absolute;
    left: 0; right: 0;
    z-index: 2;
    margin-left: 20px; margin-right: 20px;
}
}

【讨论】:

  • Gavin,当我将相对定位应用于 bg 和内部元素时,它们会出现在另一个之上。 i.imgur.com/4ctRRMk.png
  • 我想你误解了我的回答。
  • 哦,所以您的意思是将绝对定位应用于内部元素以及顶级元素?好的,完美!干杯
  • 加文,你的回答也模糊了我内心的所有元素
  • 在您提供的标记中,您的内部没有其他元素。我不能创造奇迹。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-14
  • 1970-01-01
  • 2021-09-04
  • 2018-04-24
  • 2021-03-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多