How to set blur distance in CSS?


CSS (Cascading Style Sheets) is a powerful tool for designing the visual effects of a website. The blur effect is one of the many features in CSS, which is used to blur any element using the blur property. To create a soft, dreamy effect or to draw attention to a specific element on a page, we can use the blur effect.

Syntax

css element {
   filter : blur (amount)
}

The Concept of Blur Distance in CSS

Before discussing the practical aspect of setting blur distance, it is important to understand the concept of blur distance. The amount of blur that is applied to an element is called the blur distance. This value can be defined in pixels (px), em, or other CSS units.

How to Apply Blur Distance in CSS Using the 'blur' Property

To set the blur distance in CSS, the blur property is used. The 'blur' property sets the amount of blur that should be applied to an element. To use the 'blur' property, we first select the element that we want to apply the blur effect and then set the 'blur' property to the desired value.

For example, to apply blur effect to an image, we can use the following CSS code −

img {
   filter : blur(5px)
}

Here, we have selected the 'img' element and applied a blur effect of 5 pixels on it.

Example 1

Here is a full code example on the above approach.

<!DOCTYPE html>
<html>
<head>
   <style>
      body{
         text-align:center;
      }
      img {
         filter: blur(3px);
      }
   </style>
</head>
<body>
   <h3>Applying blur distance in CSS using the 'blur' property</h3>
   <img src="https://www.tutorialspoint.com/dip/images/einstein.jpg" alt="My Image">
</body>

We can apply different blur distances to different elements in a web page. For doing this, we can select each element separately and set the 'blur' property to the desired value. For example −

h1 {
   filter : blur(3px)
}

img {
   filter : blur(5px)
}

Here, we have selected the two elements first ‘h1’ and second ‘img’ and applied a blur effect of 3 pixels and 5 pixels accordingly.

Example 2: Apply Different Blur Distances to Different Elements

<!DOCTYPE html>
<html>
<head>
   <style>
      body{
         text-align:center;
      }
      p{
         filter: blur(2px);
         font-size: 20px;
         color: red;
      }
      img {
         filter: blur(3px);
      }
   </style>
</head>
<body>
   <h3>Applying blur distance in CSS using the 'blur' property</h3>
   <p>Applied blur distance 2 pixels in CSS using the 'blur' property</p>
   <img src="https://www.tutorialspoint.com/dip/images/einstein.jpg" alt="My Image">
</body>
</html>

Creating a Focus Effect With Blur Distance in CSS

Creating a focus effect is the most common use of the blur effect in CSS. This effect is achieved by applying a blur effect to all elements on the webpage, except for the element that has the focus.

To create a focus effect with blur distance in CSS, the 'blur' property is used with the ':not' pseudo-class. The ':not' pseudo-class allows the user to select all elements except for a particular element. For example −

.blur * {
   transition: all 0.5s ease-in-out;
}

.blur:hover *:not(.no-blur) {
   filter: blur(5px);
}

Here, we have applied the 'blur' property to all child elements of the 'blur' class, when the user hovers over the 'blur' class. This effect does not apply to elements with the 'no-blur' class, because using the 'no' pseudo-class excludes elements with the 'no-blur' class from the blur effect.

Example 3

Here is a full code example on the above approach.

<!DOCTYPE html>
<html>
<head>
   <style>
      body {
         text-align: center;
      }
      .blur * {
         transition: all 0.5s ease-in-out;
      }
      .blur:hover *:not(.no-blur) {
         filter: blur(5px);
      }
   </style>
</head>
<body>
   <h2>Creating a focus effect with blur distance in CSS</h2>
   <p> Hover over the below content to see the effect </p>
   <div class="blur">
      <h2>Heading</h2>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
      <button class="no-blur">Click me</button>
   </div>
</body>
</html>

Conclusion

Setting blur distance in CSS is a simple and effective way to add visual appeal to web pages. You can easily create a blur effect on a web page with the help of the following example.

Updated on: 12-Apr-2023

202 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements