css3实现背景模糊的三种方式

css3实现背景模糊使用伪元素,因为伪元素的模糊度不会被父元素的子代继承。

Posted in HTML / CSS onMarch 09, 2021

一、普通背景模糊

<Style>
        html,
        body {
            width: 100%;
            height: 100%;
        }

        * {
            margin: 0;
            padding: 0;
        }

        /*背景模糊*/
        .bg {
            width: 100%;
            height: 100%;
            position: relative;
            background: url("./bg.jpg") no-repeat fixed;
            background-size: cover;
            box-sizing: border-box;
            filter: blur(2px);
            z-index: 1;
        }

        .content {
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
            width: 200px;
            height: 200px;
            text-align: center;
            z-index: 2;
        }
    </Style>
</head>

<body>
    <div class="bg">
        <div class="content">背景模糊</div>
    </div>
</body>

效果如下所示:

css3实现背景模糊的三种方式

 这样写会使整个div的后代模糊并且还会出现白边,导致页面非常不美观,要想解决这个问题,我们可以使用伪元素,因为伪元素的模糊度不会被父元素的子代继承。

<Style>
        html,
        body {
            width: 100%;
            height: 100%;
        }

        * {
            margin: 0;
            padding: 0;
        }

        /*背景模糊*/
        .bg {
            width: 100%;
            height: 100%;
            position: relative;
            background: url("./bg.jpg") no-repeat fixed;
            background-size: cover;
            box-sizing: border-box;
            z-index: 1;
        }

        .bg:after {
            content: "";
            width: 100%;
            height: 100%;
            position: absolute;
            left: 0;
            top: 0;
            /* 从父元素继承 background 属性的设置 */
            background: inherit;
            filter: blur(2px);
            z-index: 2;
        }


        .content {
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
            width: 200px;
            height: 200px;
            text-align: center;
            z-index: 3;
        }
    </Style>
</head>

<body>
    <div class="bg">
        <div class="content">背景模糊</div>
    </div>
</body>

效果如下所示:

css3实现背景模糊的三种方式

 

二、背景局部模糊

上一个效果会了之后,局部模糊效果就比较简单了。

<Style>
        html,
        body {
            width: 100%;
            height: 100%;
        }

        * {
            margin: 0;
            padding: 0;
        }

        /*背景模糊*/
        .bg {
            width: 100%;
            height: 100%;
            position: relative;
            background: url("./bg.jpg") no-repeat fixed;
            background-size: cover;
            box-sizing: border-box;
            z-index: 1;
        }

        .content {
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
            width: 200px;
            height: 200px;
            background: inherit;
            z-index: 2;
        }

        .content:after {
            content: "";
            width: 100%;
            height: 100%;
            position: absolute;
            left: 0;
            top: 0;
            background: inherit;
            filter: blur(15px);
            /*为了模糊更明显,调高模糊度*/
            z-index: 3;
        }

        .content>div {
            width: 100%;
            height: 100%;
            text-align: center;
            line-height: 200px;
            position: absolute;
            left: 0;
            top: 0;
            z-index: 4;
        }
    </Style>
</head>

<body>
    <div class="bg">
        <div class="content">
            <div>背景局部模糊</div>
        </div>
    </div>
</body>

效果如下图所示:

css3实现背景模糊的三种方式

 三、背景局部清晰

<Style>
        html,
        body {
            width: 100%;
            height: 100%;
        }

        * {
            margin: 0;
            padding: 0;
        }

        /*背景模糊*/
        .bg {
            width: 100%;
            height: 100%;
            position: relative;
            background: url("./bg.jpg") no-repeat fixed;
            background-size: cover;
            box-sizing: border-box;
            z-index: 1;
        }

        .bg:after {
            content: "";
            width: 100%;
            height: 100%;
            position: absolute;
            left: 0;
            top: 0;
            background: inherit;
            filter: blur(5px);
            z-index: 2;
        }

        .content {
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
            width: 200px;
            line-height: 200px;
            text-align: center;
            background: inherit;
            z-index: 3;
            box-shadow: 0 0 10px 6px rgba(0, 0, 0, .5);
        }
    </Style>
</head>

<body>
    <div class="bg">
        <div class="content">
            <div>背景局部清晰</div>
        </div>
    </div>
</body>

效果如下图所示:

css3实现背景模糊的三种方式

HTML / CSS 相关文章推荐
CSS3中31种选择器使用方法教程
Dec 05 HTML / CSS
CSS3绘制不规则图形的一些方法示例
Nov 07 HTML / CSS
用CSS3的box-reflect来制作倒影效果
Nov 15 HTML / CSS
CSS3实现内凹圆角的实例代码
May 04 HTML / CSS
HTML5中FileReader接口使用方法实例详解
Aug 26 HTML / CSS
canvas实现飞机打怪兽射击小游戏的示例代码
Jul 09 HTML / CSS
HTML5 embed标签定义和用法详解
May 09 HTML / CSS
html5 初试 indexedDB(推荐)
Jul 21 HTML / CSS
前端实现背景虚化但内容清晰且自适应 的实例代码
Aug 01 HTML / CSS
高清屏下canvas重置尺寸引发的问题的解决
Oct 14 HTML / CSS
html5调用摄像头实例代码
Jun 28 HTML / CSS
css弧边选项卡的项目实践
May 07 HTML / CSS
HTML5如何适配 iPhone IOS 底部黑条
CSS3画一个阴阳八卦图
CSS中一些@规则的用法小结
Mar 09 #HTML / CSS
a标签的css样式四个状态
Mar 09 #HTML / CSS
详解CSS样式中的 !important * _ 符号
CSS中简写属性要注意TRouBLe的顺序问题(避免踩坑)
CSS心形加载的动画源码的实现
You might like
Cannot modify header information错误解决方法
2008/10/08 PHP
PHP数组循环操作详细介绍 附实例代码
2013/02/03 PHP
微信开发之获取JSAPI TICKET
2017/07/07 PHP
使用jquery局部刷新(jquery.load)从数据库取出数据
2014/01/22 Javascript
javascript基于HTML5 canvas制作画箭头组件
2014/06/25 Javascript
让checkbox不选中即将选中的checkbox不选中
2014/07/11 Javascript
node.js中的favicon.ico请求问题处理
2014/12/15 Javascript
使用原生JS实现弹出层特效
2014/12/22 Javascript
jQuery简单tab切换效果实现方法
2015/04/08 Javascript
基于Flowplayer打造一款免费的WEB视频播放器附源码
2015/09/06 Javascript
Adapter适配器模式在JavaScript设计模式编程中的运用分析
2016/05/18 Javascript
jQuery短信验证倒计时功能实现方法详解
2016/05/25 Javascript
jQuery获取table行数并输出单元格内容的实现方法
2016/06/30 Javascript
基于JavaScript实现鼠标向下滑动加载div的代码
2016/08/31 Javascript
AngularJS实现单独作用域内的数据操作
2016/09/05 Javascript
关于vuex的学习实践笔记
2017/04/05 Javascript
vue中for循环更改数据的实例代码(数据变化但页面数据未变)
2017/09/15 Javascript
详解10分钟学会vue滚动行为
2017/09/21 Javascript
解决koa2 ctx.render is not a function报错问题
2018/08/07 Javascript
微信小程序使用template标签实现五星评分功能
2018/11/03 Javascript
jQuery使用ajax传递json对象到服务端及contentType的用法示例
2020/03/12 jQuery
如何正确解决VuePress本地访问出现资源报错404的问题
2020/12/03 Vue.js
部署Python的框架下的web app的详细教程
2015/04/30 Python
Python抓取框架 Scrapy的架构
2016/08/12 Python
Python用list或dict字段模式读取文件的方法
2017/01/10 Python
详解Python 序列化Serialize 和 反序列化Deserialize
2017/08/20 Python
python实现kNN算法
2017/12/20 Python
Pytorch Tensor的索引与切片例子
2019/08/18 Python
Python Tornado之跨域请求与Options请求方式
2020/03/28 Python
HTML5的语法变化介绍
2013/08/13 HTML / CSS
一个精品风格的世界:Atterley
2019/05/01 全球购物
Python面试题:Python里面如何生成随机数
2015/03/12 面试题
文明学生标兵事迹
2014/01/21 职场文书
总经理岗位职责范本
2014/02/02 职场文书
在校生证明
2015/06/17 职场文书
保护环境建议书作文400字
2015/09/14 职场文书