您现在的位置: 圆点视线 >> 教程 >> 网页设计 >> CSS样式表 >> 文章正文
[图文]CSS实现图片阴影效果三部曲(译文)           ★★★
CSS实现图片阴影效果三部曲(译文)
作者:网页教学…  文章来源:网页教学网  点击数:  更新时间:2007-10-24 18:39:35 
 页面功能:【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口 【字体:

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

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


[code].img-wrapper {
background: url(shadow.gif) no-repeat bottom right;
clear: right;
float: left;
}[/code]

为了显示带阴影效果的图片,需要设置图片负边距偏移。(阴影效果在右边和下方,所以设置整体移动5个像素)
[code].img-wrapper img {
margin: -5px 5px 5px -5px;
}
[/code]

—————————————————————-
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;
}
[html]

来先休息抽根烟


第一步的效果在FF,Opera下看是正常的,IE下会有空白
[/html]

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

第二步、逼真照片边框效果
现在给图片加上边框和内补白,就可以创建一个非常好的,以假乱真的照片边框效果
Second
You can create a good, fake photo border effect by giving the image a border and some
padding
[code].img-wrapper img {
background-color: #fff;
border: 1px solid #a9a9a9;
padding: 4px;
margin: -5px 5px 5px -5px;
}[/code]
PS: 1px灰色边框和4px内补白正好抵消了5个像素的边距了。
[html]


第二步的效果在FF,Opera下看是正常的,IE下会有空白,而且左上边框没有。
[/html]
第三步、修正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:

[code].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;
}[/code]

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

来先休息抽根烟


第三步的效果在FF,Opera,IE下看都是正常的。至此css shadow 三部曲结束
[/html]
BTW:阴影图片的选择可以是800*800大小的图片了,阴影的效果为5px就可以了。如果你想要比5px更长的可以自制一个。
在Photoshop里面新建795*795的透明,用白色填充图层1,设置样式默认的阴影修改距离为1,如果设置距离为0实际上5px的阴影只有4px了,再修改画布为800*800的,调整一下位置再保存输出。
 【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口】 文章录入:Rainsion    责任编辑:Rainsion  【字体:
 
  • 上一篇文章:

  •  
  • 下一篇文章:
  • 最新文章
    相关文章
    XHTML+CSS的目的和好处(WEB标准的目的和好处
    常见的CSS问题的“一站式”的解决方案
    国外译文:网页设计如何使用CSS的选择器模式
    CSS实现不定高度DIV绝对居中
    25条CSS制作网页编写的提醒及小技巧整理
    Opera 中的 CSS 妙用 ── Flash block
    什么是用户定义的样式表?
    制作网页时手写CSS应该注意的一点技巧
    网页中用于构造图表的一些基本CSS代码
    ul和li标签结合CSS样式表的进行网页布局
    网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    发表评论:
     姓  名: 评 分: 1分 2分 3分 4分 5分
     评论内容: ·严禁发表危害国家安全、政治、黄色淫秽等内容的评论。
    ·用户需对自己在使用本网服务过程中的行为承担法律责任
    ·本站管理员有权保留或删除评论内容。
    ·评论内容只代表机友个人观点,与本网站立场无关。
     
    最 新 推 荐
     
    百度主题推广
    最 新 热 门