CSS实现毛玻璃模糊效果

效果展示

效果实现

一个div,将文字放进去

<body>
    <div class="box">Welcome to Ilirus' blog</div>
</body>

美化样式

body {
    --bg:url("image.jpg");
    background: var(--bg) fixed top center / cover;
}
.box{
    background: hsla(0,0%,100%,0.2);
    color: aliceblue;
    font-size: 38px;
    width: 12em;
    height: 3em;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 0.6em;
    box-shadow: 0px 0px 3px 3px hsla(0,0%,0%,0.4);
    text-shadow: 0px 0px 8px #FFF;
}

大致效果应该是这样的

使用伪元素实现模糊效果

为div创建一个伪元素

.box::before{
    content: '';
}

设置定位和宽高

.box::before{
    position: absolute;
    width: inherit;
    height: inherit;
}

添加背景颜色,以便观察调试

.box::before{
    background: hsla(0,100%,83%,0.4);
}

现在效果应该是这样

添加模糊效果和border-radius

.box::before{
    border-radius: 0.6em;
    filter: blur(5px);
}

伪元素把文本挡住了,给它设置z-index

.box::before{
    z-index: -1;
}

现在将背景换成图片就可以了

.box::before{
    background: var(--bg) fixed top center / cover;
}

CSS代码

body {
    --bg:url("image.jpg");
    background: var(--bg) fixed top center / cover;
}
.box{
    background: hsla(0, 0%, 100%, 0.2);
    color: aliceblue;
    font-size: 38px;
    width: 12em;
    height: 3em;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 0.6em;
    box-shadow: 0px 0px 3px 3px hsla(0, 0%, 0%, 0.4);
    text-shadow: 0px 0px 8px #FFF;
}
.box::before{
    content: '';
    border-radius: 0.6em;
    position: absolute;
    width: inherit;
    height: inherit;
    background: var(--bg) fixed top center / cover;
    filter: blur(10px);
    z-index: -1;
}
Licensed under CC BY-NC-SA 4.0