box-shadow CSS property adds shadow effects around an element's frame. You can set multiple effects separated by commas. A box shadow is described by X and Y offsets relative to the element, blur and spread radius, and color.

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

box-shadow property enables you to cast a drop shadow from the frame of almost any element. If a border-radius is specified on the element with a box shadow, the box shadow takes on the same rounded corners. The z-ordering of multiple box shadows is the same as multiple text shadows (the first specified shadow is on top).

Box-shadow generator is an interactive tool allowing you to generate a box-shadow .

句法

/* Keyword values */
box-shadow: none;
/* offset-x | offset-y | color */
box-shadow: 60px -16px teal;
/* offset-x | offset-y | blur-radius | color */
box-shadow: 10px 5px 5px black;
/* offset-x | offset-y | blur-radius | spread-radius | color */
box-shadow: 2px 2px 2px 1px rgba(0, 0, 0, 0.2);
/* inset | offset-x | offset-y | color */
box-shadow: inset 5em 1em gold;
/* Any number of shadows, separated by commas */
box-shadow: 3px 3px red, -1em 0 0.4em olive;
/* Global keywords */
box-shadow: inherit;
box-shadow: initial;
box-shadow: unset;
				

Specify a single box-shadow using:

To specify multiple shadows, provide a comma-separated list of shadows.

inset
If not specified (default), the shadow is assumed to be a drop shadow (as if the box were raised above the content).
The presence of the inset keyword changes the shadow to one inside the frame (as if the content was depressed inside the box). Inset shadows are drawn inside the border (even transparent ones), above the background, but below content.
<offset-x> <offset-y>
These are two <length> values to set the shadow offset. <offset-x> specifies the horizontal distance. Negative values place the shadow to the left of the element. <offset-y> specifies the vertical distance. Negative values place the shadow above the element. See <length> for possible units.
If both values are 0 , the shadow is placed behind the element (and may generate a blur effect if <blur-radius> and/or <spread-radius> is set).
<blur-radius>
This is a third <length> value. The larger this value, the bigger the blur, so the shadow becomes bigger and lighter. Negative values are not allowed. If not specified, it will be 0 (the shadow's edge is sharp). The specification does not include an exact algorithm for how the blur radius should be calculated, however, it does elaborate as follows:
…for a long, straight shadow edge, this should create a color transition the length of the blur distance that is perpendicular to and centered on the shadow’s edge, and that ranges from the full shadow color at the radius endpoint inside the shadow to fully transparent at the endpoint outside it.
<spread-radius>
This is a fourth <length> value. Positive values will cause the shadow to expand and grow bigger, negative values will cause the shadow to shrink. If not specified, it will be 0 (the shadow will be the same size as the element).
<color>
<color> values for possible keywords and notations.
If not specified, it defaults to currentcolor .

插值

Each shadow in the list (treating none as a 0-length list) is interpolated via the color (as color) component, and x, y, blur, and (when appropriate) spread (as length) components. For each shadow, if both input shadows are or are not inset , then the interpolated shadow must match the input shadows in that regard. If any pair of input shadows has one inset and the other not inset , the entire shadow list is uninterpolable. If the lists of shadows have different lengths, then the shorter list is padded at the end with shadows whose color is transparent , all lengths are 0 , and whose inset (or not) matches the longer list.

形式定义

初始值 none
适用于 所有元素。它还适用于 ::first-letter .
继承 no
计算值 any length made absolute; any specified color computed; otherwise as specified
动画类型 a shadow list

形式句法

none | <shadow>#

where
<shadow> = inset? && <length>{2,4} && <color>?

where
<color> = <rgb()> | <rgba()> | <hsl()> | <hsla()> | <hex-color> | <named-color> | currentcolor | <deprecated-system-color>

where
<rgb()> = rgb( <percentage>{3} [ / <alpha-value> ]? ) | rgb( <number>{3} [ / <alpha-value> ]? ) | rgb( <percentage>#{3} , <alpha-value>? ) | rgb( <number>#{3} , <alpha-value>? )
<rgba()> = rgba( <percentage>{3} [ / <alpha-value> ]? ) | rgba( <number>{3} [ / <alpha-value> ]? ) | rgba( <percentage>#{3} , <alpha-value>? ) | rgba( <number>#{3} , <alpha-value>? )
<hsl()> = hsl( <hue> <percentage> <percentage> [ / <alpha-value> ]? ) | hsl( <hue>, <percentage>, <percentage>, <alpha-value>? )
<hsla()> = hsla( <hue> <percentage> <percentage> [ / <alpha-value> ]? ) | hsla( <hue>, <percentage>, <percentage>, <alpha-value>? )

where
<alpha-value> = <number> | <percentage>
<hue> = <number> | <angle>

范例

Setting three shadows

In this example, we include three shadows: an inset shadow, a regular drop shadow, and a 2px shadow that creates a border effect (we could have used an outline instead for that third shadow).

HTML

<blockquote><q>You may shoot me with your words,<br/>
You may cut me with your eyes,<br/>
You may kill me with your hatefulness,<br/>
But still, like air, I'll rise.</q>
<p>— Maya Angelou</p>
</blockquote>
					

CSS

blockquote {
  padding: 20px;
  box-shadow:
       inset 0 -3em 3em rgba(0,0,0,0.1),
             0 0  0 2px rgb(255,255,255),
             0.3em 0.3em 1em rgba(0,0,0,0.3);
}
					

结果

Setting zero for offset and blur

When the x-offset , y-offset ,和 blur are all zero, the box shadow will be a solid-colored outline of equal-size on all sides. The shadows are drawn back to front, so the first shadow sits on top of subsequent shadows. When the border-radius is set to 0, as is the default, the corners of the shadow will be, well, corners. Had we put in a border-radius of any other value, the corners would have been rounded.

We added a margin the size of the widest box-shadow to ensure the shadow doesn't overlap adjacent elements or go beyond the border of the containing box. A box-shadow does not impact box model dimensions.

HTML

<div><p>Hello World</p></div>
					

CSS

p {
  box-shadow: 0 0 0 2em #F4AAB9,
              0 0 0 4em #66CCFF;
  margin: 4em;
  padding:1em;
}
					

结果

规范

规范 状态 注释
CSS Backgrounds and Borders Module Level 3
The definition of 'box-shadow' in that specification.
候选推荐 初始定义

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
box-shadow Chrome 完整支持 10 注意事项
完整支持 10 注意事项
注意事项 Shadows affect layout in this browser. For example, if you cast an outer shadow to a box with a width of 100% , then you'll see a scrollbar.
完整支持 Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Edge 完整支持 12 Firefox 完整支持 4 注意事项
完整支持 4 注意事项
注意事项 Shadows affect layout in this browser. For example, if you cast an outer shadow to a box with a width of 100% , then you'll see a scrollbar.
不支持 3.5 — 13 Prefixed
Prefixed Implemented with the vendor prefix: -moz-
preference (needs to be set to ). To change preferences in Firefox, visit about:config.
IE 完整支持 9 注意事项
完整支持 9 注意事项
注意事项 要使用 box-shadow in Internet Explorer 9 or later, you must set border-collapse to separate .
注意事项 Since version 5.5, Internet Explorer supports Microsoft's DropShadow and Shadow Filter . You can use this proprietary extension to cast a drop shadow (though the syntax and the effect are different from CSS3)
Opera 完整支持 10.5 注意事项
完整支持 10.5 注意事项
注意事项 Shadows affect layout in this browser. For example, if you cast an outer shadow to a box with a width of 100% , then you'll see a scrollbar.
Safari 完整支持 5.1 注意事项
完整支持 5.1 注意事项
注意事项 Shadows affect layout in this browser. For example, if you cast an outer shadow to a box with a width of 100% , then you'll see a scrollbar.
完整支持 Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
WebView Android 完整支持 ≤37 注意事项
完整支持 ≤37 注意事项
注意事项 Shadows affect layout in this browser. For example, if you cast an outer shadow to a box with a width of 100% , then you'll see a scrollbar.
完整支持 Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Chrome Android 完整支持 18 注意事项
完整支持 18 注意事项
注意事项 Shadows affect layout in this browser. For example, if you cast an outer shadow to a box with a width of 100% , then you'll see a scrollbar.
完整支持 Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Firefox Android 完整支持 4 注意事项
完整支持 4 注意事项
注意事项 Shadows affect layout in this browser. For example, if you cast an outer shadow to a box with a width of 100% , then you'll see a scrollbar.
不支持 4 — 14 Prefixed
Prefixed Implemented with the vendor prefix: -moz-
preference (needs to be set to ). To change preferences in Firefox, visit about:config.
Opera Android 完整支持 14 注意事项
完整支持 14 注意事项
注意事项 Shadows affect layout in this browser. For example, if you cast an outer shadow to a box with a width of 100% , then you'll see a scrollbar.
完整支持 Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Safari iOS 完整支持 5 注意事项
完整支持 5 注意事项
注意事项 Shadows affect layout in this browser. For example, if you cast an outer shadow to a box with a width of 100% , then you'll see a scrollbar.
完整支持 Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Samsung Internet Android 完整支持 1.0 注意事项
完整支持 1.0 注意事项
注意事项 Shadows affect layout in this browser. For example, if you cast an outer shadow to a box with a width of 100% , then you'll see a scrollbar.
完整支持 Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
inset Chrome 完整支持 10 Edge 完整支持 12 Firefox 完整支持 4
完整支持 4
不支持 3.5 — 13 Prefixed
Prefixed Implemented with the vendor prefix: -moz-
IE 部分支持 9 注意事项
部分支持 9 注意事项
注意事项 要使用 box-shadow in Internet Explorer 9 or later, you must set border-collapse to separate .
注意事项 inset must be the last keyword in the declaration.
Opera 完整支持 10.5 Safari 完整支持 5.1 WebView Android 完整支持 ≤37 Chrome Android 完整支持 18 Firefox Android 完整支持 4
完整支持 4
不支持 4 — 14 Prefixed
Prefixed Implemented with the vendor prefix: -moz-
Opera Android 完整支持 14 Safari iOS 完整支持 5 Samsung Internet Android 完整支持 1.0
Multiple shadows Chrome 完整支持 10 Edge 完整支持 12 Firefox 完整支持 4
完整支持 4
不支持 3.5 — 13 Prefixed
Prefixed Implemented with the vendor prefix: -moz-
IE 完整支持 9 注意事项
完整支持 9 注意事项
注意事项 要使用 box-shadow in Internet Explorer 9 or later, you must set border-collapse to separate .
Opera 完整支持 10.5 Safari 完整支持 5.1 WebView Android 完整支持 ≤37 Chrome Android 完整支持 18 Firefox Android 完整支持 4
完整支持 4
不支持 4 — 14 Prefixed
Prefixed Implemented with the vendor prefix: -moz-
Opera Android 完整支持 14 Safari iOS 完整支持 5 Samsung Internet Android 完整支持 1.0
Spread radius Chrome 完整支持 10 Edge 完整支持 12 Firefox 完整支持 4
完整支持 4
不支持 3.5 — 13 Prefixed
Prefixed Implemented with the vendor prefix: -moz-
IE 完整支持 9 注意事项
完整支持 9 注意事项
注意事项 要使用 box-shadow in Internet Explorer 9 or later, you must set border-collapse to separate .
Opera 完整支持 10.5 Safari 完整支持 5.1 WebView Android 完整支持 ≤37 Chrome Android 完整支持 18 Firefox Android 完整支持 4
完整支持 4
不支持 4 — 14 Prefixed
Prefixed Implemented with the vendor prefix: -moz-
Opera Android 完整支持 14 Safari iOS 完整支持 5 Samsung Internet Android 完整支持 1.0

图例

完整支持

完整支持

部分支持

部分支持

见实现注意事项。

见实现注意事项。

用户必须明确启用此特征。

用户必须明确启用此特征。

要求使用供应商前缀或不同名称。

要求使用供应商前缀或不同名称。

另请参阅

元数据

  • 最后修改: