box-shadow:<length> <length> <length> <length> || <color>
取值:
- <length> <length> <length>? <length>? || <color>:
- 阴影水平偏移值(可取正负值);阴影垂直偏移值(可取正负值);阴影模糊值;阴影颜色
说明:
设置块阴影
box-shadow这个属性应用的非常普遍,可以使你的元素立刻变得漂亮起来,只是记得不要把值设得太离谱。
box-shadow的四个参数:
x-offset x轴偏移
y-offset y轴偏移
blur 模糊值
color of shadow 阴影颜色
下面来看一个例子,代码如下:
1 |
<div><span style="COLOR: #0000ff"><!</span><span style="COLOR: #ff00ff">DOCTYPE html</span><span style="COLOR: #0000ff">></span><br /><span style="COLOR: #0000ff"><</span><span style="COLOR: #800000">html</span><span style="COLOR: #0000ff">></span><br /><span style="COLOR: #0000ff"><</span><span style="COLOR: #800000">head</span><span style="COLOR: #0000ff">></span><br /><span style="COLOR: #0000ff"><</span><span style="COLOR: #800000">title</span><span style="COLOR: #0000ff">>盒子阴影</</span><span style="COLOR: #800000">title</span><span style="COLOR: #0000ff">></span><br /><span style="COLOR: #0000ff"><</span><span style="COLOR: #800000">meta </span><span style="COLOR: #ff0000">charset</span><span style="COLOR: #0000ff">="utf-8"</span><span style="COLOR: #ff0000"> </span><span style="COLOR: #0000ff">/></span><br /><span style="COLOR: #0000ff"><</span><span style="COLOR: #800000">style</span><span style="COLOR: #0000ff">></span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #800000"><br />.box </span><span style="background-color: rgb(245, 245, 245); ">{</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #ff0000"><br /> width</span><span style="background-color: rgb(245, 245, 245); ">:</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #0000ff">300px</span><span style="background-color: rgb(245, 245, 245); ">;</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #ff0000"><br /> height</span><span style="background-color: rgb(245, 245, 245); ">:</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #0000ff">300px</span><span style="background-color: rgb(245, 245, 245); ">;</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #ff0000"><br /> background-color</span><span style="background-color: rgb(245, 245, 245); ">:</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #0000ff">#fff</span><span style="background-color: rgb(245, 245, 245); ">;</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #ff0000"><br /> <br /> </span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #008000">/*</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #008000"> 设置阴影 </span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #008000">*/</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #ff0000"><br /> -webkit-box-shadow</span><span style="background-color: rgb(245, 245, 245); ">:</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #0000ff">1px 1px 3px #292929</span><span style="background-color: rgb(245, 245, 245); ">;</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #ff0000"><br /> -moz-box-shadow</span><span style="background-color: rgb(245, 245, 245); ">:</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #0000ff">1px 1px 3px #292929</span><span style="background-color: rgb(245, 245, 245); ">;</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #ff0000"><br /> box-shadow</span><span style="background-color: rgb(245, 245, 245); ">:</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #0000ff">1px 1px 3px #292929</span><span style="background-color: rgb(245, 245, 245); ">;</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #ff0000"><br /></span><span style="background-color: rgb(245, 245, 245); ">}</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #800000"><br /></span><span style="COLOR: #0000ff"></</span><span style="COLOR: #800000">style</span><span style="COLOR: #0000ff">></span><br /><span style="COLOR: #0000ff"></</span><span style="COLOR: #800000">head</span><span style="COLOR: #0000ff">></span><br /><span style="COLOR: #0000ff"><</span><span style="COLOR: #800000">body</span><span style="COLOR: #0000ff">></span><br /><span style="COLOR: #0000ff"><</span><span style="COLOR: #800000">div </span><span style="COLOR: #ff0000">class</span><span style="COLOR: #0000ff">="box"</span><span style="COLOR: #0000ff">></span><br /><span style="COLOR: #0000ff"><</span><span style="COLOR: #800000">br </span><span style="COLOR: #0000ff">/><</span><span style="COLOR: #800000">br </span><span style="COLOR: #0000ff">/><</span><span style="COLOR: #800000">br </span><span style="COLOR: #0000ff">/><</span><span style="COLOR: #800000">br </span><span style="COLOR: #0000ff">/></span><br />看,是不是很有立体感?没有设置边框啊。<br /><span style="COLOR: #0000ff"></</span><span style="COLOR: #800000">div</span><span style="COLOR: #0000ff">></span><br /><span style="COLOR: #0000ff"></</span><span style="COLOR: #800000">body</span><span style="COLOR: #0000ff">></span><br /><span style="COLOR: #0000ff"></</span><span style="COLOR: #800000">html</span><span style="COLOR: #0000ff">></span></div> |
运行效果如下(Chrome中):
还可以一次运用多个box-shadows,这样做会产生一些有趣的效果,看下面这个例子:
1 |
<div><span style="COLOR: #0000ff"><!</span><span style="COLOR: #ff00ff">DOCTYPE html</span><span style="COLOR: #0000ff">></span><br /><span style="COLOR: #0000ff"><</span><span style="COLOR: #800000">html</span><span style="COLOR: #0000ff">></span><br /><span style="COLOR: #0000ff"><</span><span style="COLOR: #800000">head</span><span style="COLOR: #0000ff">></span><br /><span style="COLOR: #0000ff"><</span><span style="COLOR: #800000">title</span><span style="COLOR: #0000ff">></</span><span style="COLOR: #800000">title</span><span style="COLOR: #0000ff">></span><br /><span style="COLOR: #0000ff"><</span><span style="COLOR: #800000">meta </span><span style="COLOR: #ff0000">charset</span><span style="COLOR: #0000ff">="utf-8"</span><span style="COLOR: #ff0000"> </span><span style="COLOR: #0000ff">/></span><br /><span style="COLOR: #0000ff"><</span><span style="COLOR: #800000">style</span><span style="COLOR: #0000ff">></span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #800000"><br />.box </span><span style="background-color: rgb(245, 245, 245); ">{</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #ff0000"><br /> width</span><span style="background-color: rgb(245, 245, 245); ">:</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #0000ff">300px</span><span style="background-color: rgb(245, 245, 245); ">;</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #ff0000"><br /> height</span><span style="background-color: rgb(245, 245, 245); ">:</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #0000ff">300px</span><span style="background-color: rgb(245, 245, 245); ">;</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #ff0000"><br /> background-color</span><span style="background-color: rgb(245, 245, 245); ">:</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #0000ff">#fff</span><span style="background-color: rgb(245, 245, 245); ">;</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #ff0000"><br /> <br /> </span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #008000">/*</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #008000"> 设置阴影 </span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #008000">*/</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #ff0000"><br /> -webkit-box-shadow</span><span style="background-color: rgb(245, 245, 245); ">:</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #0000ff">1px 1px 3px green, -1px -1px 3px blue</span><span style="background-color: rgb(245, 245, 245); ">;</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #ff0000"><br /> -moz-box-shadow</span><span style="background-color: rgb(245, 245, 245); ">:</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #0000ff">1px 1px 3px green, -1px -1px 3px blue</span><span style="background-color: rgb(245, 245, 245); ">;</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #ff0000"><br /> box-shadow</span><span style="background-color: rgb(245, 245, 245); ">:</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #0000ff">1px 1px 3px green, -1px -1px 3px blue</span><span style="background-color: rgb(245, 245, 245); ">;</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #ff0000"><br /></span><span style="background-color: rgb(245, 245, 245); ">}</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #800000"><br /></span><span style="COLOR: #0000ff"></</span><span style="COLOR: #800000">style</span><span style="COLOR: #0000ff">></span><br /><span style="COLOR: #0000ff"></</span><span style="COLOR: #800000">head</span><span style="COLOR: #0000ff">></span><br /><span style="COLOR: #0000ff"><</span><span style="COLOR: #800000">body</span><span style="COLOR: #0000ff">></span><br /><span style="COLOR: #0000ff"><</span><span style="COLOR: #800000">div </span><span style="COLOR: #ff0000">class</span><span style="COLOR: #0000ff">="box"</span><span style="COLOR: #0000ff">></span><br /><span style="COLOR: #0000ff"><</span><span style="COLOR: #800000">br </span><span style="COLOR: #0000ff">/><</span><span style="COLOR: #800000">br </span><span style="COLOR: #0000ff">/><</span><span style="COLOR: #800000">br </span><span style="COLOR: #0000ff">/><</span><span style="COLOR: #800000">br </span><span style="COLOR: #0000ff">/></span><br />看,我有两个颜色的阴影!O(∩_∩)O哈!<br /><span style="COLOR: #0000ff"></</span><span style="COLOR: #800000">div</span><span style="COLOR: #0000ff">></span><br /><span style="COLOR: #0000ff"></</span><span style="COLOR: #800000">body</span><span style="COLOR: #0000ff">></span><br /><span style="COLOR: #0000ff"></</span><span style="COLOR: #800000">html</span><span style="COLOR: #0000ff">></span></div> |
运行效果如下(Chrome中):
可以看到,有两个颜色的阴影。
看下面一个例子:为下面这张图片添加阴影,让其有立体感:
1 |
<div><span style="COLOR: #0000ff"><!</span><span style="COLOR: #ff00ff">DOCTYPE html</span><span style="COLOR: #0000ff">></span><br /><span style="COLOR: #0000ff"><</span><span style="COLOR: #800000">html</span><span style="COLOR: #0000ff">></span><br /><span style="COLOR: #0000ff"><</span><span style="COLOR: #800000">head</span><span style="COLOR: #0000ff">></span><br /><span style="COLOR: #0000ff"><</span><span style="COLOR: #800000">title</span><span style="COLOR: #0000ff">></</span><span style="COLOR: #800000">title</span><span style="COLOR: #0000ff">></span><br /><span style="COLOR: #0000ff"><</span><span style="COLOR: #800000">meta </span><span style="COLOR: #ff0000">charset</span><span style="COLOR: #0000ff">="utf-8"</span><span style="COLOR: #ff0000"> </span><span style="COLOR: #0000ff">/></span><br /><span style="COLOR: #0000ff"><</span><span style="COLOR: #800000">style</span><span style="COLOR: #0000ff">></span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #800000"><br />body </span><span style="background-color: rgb(245, 245, 245); ">{</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #ff0000"><br /> width</span><span style="background-color: rgb(245, 245, 245); ">:</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #0000ff">500px</span><span style="background-color: rgb(245, 245, 245); ">;</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #ff0000"><br /> margin</span><span style="background-color: rgb(245, 245, 245); ">:</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #0000ff">50px auto</span><span style="background-color: rgb(245, 245, 245); ">;</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #ff0000"><br /></span><span style="background-color: rgb(245, 245, 245); ">}</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #800000"><br />.box </span><span style="background-color: rgb(245, 245, 245); ">{</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #ff0000"><br /> position</span><span style="background-color: rgb(245, 245, 245); ">:</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #0000ff">relative</span><span style="background-color: rgb(245, 245, 245); ">;</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #ff0000"><br /> -webkit-box-shadow</span><span style="background-color: rgb(245, 245, 245); ">:</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #0000ff">1px 2px 4px rgba(0, 0, 0, 0.5)</span><span style="background-color: rgb(245, 245, 245); ">;</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #ff0000"><br /> -moz-box-shadow</span><span style="background-color: rgb(245, 245, 245); ">:</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #0000ff">1px 2px 4px rgba(0, 0, 0, 0.5)</span><span style="background-color: rgb(245, 245, 245); ">;</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #ff0000"><br /> box-shadow</span><span style="background-color: rgb(245, 245, 245); ">:</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #0000ff">1px 2px 4px rgba(0, 0, 0, 0.5)</span><span style="background-color: rgb(245, 245, 245); ">;</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #ff0000"><br /> padding</span><span style="background-color: rgb(245, 245, 245); ">:</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #0000ff">10px</span><span style="background-color: rgb(245, 245, 245); ">;</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #ff0000"><br /> background</span><span style="background-color: rgb(245, 245, 245); ">:</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #0000ff">#fff</span><span style="background-color: rgb(245, 245, 245); ">;</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #ff0000"><br /></span><span style="background-color: rgb(245, 245, 245); ">}</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #800000"><br />.box img </span><span style="background-color: rgb(245, 245, 245); ">{</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #ff0000"><br /> width</span><span style="background-color: rgb(245, 245, 245); ">:</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #0000ff">100%</span><span style="background-color: rgb(245, 245, 245); ">;</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #ff0000"><br /> border</span><span style="background-color: rgb(245, 245, 245); ">:</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #0000ff">1px solid #8a4419</span><span style="background-color: rgb(245, 245, 245); ">;</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #ff0000"><br /> border-style</span><span style="background-color: rgb(245, 245, 245); ">:</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #0000ff">inset</span><span style="background-color: rgb(245, 245, 245); ">;</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #ff0000"><br /></span><span style="background-color: rgb(245, 245, 245); ">}</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #800000"><br />.box:after </span><span style="background-color: rgb(245, 245, 245); ">{</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #ff0000"><br /> content</span><span style="background-color: rgb(245, 245, 245); ">:</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #0000ff">''</span><span style="background-color: rgb(245, 245, 245); ">;</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #ff0000"><br /> position</span><span style="background-color: rgb(245, 245, 245); ">:</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #0000ff">absolute</span><span style="background-color: rgb(245, 245, 245); ">;</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #ff0000"><br /> z-index</span><span style="background-color: rgb(245, 245, 245); ">:</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #0000ff">-1</span><span style="background-color: rgb(245, 245, 245); ">;</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #ff0000"><br /> <br /> </span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #008000">/*</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #008000"> 阴影部分 </span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #008000">*/</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #ff0000"><br /> -webkit-box-shadow</span><span style="background-color: rgb(245, 245, 245); ">:</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #0000ff">0 15px 20px rgba(0, 0, 0, 0.3)</span><span style="background-color: rgb(245, 245, 245); ">;</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #ff0000"><br /> -moz-box-shadow</span><span style="background-color: rgb(245, 245, 245); ">:</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #0000ff">0 15px 20px rgba(0, 0, 0, 0.3)</span><span style="background-color: rgb(245, 245, 245); ">;</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #ff0000"><br /> box-shadow</span><span style="background-color: rgb(245, 245, 245); ">:</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #0000ff">0 15px 20px rgba(0, 0, 0, 0.3)</span><span style="background-color: rgb(245, 245, 245); ">;</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #ff0000"><br /> <br /> width</span><span style="background-color: rgb(245, 245, 245); ">:</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #0000ff">70%</span><span style="background-color: rgb(245, 245, 245); ">;</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #ff0000"><br /> left</span><span style="background-color: rgb(245, 245, 245); ">:</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #0000ff">15%</span><span style="background-color: rgb(245, 245, 245); ">;</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #ff0000"><br /> height</span><span style="background-color: rgb(245, 245, 245); ">:</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #0000ff">100px</span><span style="background-color: rgb(245, 245, 245); ">;</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #ff0000"><br /> bottom</span><span style="background-color: rgb(245, 245, 245); ">:</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #0000ff">0</span><span style="background-color: rgb(245, 245, 245); ">;</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #ff0000"><br /></span><span style="background-color: rgb(245, 245, 245); ">}</span><span style="BACKGROUND-COLOR: #f5f5f5; COLOR: #800000"><br /></span><span style="COLOR: #0000ff"></</span><span style="COLOR: #800000">style</span><span style="COLOR: #0000ff">></span><br /><span style="COLOR: #0000ff"></</span><span style="COLOR: #800000">head</span><span style="COLOR: #0000ff">></span><br /><span style="COLOR: #0000ff"><</span><span style="COLOR: #800000">body</span><span style="COLOR: #0000ff">></span><br /><span style="COLOR: #0000ff"><</span><span style="COLOR: #800000">div </span><span style="COLOR: #ff0000">class</span><span style="COLOR: #0000ff">="box"</span><span style="COLOR: #0000ff">></span><br /> <span style="COLOR: #0000ff"><</span><span style="COLOR: #800000">img </span><span style="COLOR: #ff0000">src</span><span style="COLOR: #0000ff">="allstar.jpg"</span><span style="COLOR: #ff0000"> title</span><span style="COLOR: #0000ff">="球星云集"</span><span style="COLOR: #ff0000"> </span><span style="COLOR: #0000ff">/></span><br /><span style="COLOR: #0000ff"></</span><span style="COLOR: #800000">div</span><span style="COLOR: #0000ff">></span><br /><span style="COLOR: #0000ff"></</span><span style="COLOR: #800000">body</span><span style="COLOR: #0000ff">></span><br /><span style="COLOR: #0000ff"></</span><span style="COLOR: #800000">html</span><span style="COLOR: #0000ff">></span></div> |
运行效果如下(Chrome中):
以前我吗需要在photoshop中把图片做成这样的效果,现在可以直接用CSS代码来实现了。
转自:http://www.cnblogs.com/lianjun/archive/2011/03/14/1983465.html