CSS Handbook · 颜色与视觉效果

第七章 / 7.3 阴影与滤镜

第七章 · 7.3 节

阴影与滤镜


阴影负责塑造元素的"高度感",滤镜负责调整元素的视觉质感——本教程页面本身那道让纸张"悬浮"起来的两侧阴影,正是 5.1 节讲的颜色透明度与本节 box-shadow 的结合运用。

box-shadow 参数详解

.card {
  box-shadow: 0 4px 12px -2px rgba(0,0,0,.25);
  /*          ↑x  ↑y  ↑blur ↑spread ↑color */
}
参数作用
offset-x水平偏移,正值向右
offset-y垂直偏移,正值向下
blur-radius模糊半径,越大边缘越柔和,不能为负
spread-radius扩散半径,可选;正值放大阴影,负值收缩
color阴影颜色,通常搭配透明度使用
不同参数对比
轻
重
描边式
技巧用 box-shadow: 0 0 0 Npx color(偏移与模糊均为 0)可以模拟一条不占布局空间的描边,效果类似 outline,但支持 border-radius 圆角贴合。

内阴影与多层阴影

加上 inset 关键字,阴影会画在盒子内部,常用于表现"凹陷"质感的输入框;box-shadow 也支持用逗号叠加多层,从而同时表现"贴近的清晰阴影"与"远处的柔和阴影",观感更接近真实光照。

input:focus {
  box-shadow: inset 0 1px 3px rgba(0,0,0,.15);
}

.elevated {
  box-shadow:
    0 1px 2px rgba(0,0,0,.12),
    0 8px 24px rgba(0,0,0,.16);
}

text-shadow

参数与 box-shadow 类似但不支持 spread 与 inset,只作用于文字轮廓:

.headline {
  text-shadow: 0 2px 4px rgba(0,0,0,.3);
}

filter 滤镜

filter 提供了一组图像处理函数,可以直接作用于元素(含其子元素与背景),无需切到图像编辑软件预处理:

函数作用
blur(8px)高斯模糊
brightness(1.2)亮度,1 为原始亮度
grayscale(1)灰度化,1 为完全灰度
drop-shadow(0 4px 6px rgba(0,0,0,.3))阴影,可贴合透明 PNG 的不规则轮廓
.thumb { filter: grayscale(1); transition: filter .2s; }
.thumb:hover { filter: grayscale(0); }

filter: drop-shadow() 与 box-shadow 的区别:后者贴合的是元素的矩形盒子边界,前者贴合的是元素内容(含透明区域)实际的可见轮廓,更适合给不规则形状的图标或图片添加阴影。

backdrop-filter

与 filter 处理元素自身不同,backdrop-filter 处理的是元素背后已经渲染好的内容,是"毛玻璃"效果的标准实现方式:

.glass-nav {
  background: rgba(244,245,240,.6);
  backdrop-filter: blur(12px) saturate(1.4);
}
性能提示backdrop-filter 的模糊计算成本较高,避免在同一页面大范围、多层叠加使用,滚动场景下尤其要留意帧率。

Chapter 7 · Section 7.3

Shadows and Filters


Shadows shape an element's sense of "height", while filters adjust its visual texture — the side shadows that make the paper "float" on this tutorial page itself are a combination of the color transparency from Section 5.1 and the box-shadow covered here.

box-shadow Parameters in Detail

.card {
  box-shadow: 0 4px 12px -2px rgba(0,0,0,.25);
  /*          ↑x  ↑y  ↑blur ↑spread ↑color */
}
ParameterEffect
offset-xHorizontal offset; positive values move right
offset-yVertical offset; positive values move down
blur-radiusBlur radius; larger values soften the edge; cannot be negative
spread-radiusSpread radius; optional; positive values enlarge the shadow, negative values shrink it
colorShadow color; usually combined with transparency
Comparing different parameters
Light
Heavy
Outline-style
TipUse box-shadow: 0 0 0 Npx color (zero offset and blur) to simulate an outline that takes up no layout space — it behaves like outline but follows border-radius rounded corners.

Inset Shadows and Layered Shadows

Adding the inset keyword draws the shadow inside the box, commonly used to give input fields a "recessed" feel; box-shadow also supports layering multiple shadows with commas, so you can show a crisp close shadow and a soft distant shadow at the same time, looking closer to real lighting.

input:focus {
  box-shadow: inset 0 1px 3px rgba(0,0,0,.15);
}

.elevated {
  box-shadow:
    0 1px 2px rgba(0,0,0,.12),
    0 8px 24px rgba(0,0,0,.16);
}

text-shadow

The parameters are similar to box-shadow but spread and inset are not supported, and it only affects the outline of the text:

.headline {
  text-shadow: 0 2px 4px rgba(0,0,0,.3);
}

filter

filter provides a set of image-processing functions that can be applied directly to an element (including its children and background), without switching to image-editing software for preprocessing:

FunctionEffect
blur(8px)Gaussian blur
brightness(1.2)Brightness; 1 is the original brightness
grayscale(1)Grayscale; 1 is fully grayscale
drop-shadow(0 4px 6px rgba(0,0,0,.3))Shadow that follows the irregular outline of a transparent PNG
.thumb { filter: grayscale(1); transition: filter .2s; }
.thumb:hover { filter: grayscale(0); }

The difference between filter: drop-shadow() and box-shadow: the latter follows the element's rectangular box border, while the former follows the actual visible outline of the element's content (including transparent areas) — better suited for adding shadows to irregularly shaped icons or images.

backdrop-filter

Unlike filter, which processes the element itself, backdrop-filter processes the content already rendered behind the element — the standard way to achieve the "frosted glass" effect:

.glass-nav {
  background: rgba(244,245,240,.6);
  backdrop-filter: blur(12px) saturate(1.4);
}
Performance notebackdrop-filter's blur computation is expensive; avoid using it over large areas or stacking multiple layers on the same page, and watch the frame rate especially when scrolling.