css3实现背景模糊的三种方式(小结)


Posted in HTML / CSS onMay 15, 2020

一、普通背景模糊

代码:

<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实现背景模糊的三种方式(小结)

到此这篇关于css3实现背景模糊的三种方式的文章就介绍到这了,更多相关css3背景模糊内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章,希望大家以后多多支持三水点靠木!

HTML / CSS 相关文章推荐
浏览器实现移动端高性能css3动画(开启gpu加速)
Dec 23 HTML / CSS
CSS3效果:自定义“W”形运行轨迹实例
Mar 29 HTML / CSS
使用CSS3来代替JS实现交互
Aug 10 HTML / CSS
CSS3+JavaScript实现炫酷呼吸效果的示例代码
Jun 15 HTML / CSS
HTML5 Canvas实现文本对齐的方法总结
Mar 24 HTML / CSS
HTML5中语义化 b 和 i 标签
Oct 17 HTML / CSS
HTML5 history新特性pushState、replaceState及两者的区别
Dec 26 HTML / CSS
详解HTML5中的picture元素响应式处理图片
Jan 03 HTML / CSS
HTML5中使用json对象的实例代码
Sep 10 HTML / CSS
HTML5实现签到 功能
Oct 09 HTML / CSS
CSS精灵图的原理与使用方法介绍
Mar 17 HTML / CSS
CSS中calc(100%-100px)不加空格不生效
May 07 HTML / CSS
css3遮罩层镂空效果的多种实现方法
May 11 #HTML / CSS
css3的focus-within选择器的使用
May 11 #HTML / CSS
CSS3实现渐变背景兼容问题
May 06 #HTML / CSS
通过css3的filter滤镜改变png图片的颜色的示例代码
May 06 #HTML / CSS
基于ccs3的timeline时间线实现方法
Apr 30 #HTML / CSS
CSS3实现div从下往上滑入滑出效果示例
Apr 28 #HTML / CSS
深入浅析CSS3中的Flex布局整理
Apr 27 #HTML / CSS
You might like
php 设计模式之 工厂模式
2008/12/19 PHP
PHP随机生成唯一HASH值自定义函数
2015/04/20 PHP
PHP Post获取不到非表单数据的问题解决办法
2018/02/27 PHP
laravel框架模型和数据库基础操作实例详解
2020/01/25 PHP
JavaScript通过RegExp实现客户端验证处理程序
2013/05/07 Javascript
onbeforeunload与onunload事件异同点总结
2013/06/24 Javascript
js根据日期判断星座的示例代码
2014/01/23 Javascript
jQuery在ul中显示某个li索引号的方法
2015/03/17 Javascript
JavaScript中Number.NEGATIVE_INFINITY值的使用详解
2015/06/05 Javascript
jQuery实现简单下拉导航效果
2015/09/07 Javascript
基于Javascript实现弹出页面效果
2016/01/01 Javascript
jQuery简单创建节点的方法
2016/09/09 Javascript
js实现倒计时及时间对象
2016/11/15 Javascript
vue组件watch属性实例讲解
2017/11/07 Javascript
微信小程序录音与播放录音功能
2017/12/25 Javascript
Vue组件通信$attrs、$listeners实现原理解析
2020/09/03 Javascript
Python3连接MySQL(pymysql)模拟转账实现代码
2016/05/24 Python
Python发送http请求解析返回json的实例
2018/03/26 Python
使用Python进行QQ批量登录的实例代码
2018/06/11 Python
详解pandas如何去掉、过滤数据集中的某些值或者某些行?
2019/05/15 Python
Python基础之函数基本用法与进阶详解
2020/01/02 Python
python 伯努利分布详解
2020/02/25 Python
pandas to_excel 添加颜色操作
2020/07/14 Python
Python实现JS解密并爬取某音漫客网站
2020/10/23 Python
Django自定义YamlField实现过程解析
2020/11/11 Python
python中time.ctime()实例用法
2021/02/03 Python
Ticketmaster德国票务网站:购买音乐会和体育等门票
2016/11/14 全球购物
BOSE德国官网:尽探索之力,享音乐之极
2016/12/11 全球购物
英国音乐设备和乐器商店:Gear4music
2017/10/16 全球购物
儿科主治医生个人求职信
2013/09/23 职场文书
大型晚会策划方案
2014/02/06 职场文书
财务科科长岗位职责
2014/03/10 职场文书
解除聘用合同证明书范本
2014/09/11 职场文书
幼儿园辞职信范文
2015/02/27 职场文书
如何理解Vue前后端数据交互与显示
2021/05/10 Vue.js
JavaScript实现音乐播放器
2022/08/14 Javascript