- CSS 教程
- CSS - 首页
- CSS - 路线图
- CSS - 简介
- CSS - 语法
- CSS - 选择器
- CSS - 包含
- CSS - 测量单位
- CSS - 颜色
- CSS - 背景
- CSS - 字体
- CSS - 文本
- CSS - 图片
- CSS - 链接
- CSS - 表格
- CSS - 边框
- CSS - 块级边框
- CSS - 内联边框
- CSS - 外边距
- CSS - 列表
- CSS - 内边距
- CSS - 光标
- CSS - 轮廓
- CSS - 尺寸
- CSS - 滚动条
- CSS - 块内元素
- CSS - 下拉菜单
- CSS - 可见性
- CSS - 溢出
- CSS - 清除浮动
- CSS - 浮动
- CSS - 箭头
- CSS - 调整大小
- CSS - 引号
- CSS - 顺序
- CSS - 位置
- CSS - 连字符
- CSS - 悬停
- CSS - 显示
- CSS - 聚焦
- CSS - 缩放
- CSS - 位移
- CSS - 高度
- CSS - 连字符字符
- CSS - 宽度
- CSS - 不透明度
- CSS - Z-index
- CSS - 底部
- CSS - 导航栏
- CSS - 叠加层
- CSS - 表单
- CSS - 对齐
- CSS - 图标
- CSS - 图片库
- CSS - 注释
- CSS - 加载器
- CSS - 属性选择器
- CSS - 组合器
- CSS - 根元素
- CSS - 盒模型
- CSS - 计数器
- CSS - 剪裁
- CSS - 书写模式
- CSS - Unicode-bidi
- CSS - min-content
- CSS - all
- CSS - inset
- CSS - isolation
- CSS - overscroll
- CSS - justify-items
- CSS - justify-self
- CSS - tab-size
- CSS - pointer-events
- CSS - place-content
- CSS - place-items
- CSS - place-self
- CSS - max-block-size
- CSS - min-block-size
- CSS - mix-blend-mode
- CSS - max-inline-size
- CSS - min-inline-size
- CSS - offset
- CSS - accent-color
- CSS - user-select
- CSS 高级
- CSS - 网格
- CSS - 网格布局
- CSS - Flexbox
- CSS - 可见性
- CSS - 定位
- CSS - 图层
- CSS - 伪类
- CSS - 伪元素
- CSS - @规则
- CSS - 文本效果
- CSS - 分页媒体
- CSS - 打印
- CSS - 布局
- CSS - 验证
- CSS - 图片精灵
- CSS - !important
- CSS - 数据类型
- CSS3 教程
- CSS3 - 教程
- CSS - 圆角
- CSS - 边框图片
- CSS - 多重背景
- CSS - 颜色
- CSS - 渐变
- CSS - 盒阴影
- CSS - box-decoration-break
- CSS - caret-color
- CSS - 文本阴影
- CSS - 文本
- CSS - 2D 变换
- CSS - 3D 变换
- CSS - 过渡
- CSS - 动画
- CSS - 多列
- CSS - box-sizing
- CSS - 工具提示
- CSS - 按钮
- CSS - 分页
- CSS - 变量
- CSS - 媒体查询
- CSS - 函数
- CSS - 数学函数
- CSS - 遮罩
- CSS - 形状
- CSS - 样式图片
- CSS - 特效性
- CSS - 自定义属性
- CSS 响应式
- CSS RWD - 简介
- CSS RWD - 视口
- CSS RWD - 网格视图
- CSS RWD - 媒体查询
- CSS RWD - 图片
- CSS RWD - 视频
- CSS RWD - 框架
- CSS 工具
- CSS - PX 到 EM 转换器
- CSS - 颜色选择器和动画
- CSS 资源
- CSS - 有用资源
- CSS - 讨论
CSS filter 属性
CSS filter 属性应用图形效果,例如模糊、亮度、对比度等等,到元素上。它允许我们操作元素的显示方式,而不会改变其底层内容或结构。
语法
filter: none | blur() | brightness() | contrast() | drop-shadow() | grayscale() | hue-rotate() | invert() | opacity() | saturate() | sepia() | url();
属性值
| 值 | 描述 |
|---|---|
| none | 指定无效果。默认值。 |
| blur(px) | 应用图像模糊效果。值越大,模糊效果越明显。默认为 0。 |
| brightness(%) | 调整图像亮度。0% 会导致图像变黑,100% 为默认值,显示原始图像。 |
| contrast(%) | 调整图像对比度。0% 会导致图像变黑,100% 为默认值,显示原始图像。 |
| drop-shadow(offset-x, offset-y, blur-radius, color) | 为元素应用投影效果。
|
| grayscale(%) | 将图像转换为灰度图像。0% 为默认值,表示原始图像;100% 为灰度图像。不允许使用负值。 |
| hue-rotate(deg) | 应用色相旋转。指定角度会根据颜色圆上的角度调整图像。0deg 为默认值,表示原始图像;360deg 为最大值。 |
| invert(%) | 反转图像。0% 为默认值,表示原始图像;100% 会导致图像完全反转。不允许使用负值。 |
| opacity(%) | 控制图像的透明度。0% 表示完全透明的图像;100% 为默认值,表示原始图像(不透明)。不允许使用负值。 |
| saturate(%) | 饱和图像。0% 表示不饱和的图像;100% 为默认值,表示原始图像。不允许使用负值。 |
| sepia(%) | 将图像转换为棕褐色。0% 为默认值,表示原始图像;100% 为棕褐色图像。不允许使用负值。 |
| url() | 获取指定 SVG 滤镜的 XML 文件位置,可能包含指向特定滤镜元素的锚点。例如:filter: url(svg-url#element-id)。 |
| initial | 将属性设置为其默认值。 |
| inherit | 从父元素继承属性。 |
CSS filter 属性示例
以下示例解释了具有不同值的filter 属性。
使用 none 函数的 filter 属性
为了不对元素产生任何影响,我们使用none 值。以下示例显示了这一点。
示例
<!DOCTYPE html>
<html>
<head>
<style>
#filter{
filter: none;
}
</style>
</head>
<body>
<h2>
CSS filter property
</h2>
<h4>
filter: none
</h4>
<img src="/css/images/content.png" alt="image"
width="250" height="250" id="filter"/>
</body>
</html>
使用 blur 函数的 filter 属性
为了在元素中引入模糊效果,我们使用blur() 函数。我们以像素为单位指定模糊值。值越大,模糊效果越明显。以下示例显示了这一点。
示例
<!DOCTYPE html>
<html>
<head>
<style>
#filter{
filter: blur(5px);
}
</style>
</head>
<body>
<h2>
CSS filter property
</h2>
<h4>
filter: blur(5px)
</h4>
<img src="/css/images/content.png" alt="image"
width="250" height="250" id="filter"/>
<h4>
Image used:
</h4>
<img src="/css/images/content.png" alt="image"
width="200" height="200"/>
</body>
</html>
使用 brightness 函数的 filter 属性
为了调整元素的亮度,我们使用brightness() 函数。我们以百分比为单位指定值。以下示例显示了这一点。
示例
<!DOCTYPE html>
<html>
<head>
<style>
div {
display: grid;
}
#filter1 {
filter: brightness(30%);
}
#filter2 {
filter: brightness(60%);
}
</style>
</head>
<body>
<h2>
CSS filter property
</h2>
<div>
<h4>
filter: brightness(30%)
</h4>
<img src="/css/images/content.png" alt="image"
width="250" height="250" id="filter1" />
<h4>
filter: brightness(60%)
</h4>
<img src="/css/images/content.png" alt="image"
width="250" height="250" id="filter2" />
</div>
<h4>
Image used:
</h4>
<img src="/css/images/content.png" alt="image"
width="200" height="200" />
</body>
</html>
使用 contrast 函数的 filter 属性
为了调整元素的暗度,我们使用contrast 函数。我们以百分比为单位指定值。以下示例显示了这一点。
示例
<!DOCTYPE html>
<html>
<head>
<style>
div {
display: grid;
}
#filter1 {
filter: contrast(20%);
}
#filter2 {
filter: contrast(55%);
}
</style>
</head>
<body>
<h2>
CSS filter property
</h2>
<div>
<h4>
filter: contrast(20%)
</h4>
<img src="/css/images/content.png" alt="image"
width="250" height="250" id="filter1" />
<h4>
filter: contrast(55%)
</h4>
<img src="/css/images/content.png" alt="image"
width="250" height="250" id="filter2" />
</div>
<h4>
Image used:
</h4>
<img src="/css/images/content.png" alt="image"
width="200" height="200" />
</body>
</html>
使用 drop-shadow 函数的 filter 属性
为了向元素添加背景阴影,我们使用drop-shadow() 函数。我们以像素和颜色格式指定值。它最多可接受 4 个值。第一个值是 x 偏移量,第二个值是 y 偏移量,第三个是模糊半径,第四个是阴影的颜色。以下示例显示了这一点。
示例
<!DOCTYPE html>
<html>
<head>
<style>
div {
display: grid;
gap: 10px;
}
#filter1 {
filter: drop-shadow(15px 20px 15px grey);
}
#filter2 {
filter: drop-shadow(40px 40px 40px red);
}
</style>
</head>
<body>
<h2>
CSS filter property
</h2>
<div>
<h4>
filter: drop-shadow (15px 20px 15px grey)
</h4>
<img src="/css/images/content.png" alt="image"
width="250" height="250" id="filter1" />
<h4>
filter: drop-shadow (40px 40px 40px red)
</h4>
<img src="/css/images/content.png" alt="image"
width="250" height="250" id="filter2" />
</div>
</body>
</html>
使用 grayscale 函数的 filter 属性
为了突出元素中的黑白特征,我们使用grayscale() 函数。我们以百分比为单位指定值。以下示例显示了这一点。
示例
<!DOCTYPE html>
<html>
<head>
<style>
div {
display: grid;
}
#filter1 {
filter: grayscale(65%);
}
#filter2 {
filter: grayscale(100%);
}
</style>
</head>
<body>
<h2>
CSS filter property
</h2>
<div>
<h4>
filter: grayscale(45%)
</h4>
<img src="/css/images/content.png" alt="image"
width="250" height="250" id="filter1" />
<h4>
filter: grayscale(100%)
</h4>
<img src="/css/images/content.png" alt="image"
width="250" height="250" id="filter2" />
</div>
<h4>
Image used:
</h4>
<img src="/css/images/content.png" alt="image"
width="200" height="200" />
</body>
</html>
使用 hue-rotate 函数的 filter 属性
为了通过旋转色相角来调整元素颜色的色相,我们使用hue-rotate() 函数。我们以度为单位指定值。度值指定围绕色轮的旋转角度,这会创建各种颜色效果和变换。以下示例显示了这一点。
示例
<!DOCTYPE html>
<html>
<head>
<style>
div {
display: grid;
}
#filter1 {
filter: hue-rotate(75deg);
}
#filter2 {
filter: hue-rotate(35deg);
}
</style>
</head>
<body>
<h2>
CSS filter property
</h2>
<div>
<h4>
filter: hue-rotate(75deg)
</h4>
<img src="/css/images/content.png" alt="image" width="250" height="250" id="filter1" />
<h4>
filter: hue-rotate(35deg)
</h4>
<img src="/css/images/content.png" alt="image" width="250" height="250" id="filter2" />
</div>
<h4>
Image used:
</h4>
<img src="/css/images/content.png" alt="image" width="200" height="200" />
</body>
</html>
使用 opacity 函数的 filter 属性
为了调整元素的透明度,我们使用opacity() 函数。我们以百分比为单位指定值。以下示例显示了这一点。
示例
<!DOCTYPE html>
<html>
<head>
<style>
div {
display: grid;
}
#filter1 {
filter: opacity(35%);
}
#filter2 {
filter: opacity(60%);
}
</style>
</head>
<body>
<h2>
CSS filter property
</h2>
<div>
<h4>
filter: opacity(35%)
</h4>
<img src="/css/images/content.png" alt="image"
width="250" height="250" id="filter1" />
<h4>
filter: opacity(60%)
</h4>
<img src="/css/images/content.png" alt="image"
width="250" height="250" id="filter2" />
</div>
<h4>
Image used:
</h4>
<img src="/css/images/content.png" alt="image"
width="200" height="200" />
</body>
</html>
使用 saturate 函数的 filter 属性
为了调整元素的颜色饱和度,我们使用saturate() 函数。我们以百分比为单位指定值。以下示例显示了这一点。
示例
<!DOCTYPE html>
<html>
<head>
<style>
div {
display: grid;
}
#filter1 {
filter: saturate(25%);
}
#filter2 {
filter: saturate(65%);
}
</style>
</head>
<body>
<h2>
CSS filter property
</h2>
<div>
<h4>
filter: saturate(25%)
</h4>
<img src="/css/images/content.png" alt="image"
width="250" height="250" id="filter1" />
<h4>
filter: saturate(65%
)</h4>
<img src="/css/images/content.png" alt="image"
width="250" height="250" id="filter2" />
</div>
<h4>
Image used:
</h4>
<img src="/css/images/content.png" alt="image"
width="200" height="200" />
</body>
</html>
使用 sepia 函数的 filter 属性
为了向元素应用棕褐色调,使其呈现类似于旧照片的温暖的棕色外观,我们使用sepia() 函数。我们以百分比为单位指定值。以下示例显示了这一点。
示例
<!DOCTYPE html>
<html>
<head>
<style>
div {
display: grid;
}
#filter1 {
filter: sepia(45%);
}
#filter2 {
filter: sepia(90%);
}
</style>
</head>
<body>
<h2>
CSS filter property
</h2>
<div>
<h4>
filter: sepia(45%)
</h4>
<img src="/css/images/content.png" alt="image"
width="250" height="250" id="filter1" />
<h4>
filter: sepia(90%)
</h4>
<img src="/css/images/content.png" alt="image"
width="250" height="250" id="filter2" />
</div>
<h4>
Image used:
</h4>
<img src="/css/images/content.png" alt="image"
width="200" height="200" />
</body>
</html>
使用 filter URL 的 filter 属性
为了将外部 SVG 滤镜应用于元素,以便我们可以使用在 SVG 文件中定义的自定义滤镜效果(可以包含诸如颜色调整、模糊和失真之类的复杂效果),我们必须在url() 中指定滤镜的 id。以下示例显示了这一点。
示例
<!DOCTYPE html>
<html>
<head>
<style>
.myelement {
filter: url(#pink-flower);
}
div {
display: grid;
gap: 10px;
max-width: 300px;
}
img {
width: 100%;
max-height: 200px;
object-fit: cover;
}
svg {
height: 0;
}
</style>
</head>
<body>
<svg xmlns="http://www.w3.org/2000/svg">
<defs>
<filter id="pink-flower" x="0" y="0"
width="100%" height="100%">
<feComponentTransfer>
<feFuncR type="linear" slope="1" intercept="0.5">
</feFuncR>
<feFuncG type="linear" slope="0.5" intercept="0.5">
</feFuncG>
<feFuncB type="linear" slope="0" intercept="1">
</feFuncB>
</feComponentTransfer>
</filter>
</defs>
</svg>
<h2>
CSS filter property
</h2>
<h3>
filter: url() function
</h3>
<div>
<img src="/css/images/content.png"
alt="pink flower" />
<img src="/css/images/content.png"
alt="blurred pink flower" class="myelement" />
</div>
</body>
</html>
支持的浏览器
| 属性 | ![]() |
![]() |
![]() |
![]() |
![]() |
|---|---|---|---|---|---|
| filter | 53.0 | 13.0 | 35.0 | 9.1 | 40.0 |




