css(层叠样式表)是设计网站视觉效果的强大工具。 模糊效果是css中的众多功能之一,它用于使用模糊来模糊任何元素财产。要创建柔和、梦幻的效果或将注意力吸引到页面上的特定元素,我们可以使用模糊效果。
语法css element { filter : blur (amount)}
css中模糊距离的概念在讨论设置模糊距离的实际方面之前,了解模糊距离的概念很重要。应用于元素的模糊量称为模糊距离。该值可以用像素 (px)、em 或其他 css 单位来定义。
如何使用“blur”属性在 css 中应用模糊距离要在css中设置模糊距离,需要使用blur属性。 “模糊”属性设置应应用于元素的模糊量。要使用“模糊”属性,我们首先选择要应用模糊效果的元素,然后将“模糊”属性设置为所需的值。
例如,要对图像应用模糊效果,我们可以使用以下 css 代码 -
img { filter : blur(5px)}
在这里,我们选择了“img”元素并对其应用了 5 像素的模糊效果。
示例 1这是上述方法的完整代码示例。
<!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>
我们可以对网页中的不同元素应用不同的模糊距离。为此,我们可以单独选择每个元素并将“模糊”属性设置为所需的值。例如 -
h1 { filter : blur(3px)}img { filter : blur(5px)}
在这里,我们选择了两个元素,第一个“h1”和第二个“img”,并相应地应用了 3 像素和 5 像素的模糊效果。
示例 2:对不同元素应用不同的模糊距离<!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>
在 css 中创建具有模糊距离的焦点效果创建焦点效果是 css 中模糊效果最常见的用途。这种效果是通过对网页上除焦点元素之外的所有元素应用模糊效果来实现的。
要在 css 中创建具有模糊距离的焦点效果,请将“blur”属性与“:not”伪类一起使用。 ':not' 伪类允许用户选择除特定元素之外的所有元素。例如 -
.blur * { transition: all 0.5s ease-in-out;}.blur:hover *:not(.no-blur) { filter: blur(5px);}
在这里,当用户将鼠标悬停在“blur”类上时,我们将“blur”属性应用于“blur”类的所有子元素。此效果不适用于具有“no-blur”类的元素,因为使用“no”伪类会将具有“no-blur”类的元素从模糊效果中排除。
示例 3这是上述方法的完整代码示例。
<!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>
结论在 css 中设置模糊距离是为网页添加视觉吸引力的简单而有效的方法。借助以下示例,您可以轻松在网页上创建模糊效果。
以上就是css中如何设置模糊距离?的详细内容。