您现在的位置:中国下载站学院中心网页设计Html·Css教程Css教程 → 文章列表

CSS实现图片阴影效果三部曲(译文)

作者:佚名  来源:不详  发布时间:2007-10-20 21:27:01   

减小字体 增大字体

 
 

CSS实现图片阴影效果三部曲 css shadow triple steps
此文译自CSS.Mastery.Advanced.Web.Standards.Solutions.Feb.2006.pdf
预览效果请直接看第三步。译文为直译,具体请参考英文版本。英文书籍也有很多“八股”,看多了就习惯了

正文开始:
第一步、如何实现图片阴影效果
为了创建这种效果,首先你设置一个阴影图片背景在外套图层标签。因为图层标签是块级元素,它们将水平伸展,并占据可以获取的所有空间。这种情形下我们可以使用这种技巧,用图层外套住图片。另外,也可以设置图层浮动,来收缩外套。(笔者用的系统是mac)


以下内容为<a target=程序代码"/> 以下内容为程序代码
.img-wrapper {
background: url(shadow.gif) no-repeat bottom right;
clear: right;
float: left;
}

为了显示带阴影效果的图片,需要设置图片负边距偏移。(阴影效果在右边和下方,所以设置整体移动5个像素)

以下内容为<a target=程序代码"/> 以下内容为程序代码
.img-wrapper img {
margin: -5px 5px 5px -5px;
}

—————————————————————-
First
To create the effect, you first need to apply your shadow graphic to the background of the
wrapper div. Because divs are block-level elements, they stretch horizontally, taking up all
the available space. In this situation we want the div to wrap around the image. You can
do this by explicitly setting a width for the wrapper div, but doing so reduces the usefulness
of this technique. Instead, you can float the div, causing it to “shrink-wrap” on modern
browsers, with one exception: IE 5.x on the Mac.
.img-wrapper {
background: url(shadow.gif) no-repeat bottom right;
clear: right;
float: left;
}
To reveal the shadow image and create the drop shadow effect (see Figure 3-13), you need
to offset the image using negative margins:
.img-wrapper img {
margin: -5px 5px 5px -5px;
}

以下是程序代码


[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]

经过第一步已经实现了图片阴影效果,但是还不完善,接下来还是要继续修饰

第二步、逼真照片边框效果
现在给图片加上边框和内补白,就可以创建一个非常好的,以假乱真的照片边框效果
Second
You can create a good, fake photo border effect by giving the image a border and some
padding

以下内容为<a target=程序代码"/> 以下内容为程序代码
.img-wrapper img {
background-color: #fff;
border: 1px solid #a9a9a9;
padding: 4px;
margin: -5px 5px 5px -5px;
}

PS: 1px灰色边框和4px内补白正好抵消了5个像素的边距了。
以下是程序代码


[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]

第三步、修正IE中图片显示的bug
刚才那些在标准兼容的浏览器中是显示正常的。无论怎样,我们都需要加一些简单的规则使得他在IE6显示正常。
This works for most modern, standards-compliant browsers. However, we need to add in a
couple of simple rules to get it working correctly in IE 6:

以下内容为<a target=程序代码"/> 以下内容为程序代码
.img-wrapper {
background: url(shadow.gif) no-repeat bottom right;
clear: right;
float: left;
position: relative;
}
.img-wrapper img {
background-color: #fff;
border: 1px solid #a9a9a9;
padding: 4px;
display: block;
margin: -5px 5px 5px -5px;
position: relative;
}

PS:div设置relative 是为了使得左上两条边框正常显示,设置display:block;是为了去除图片下面的空白。

以下是程序代码


[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]

BTW:阴影图片的选择可以是800*800大小的图片了,阴影的效果为5px就可以了。如果你想要比5px更长的可以自制一个。
在Photoshop里面新建795*795的透明,用白色填充图层1,设置样式默认的阴影修改距离为1,如果设置距离为0实际上5px的阴影只有4px了,再修改画布为800*800的,调整一下位置再保存输出。

在百度中搜索更多CSS实现图片阴影效果三部曲(译文)相关网页 转贴于:中国下载站

  • 上一篇文章:CSS实现不定高度DIV绝对居中
  • 下一篇文章:国外译文:网页设计如何使用CSS的选择器模式?
  • 阅读统计:[]
  • 中国下载站】【设为主页】【收藏本页】【打印本文】【回到顶部】【关闭此页

    相关文章
    文章评论(评论内容只代表网友观点,与本站立场无关!)

    用户名: 查看更多评论

    分 值:100分 85分 70分 55分 40分 25分 10分 0分

    内 容:

             (注“”为必填内容。) 验证码: 验证码,看不清楚?请点击刷新验证码


    设为首页 - 关于我们 - 广告服务 - 网站地图 - 加入收藏 - 网站声明 - 网站帮助 - 友情链接

    • Copyright (C) 2006-2008 www.cndownz.com All Rights Reserved.
      中国下载站 版权所有. 粤ICP备05141802号. 对本站有任何建议、意见或投诉,请来信:cndownzcom@yahoo.com.cn.
      喜欢中国下载站(cndownz.com),请把中国下载站(cndownz.com)告诉你QQ上的5位好友,多谢支持!