search
HomeWeb Front-endHTML TutorialCSS秘密花园:不规则的阴影_html/css_WEB-ITnose

《 CSS Secrets 》是 @Lea Verou 最新著作,这本书讲解了有关于CSS中一些小秘密。是一本CSSer值得一读的一本书,经过一段时间的阅读,我、@南北和@彦子一起将在W3cplus发布一系列相关的读后感,与大家一起分享。

问题

当我们想要为矩形或任何可以用 border-radius 创建的形状应用投影的时候, box-shadow 是非常棒的。但是,当我们有伪元素或其他半透明的样式的时候,它可能就不是那么好用了,因为 box-shadow 会耍赖忽略透明度。例子如下:

  • 半透明图像、背景图像、或 border-image (如,老式的金色相框)。
  • Dotted边框、dashed边框、或没有背景的半透明边框(或 background-clip 不是 border-box 时)
  • 对话框气泡,如果它们指向Speaker的小角是通过伪元素创建的。
  • 像我们在“Cutout corners” secret 96页看到的切口角。
  • 大多数折叠角效果,包括本章内容后面会提到的一个内容。
  • 通过 clip-path 创建的图形,像“Diamond images” secret 90页的钻石图像。

带有CSS样式但 box-shadow 失效的元素,应用的 box-shadow 的值是 2px 2px 10px rgba(0,0,0,.5)

对应用 box-shadow 的尝试失败的结果可以在上图中看到。那是否有适用这种情况的解决方案呢,还是说这里我们只能完全give up阴影效果了?

解决方案

滤镜效果规范 提供了一个针对这个问题的解决方案,通过一个新的 filter 属性,从SVG借来的。但是,尽管CSS滤镜基本上是从SVG滤镜而来,但是它们不需要任何SVG的知识。相反,它们通过一些便利的功能函数指定即可,如 blur() 、 grayscale() 、还有 drop-shadow() !只要你想要,你甚至可以链式应用多个滤镜,用空格把它们分开,如下:

filter: blur() grayscale() drop-shadow();

drop-shadow() 滤镜接受和基本 box-shadow 相同的参数,但是没有扩散半径,没有 inset 关键字,不能用逗号分隔多个不同的阴影。比如说,与其这样写:

box-shadow: 2px 2px 10px rgba(0,0,0,.5);

我们可以这样写:

filter: drop-shadow(2px 2px 10px rgba(0,0,0,.5));

你可以在下图中看到 drop-shadow() 滤镜应用于文章开头图片中元素的效果。

这里使用的可能是不同的模糊算法,所以你可能需要调整模糊的值。

CSS滤镜最好的一点就是它们可以优雅降级:当遇到不支持它们的浏览器时,就不会应用该滤镜效果。你可以通过适用滤镜组合来获得好一点的浏览器支持,如果你真的需要在尽可能多的浏览器中展示这个效果。你可以在 滤镜效果规范 中,对每个滤镜功能都找到对应的SVG滤镜。你可以一起使用SVG滤镜和简化的CSS,让级联去采用最好的那一个:

filter: url(drop-shadow.svg#drop-shadow);filter: drop-shadow(2px 2px 10px rgba(0,0,0,.5));

可惜的是,如果SVG滤镜是一个单独的文件,在你的CSS代码中,这不是一个定制得很好的、人性化的功能;但是如果是内联SVG,它就是会让代码变得混乱。文件中的参数是固定的,如果我们只是想要一个稍微有点不一样的阴影,引入多个文件是不切实际的。我们可以使用data URIs(这也节省了额外的HTTP请求),但仍然会让文件变得更大。因为这是一个fallback,使用一两个变形是OK的,即使是为了稍微有一点不同的 dropshadow() 滤镜。

另一个需要考虑的问题是每个非透明区域都会无差别地蒙上一层阴影,包括文本(如果背景是透明的),如下图所示:

你可能会认为可以通过使用 text-shadow: none; 把文字阴影去掉,但是 text-shadow 是完全独立的,不能去掉文本上 drop-shadow() 滤镜的效果。另外,如果你使用 text-shadow 来完成真正的文本阴影,这个阴影也会被 drop-shadow() 滤镜再次添加阴影,也就是建立投影的投影!看一下下面示例的CSS代码(还有完成的一点都不好看的效果——它试图从各个方面来证明这个事情):

color: deeppink;border: 2px solid;text-shadow: .1em .2em yellow;filter: drop-shadow(.05em .05em .1em gray);

你可以在下图中看到效果,这就是 text-shadow 和 drop-shadow() 一起作用的效果:

text-shadows 也会通过 drop-shadow() 滤镜来产生投影

Statement
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
The Future of HTML, CSS, and JavaScript: Web Development TrendsThe Future of HTML, CSS, and JavaScript: Web Development TrendsApr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

HTML: The Structure, CSS: The Style, JavaScript: The BehaviorHTML: The Structure, CSS: The Style, JavaScript: The BehaviorApr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML: Evolution and Trends in Web DesignThe Future of HTML: Evolution and Trends in Web DesignApr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML vs. CSS vs. JavaScript: A Comparative OverviewHTML vs. CSS vs. JavaScript: A Comparative OverviewApr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML: Is It a Programming Language or Something Else?HTML: Is It a Programming Language or Something Else?Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML: Building the Structure of Web PagesHTML: Building the Structure of Web PagesApr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

From Text to Websites: The Power of HTMLFrom Text to Websites: The Power of HTMLApr 13, 2025 am 12:07 AM

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

Understanding HTML, CSS, and JavaScript: A Beginner's GuideUnderstanding HTML, CSS, and JavaScript: A Beginner's GuideApr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.