How to set background blur with CSS

How to set background blur with CSS

When making some pages, in order to make the pages look better, we often need to set some background images. However, when the background images are too fancy, it will affect our main content, so we need to use the filter attribute to set its blur value.

The html code is as follows

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<div class="cover">
  <h1>I am the content that needs to be highlighted</div>
</body>
</html>

But if you use it directly on the background image,

.cover{
    width:600px;
    height:300px;
    position:relative;
    text-align:center;
    line-height:300px;
    color:white;
    background:transparent url(http://91jean.oss-cn-hangzhou.aliyuncs.com/18-8-31/16567303.jpg) center center no-repeat;
    filter:blur(5px);
    background-size:cover;
}

The following situation may occur.

We will find that both the background and the main content have become blurred.

Solution: Add a pseudo element to the background image, set the background image and filter attributes on the pseudo element. The specific code is as follows

.cover{
    width:600px;
    height:300px;
    position:relative;
    text-align:center;
    line-height:300px;
    color:white;
}

.cover::before{    
    content:'';
    position:absolute;
    top:0;
    left:0;
    width:600px;
    height:300px;
    background:transparent url(http://91jean.oss-cn-hangzhou.aliyuncs.com/18-8-31/16567303.jpg) center center no-repeat;
    filter:blur(5px);
    z-index:-1;
    background-size:cover;
} 

Summarize

The above is the implementation method of setting background blur with CSS introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

<<:  JavaScript to implement retractable secondary menu

>>:  Use the docker build kit to build a Docker image that can be used on the Raspberry Pi

Recommend

Awk command line or script that helps you sort text files (recommended)

Awk is a powerful tool that can perform some task...

Solution to the problem that docker logs cannot be retrieved

When checking the service daily, when I went to l...

Detailed explanation of commands to view linux files

How to view linux files Command to view file cont...

Mac VMware Fusion CentOS7 configuration static IP tutorial diagram

Table of contents Install CentOS7 Configuring Sta...

Theory: The two years of user experience

<br />It has been no more than two years sin...

Use the sed command to modify the kv configuration file in Linux

sed is a character stream editor under Unix, that...

Sharing tips on using scroll bars in HTML

Today, when we were learning about the Niu Nan new...

Tutorial on Migrating Projects from MYSQL to MARIADB

Prepare the database (MySQL). If you already have...

Docker deploys mysql remote connection to solve 2003 problems

Connecting to MySQL Here I use navicat to connect...

HTML code text box limit input text box becomes gray limit text box input

Method 1: Set the readonly attribute to true. INPU...

Solution to the failure of docker windows10 shared directory mounting

cause When executing the docker script, an error ...

How to receive binary file stream in Vue to realize PDF preview

Background Controller @RequestMapping("/getP...

3 methods to restore table structure from frm file in mysql [recommended]

When mysql is running normally, it is not difficu...

mysql group_concat method example to write group fields into one row

This article uses an example to describe how to u...

Vue implements countdown function

This article example shares the specific code of ...