Table of Contents
grammar
The concept of fuzzy distance in CSS
How to apply blur distance in CSS using the "blur" property
Example 3
Applying blur distance in CSS using the 'blur' property
Example 2: Apply different blur distances to different elements
Creating a focus effect with blurred distance in CSS
Creating a focus effect with blur distance in CSS
Heading
in conclusion
Home Web Front-end CSS Tutorial How to set blur distance in CSS?

How to set blur distance in CSS?

Sep 01, 2023 pm 10:37 PM

How to set blur distance in CSS?

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

grammar

css element {
   filter : blur (amount)
}
Copy after login

The concept of fuzzy distance in CSS

Before discussing the practical aspects of setting fuzzy distance, it is important to understand the concept of fuzzy distance. The amount of blur applied to an element is called the blur distance. The 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, you need to use the blur attribute. The Blur property sets the amount of blur that should be applied to the element. To use the Blur attribute, we first select the element to which we want to apply the blur effect and then set the Blur attribute to the desired value.

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

img {
   filter : blur(5px)
}
Copy after login

Here we have selected the "img" element and applied a 5 pixel blur effect to it.

Example 1

This is a complete code example of the above method.

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

We can apply different fuzzy distances to different elements in the web page. To do this, we can select each element individually and set the "blur" property to the desired value. For example -

h1 {
   filter : blur(3px)
}

img {
   filter : blur(5px)
}
Copy after login

Here, we have selected two elements, the first "h1" and the 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 id="Applying-blur-distance-in-CSS-using-the-blur-property">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="/static/imghw/default1.png"  data-src="https://www.tutorialspoint.com/dip/images/einstein.jpg"  class="lazy" alt="My Image">
</body>
</html>
Copy after login

Creating a focus effect with blurred distance in CSS

Creating a focus effect is the most common use of blur effects in CSS. This effect is achieved by applying a blur effect to all elements on the web page except the focused element.

To create a focus effect with blurred distance in CSS, use the "blur" attribute with the ":not" pseudo-class. The ':not' pseudo-class allows the user to select all elements except specific elements. For example -

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

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

Here, we apply the "blur" attribute to all child elements of the "blur" class when the user hovers over the "blur" class. This effect does not work on elements with class "no-blur" because using the "no" pseudo-class excludes elements with class "no-blur" from the blur effect.

Example 3

This is a complete code example of the above method.

<!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 id="Creating-a-focus-effect-with-blur-distance-in-CSS">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 id="Heading">Heading</h2>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
      <button class="no-blur">Click me</button>
   </div>
</body>
</html>
Copy after login

in conclusion

Setting blur distance in CSS is a simple and effective way to add visual appeal to your web pages. With the following examples, you can easily create blur effects on web pages.

The above is the detailed content of How to set blur distance in CSS?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It&#039;s out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That&#039;s like this.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

Using Markdown and Localization in the WordPress Block Editor Using Markdown and Localization in the WordPress Block Editor Apr 02, 2025 am 04:27 AM

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Apr 05, 2025 pm 05:51 PM

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...

How to select a child element with the first class name item through CSS? How to select a child element with the first class name item through CSS? Apr 05, 2025 pm 11:24 PM

When the number of elements is not fixed, how to select the first child element of the specified class name through CSS. When processing HTML structure, you often encounter different elements...

See all articles