理解 CSS3 滤镜函数
滤镜函数用于设置元素的视觉效果,例如对比度、亮度、色相旋转、不透明度以及图像等。让我们看看一些滤镜函数。
语法
以下是 CSS 中 filter 的语法:
filter: none | drop-shadow() | blur() | brightness() | contrast() | grayscale() | hue-rotate() | invert() | opacity() | saturate() | sepia() | url();
如上所示,使用filter属性,我们可以设置以下效果:模糊、对比度、投影、模糊、亮度、灰度、色相旋转、反色、不透明度、饱和度、褐色调、url。
反色
invert()用于反转图像中的样本。这里,0% (0) 是实际图像,而 100% 的值将完全反转图像。要在 CSS3 中反转图像,请对filter属性使用invert()。
示例
让我们来看一个使用反转为图像添加视觉效果的示例:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
img{
margin: 20px;
}
img.normal{
width: 300px;
height: 300px;
}
img.filter {
width: 300px;
height: 300px;
filter: invert(100%);
}
</style>
</head>
<body>
<h1>CSS Image effects example</h1>
<img class="normal" src="https://tutorialspoint.com/python/images/python-mini-logo.jpg">
<img class="filter" src="https://tutorialspoint.com/python/images/python-mini-logo.jpg">
</body>
</html>
对比度
要在 CSS 中设置图像对比度,请使用 filter contrast(%)。请记住,值为 0 会使图像变黑,100% 用于原始图像和默认图像。其余的,您可以设置任何您选择的值,但超过 100% 的值会使图像对比度更高。
示例
现在让我们来看一个使用 CSS3 调整图像对比度的示例:
<!DOCTYPE html>
<html>
<head>
<style>
img.demo {
filter: brightness(120%);
filter: contrast(120%);
}
</style>
</head>
<body>
<h1>Learn MySQL</h1>
<img src="https://tutorialspoint.com/mysql/images/mysql-mini-logo.jpg" alt="MySQL" width="160" height="150">
<h1>Learn MySQL</h1>
<p>Below image is brighter and has more contrast than the original image above.</p>
<img class="demo" src="https://tutorialspoint.com/mysql/images/mysql-mini-logo.jpg" alt="MySQL" width="160" height="150">
</body>
</html>
模糊
filter属性用于设置视觉效果,例如模糊、投影、对比度、亮度、饱和度、阴影等 CSS 中的图像。我们将看到如何创建一个模糊图像。blur用于模糊图像。要在 CSS3 中设置模糊效果,请对filter属性使用blur值。值越大,模糊程度越高。
示例
以下是使用 CSS 创建模糊背景图像的代码:
<!DOCTYPE html>
<html>
<head>
<style>
body,
html {
height: 100vh;
margin: 0;
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}
* {
box-sizing: border-box;
}
.backgroundImage {
background-image: url("https://images.pexels.com/photos/1172207/pexels-photo-1172207.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500");
filter: blur(10px);
height: 100%;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
.transparentText {
background-color: rgba(0, 0, 0, 0.4);
color: white;
border: 3px solid #f1f1f1;
position: absolute;
top: 40%;
left: 30%;
width: 50%;
padding: 20px;
text-align: center;
}
</style>
</head>
<body>
<div class="backgroundImage"></div>
<div class="transparentText">
<h1>I am Amit</h1>
<h1>I am an Entrepreneur</h1>
</div>
</body>
</html>
投影
要在 CSS3 中为图像添加投影,请对 filter 属性使用 drop-shadow 值。它具有以下值:
h-shadow - 指定水平阴影的像素值。
v-shadow - 指定垂直阴影的像素值。负值将阴影放在图像上方。
blur - 为阴影添加模糊效果。
spread - 正值会导致阴影扩大,负值会导致阴影缩小。
color - 为阴影添加颜色
示例
现在让我们来看一个示例:
<!DOCTYPE html>
<html>
<head>
<style>
img.demo {
filter: brightness(120%);
filter: contrast(120%);
filter: drop-shadow(10px 10px 10px green);
}
</style>
</head>
<body>
<h1>Learn MySQL</h1>
<img src="https://tutorialspoint.com/mysql/images/mysql-mini-logo.jpg" alt="MySQL" width="160" height="150">
<h1>Learn MySQL</h1>
<img class="demo" src="https://tutorialspoint.com/mysql/images/mysql-mini-logo.jpg" alt="MySQL" width="160" height="150">
</body>
</html>
褐色调效果
要使用 CSS3 将褐色调效果应用于图像,请对 filter 属性使用 sepia 值。代码如下:
示例
<!DOCTYPE html>
<html>
<head>
<style>
img.demo {
filter: sepia(100%);
}
</style>
</head>
<body>
<h1>Learn Spring Framework</h1>
<img src="https://tutorialspoint.com/spring/images/spring-mini-logo.jpg" alt="Spring Framework" width="160" height="150">
<h1>Learn Spring Framework</h1>
<img class="demo" src="https://tutorialspoint.com/spring/images/spring-mini-logo.jpg" alt="Spring Framework" width="160" height="150">
</body>
</html>
色相旋转
色相旋转调整即增加或减少图像色相值。要在 CSS3 中设置图像的色相旋转,请对filter属性使用hue-rotate值。色相旋转以度数设置,即
实际图像 - 0 度,即默认值
旋转 30 度 - 30 度
旋转 90 度 - 90 度,等等。
正色相旋转 - 增加色相值
负色相旋转 - 减少色相值
示例
现在让我们来看一个使用 CSS3 应用色相旋转的示例。我们将使用色相旋转设置 45 度:
<!DOCTYPE html>
<html>
<head>
<style>
img.demo {
filter: hue-rotate(45deg);
}
</style>
</head>
<body>
<h1>Learn Spring Framework</h1>
<img src="https://tutorialspoint.com/spring/images/spring-mini-logo.jpg" alt="Spring Framework" width="160" height="150">
<h1>Learn Spring Framework</h1>
<img class="demo" src="https://tutorialspoint.com/spring/images/spring-mini-logo.jpg" alt="Spring Framework" width="160" height="150">
</body>
</html>
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP