Skip to Content
CSS:权威指南,第 5 版
book

CSS:权威指南,第 5 版

by Eric Meyer, Estelle Weyl
May 2025
Intermediate to advanced
1128 pages
15h 21m
Chinese
O'Reilly Media, Inc.
Content preview from CSS:权威指南,第 5 版

第 20 章 滤镜 滤镜、混合、剪切和遮罩

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

有几种特殊属性允许作者使用可视化过滤器改变元素的外观,指定不同的方法将元素与它们后面的东西进行可视化混合,以及通过显示部分和隐藏其他部分来改变元素的表现形式。 虽然这些概念看似互不相关,但它们都有一个共同点:允许以以前难以实现或不可能实现的方式改变元素。

CSS 过滤器

CSS提供了一种方法,通过filter 属性,将内置视觉滤镜效果以及页面或外部文件中定义的自定义滤镜应用到元素中。

值语法允许使用空格分隔的过滤函数列表,每个过滤函数依次应用。因此,在声明filter: opacity(0.5) blur(1px); 的情况下,不透明度会被应用到元素上,然后半透明的结果会被模糊化。 如果顺序颠倒,应用顺序也会颠倒:完全不透明的元素被模糊,模糊后的结果变成半透明。

CSS规范在讨论filter 时谈到了 "输入图像",但这并不意味着filter 只能用于图像。 任何 HTML 元素都可以被过滤,所有图形 SVG 元素都可以被过滤。 输入图像是渲染元素过滤的视觉副本。 过滤器应用于该输入,然后将最终过滤结果呈现到显示媒介(如设备显示屏)上。

所有允许使用的值(url() 除外)都是函数值,每个函数允许使用的值类型取决于相关函数。 为便于理解,我们将这些函数分为几大类。

基本过滤器

以下滤镜都是基本滤镜,它们所产生的变化与其名称直接对应--模糊、阴影和不透明度变化:

blur( <length>)

使用高斯模糊法模糊元素的内容,其标准偏差由所提供的 <length> 所提供的值定义,其中0 的值会使元素保持不变。 不允许使用负长度。

opacity( [ <number> | <percentage> ])

对元素应用透明度过滤器,其方式与opacity 属性非常相似,0 的值会产生一个完全透明的元素,而1100% 的值会使元素保持不变。 不允许使用负值。 允许使用大于1100% 的值,但在计算最终值时,会将其剪切为1100%

警告

该规范明确指出,filter: opacity() 并不是 opacity 属性的替代品或速记符号,事实上,两者都可以应用于同一元素,从而产生一种双重透明度。

drop-shadow( <length>{2,3} <color>?)

创建与元素 alpha 通道形状相匹配的 阴影,并使用模糊效果和可选颜色。 长度和颜色的处理方法与属性box-shadow 相同,这意味着前两个 <length> 值可以是负值,但第三个值(定义模糊)不能是负值。 与 box-shadow 不同的是,inset 值是不允许的。要应用多个阴影,请提供多个空格分隔的drop-shadow() 函数;与box-shadow 不同的是,逗号分隔的阴影在这里不起作用。 如果没有提供 <color> 值,则使用的颜色与该元素的 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

学习 Java,第 6 版

学习 Java,第 6 版

Marc Loy, Patrick Niemeyer, Daniel Leuck
学习 React,第二版

学习 React,第二版

Alex Banks, Eve Porcello

Publisher Resources

ISBN: 9798341657021