html - 使用 CSS 使文本框背景模糊

标签 html css css-filters

整个问题都在我创建的这个 JSFiddle 页面上进行了演示:http://jsfiddle.net/r480pggf/4/

我的目的是让黑框的背景变得模糊。黑框应该包含 p 标签内的文本。

我几乎已经解决了这个问题,但出于某种原因,如果我在黑框内使用 p 标签,它会在黑框顶部造成模糊(可能有点难以看清,请仔细观察你的显示器)。如果删除 p 标签,一切都会按预期进行,但我想在黑框内随意使用 p 标签。

HTML

<div class="content-box">
    <div class="content-bg"></div>
    <div class="content">
        <p>Text with blurred background</p>
        <br>Text with blurred background
        <br>Text with blurred background
        <br>Text with blurred background
        <br>Text with blurred background
        <br>Text with blurred background
        <br>Text with blurred background
    </div>
</div>

CSS

body {
    background-image: url('http://7-themes.com/data_images/out/65/6995506-wood-textured-desktop-background.jpg');
    background-color: black;
    background-repeat: no-repeat;
    background-position: center;
    background-attachment: fixed;
    background-size: cover;
    background-size: no-repeat fixed center center cover;
    overflow: hidden;
}

.content-box {
    position: relative;
    width: 300px;
    overflow: hidden;
}

.content-bg {
    position: absolute;
    background-size: cover;
    background-image: url('http://7-themes.com/data_images/out/65/6995506-wood-textured-desktop-background.jpg');
    background-color: black;
    background-repeat: no-repeat;
    background-position: center;
    background-attachment: fixed;
    background-size: cover;
    background-size: no-repeat fixed center center cover;
    filter: blur(5px);
    -webkit-filter: blur(5px);
}

.content {
    border-radius: 10px;
    z-index: 100;
    background-color: black;
    opacity: 0.7;
    color: white;
}

JS

function updateBackground() {
    var contentBox = $(".content-box");
    var bg = $(".content-bg");
    bg.css("left", -(contentBox.offset().left));
    bg.css("top", -(contentBox.offset().top));
    bg.width($(window).width());
    bg.height($(window).height());
}

$(window).resize(function() {
    updateBackground();
});

updateBackground();

最佳答案

棕色框顶部外部的模糊来自第一个元素的边距,如果它有一些 p 元素默认有顶部和底部边距。您需要删除此边距,例如:

.content :first-child { margin-top: 0px }

fiddle

编辑:

这是一个与边距相关的事情,以及边距的崩溃。请参阅 MDN 引用 here .相关部分是:

Parent and first/last child : If there is no border, padding, inline content, or clearance to separate the margin-top of a block with the margin-top of its first child block, [...] then those margins collapse. The collapsed margin ends up outside the parent.

我在评论中采用了您的示例,以使其显示与边框的区别:fiddle .

对于您的问题,另一种解决方案可能是为您的 .content 框提供透明边框:

.content { border: solid 1px transparent; }

fiddle .

关于html - 使用 CSS 使文本框背景模糊,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32027740/

相关文章:

html - Bootstrap CSS 和其他 CSS/HTML 组件包

javascript - 找到 child H3 并检查是否有值(value)

html - Chrome 图像无法呈现

PHP/HTML/CSS 表格滚动

javascript - 如何运行通过ajax进程包含的javascript代码

php - 使用 php 和 .htaccess 进行友好的 url 重写。相对链接到 css 图像和 js 的困难

html - 使用 css 在 html 上对齐我的 block 文本

html - 2 栏设计,如何将我的左栏滚动到页面右侧

css - 模糊过渡时如何防止图像闪烁?

html - 重复使用带过滤器的背景图像