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

Detailed explanation of how to use CMD command to operate MySql database

First: Start and stop the mysql service net stop ...

JavaScript implements constellation query function with detailed code

Table of contents 1. Title 2. Code 3. Results IV....

Detailed explanation of how two Node.js processes communicate

Table of contents Preface Communication between t...

Eight examples of how Vue implements component communication

Table of contents 1. Props parent component ---&g...

Complete steps for Docker to pull images

1. Docker pull pulls the image When using $ docke...

MySQL derived table (Derived Table) simple usage example analysis

This article uses an example to describe the simp...

A brief analysis of the difference between static and self in PHP classes

Use self:: or __CLASS__ to get a static reference...

Solution to forgetting the MYSQL database password under MAC

Quick solution for forgetting MYSQL database pass...

How to change the root user's password in MySQL

Method 1: Use the SET PASSWORD command mysql> ...

How to use display:olck/none to create a menu bar

The effect of completing a menu bar through displ...

Solution to the problem of mysql master-slave switch canal

After configuring VIP, the error message that app...

HTML page header code is completely clear

All the following codes are between <head>.....

Example analysis to fix problems in historical Linux images

Fix for issues with historical Linux images The E...

Incomplete solution for using input type=text value=str

I encountered a very strange problem today. Look a...

Go to another file after submitting the form

<br />Question: How to write in HTML to jump...