我有一个JPEG文件,我使用作为搜索页面的背景图像,我使用CSS来设置它,因为我在Backbone.js上下文中工作:
background-image: url("whatever.jpg");
我想应用css3模糊过滤器只对背景,但我不确定如何样式只是一个元素。如果我试着:
-webkit-filter: blur(5px);
-moz-filter: blur(5px);
-o-filter: blur(5px);
-ms-filter: blur(5px);
filter: blur(5px);
就在我的CSS中的background-image下面,它为整个页面设置了样式,而不仅仅是背景。是否有一种方法只选择图像并应用过滤器?或者,有没有一种方法可以关闭页面上所有其他元素的模糊效果?
pen
取消了对额外元素的需求,同时使内容适合文档流,而不是像其他解决方案那样固定/绝对。
通过使用
.content {
/* this is needed or the background will be offset by a few pixels at the top */
overflow: auto;
position: relative;
}
.content::before {
content: "";
position: fixed;
left: 0;
right: 0;
z-index: -1;
display: block;
background-image: url('https://i.imgur.com/lL6tQfy.png');
background-size:cover;
width: 100%;
height: 100%;
-webkit-filter: blur(5px);
-moz-filter: blur(5px);
-o-filter: blur(5px);
-ms-filter: blur(5px);
filter: blur(5px);
}
<div class="content">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
编辑如果你想删除边缘的白色边框,使用宽度和高度为110%,左侧和顶部为-5%。这将放大你的背景-但不应该有纯色从边缘流出。感谢Chad Fawcett的建议。
.content {
/* this is needed or the background will be offset by a few pixels at the top */
overflow: auto;
position: relative;
}
.content::before {
content: "";
position: fixed;
top: -5%;
left: -5%;
right: -5%;
z-index: -1;
display: block;
background-image: url('https://i.imgur.com/lL6tQfy.png');
background-size:cover;
width: 110%;
height: 110%;
-webkit-filter: blur(5px);
-moz-filter: blur(5px);
-o-filter: blur(5px);
-ms-filter: blur(5px);
filter: blur(5px);
}
<div class="content">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
虽然所有提到的解决方案都非常聪明,但当我尝试它们时,它们似乎都有一些小问题或与页面上的其他元素产生潜在的连锁影响。
最后,为了节省时间,我只是回到了我以前的解决方案:我使用Paint。选择效果,高斯模糊,半径5到10像素,保存为页面图像。: -)
HTML:
<body class="mainbody">
</body
CSS:
body.mainbody
{
background: url('../images/myphoto.blurred.png');
-moz-background-size: cover;
-webkit-background-size: cover;
background-size: cover;
background-position: top center !important;
background-repeat: no-repeat !important;
background-attachment: fixed;
}
编辑:
我终于让它工作了,但解决方案绝不简单!在这里看到的:
过滤器:模糊(1 px);不能在firefox, IE和opera中工作