译自:CSS3 Image Styles
中文:CSS3图片样式
请尊重版权,转载请注明来源,多谢~~


直接在图片元素上直接应用CSS3 inset box-shadow 或 border-radius时,浏览器并不能完美的渲染它们。不过,如果把这个图片用作背景图,你就可以可以给它添加任何样式了,浏览器也会很好地渲染。Darcy Clarke和我做了一个简单的教程,讲解如何使用jQuery来动态地制作完美的圆角图片。今天我将重温这个主题然后向你展示使用background-image的方法可以实现多少效果。我将向你展示如何使用box-shadow、border-radius 和 transition 来创作不同的图片风格。

先看下demo

问题 (见 demo)

看一下demo,请注意在第一行的图片中使用了border-radius和inset box-shadow。Firefox会直接在图片元素上渲染border-radius,但不会渲染inset box-shadow。chrome/safari则两者都不渲染。

解决方案

要让 border-radius 和 inset box-shadow 正常工作,解决方案就是将实际图片变作background-image.

动态方法

要想动态实现,可以简单的使用jQuery为每个图片元素外面包一个背景图片。下面的jQuery代码会将所有图片外面包一个span标签然后将图片用作其背景图片(jQuery代码由Darcy Clarke编写)。

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){$("img").load(function() {$(this).wrap(function(){return '<span class="image-wrap ' + $(this).attr('class') + '" style="position:relative; display:inline-block; background:url(' + $(this).attr('src') + ') no-repeat center center; width: ' + $(this).width() + 'px; height: ' + $(this).height() + 'px;" />';});$(this).css("opacity","0");});
});
</script>

输出

上面的脚本将会输出下面的HTML代码:

<span class="image-wrap " style="position:relative; display:inline-block; background:url(image.jpg) no-repeat center center; width: 150px; height: 150px;"><img src="data:image.jpg" style="opacity: 0;">
</span>

圆形图片(见 demo)

现在,图片被用作背景图了,你可以给它添加任意你想要的样式上去。下面是一个简单的使用border-radius实现的圆形图片。如果你对CSS3不太了解,可以阅读下Basics of CSS3,也可以搜索一下前端观察。

CSS

.circle .image-wrap {-webkit-border-radius: 50em;-moz-border-radius: 50em;border-radius: 50em;
}

卡片风格(见 demo)

下面是一个像卡片的图片风格,用了多个inset box-shadow。

CSS

.card .image-wrap {-webkit-box-shadow: inset 0 0 1px rgba(0,0,0,.8), inset 0 2px 0 rgba(255,255,255,.5), inset 0 -1px 0 rgba(0,0,0,.4);-moz-box-shadow: inset 0 0 1px rgba(0,0,0,.8), inset 0 2px 0 rgba(255,255,255,.5), inset 0 -1px 0 rgba(0,0,0,.4);box-shadow: inset 0 0 1px rgba(0,0,0,.8), inset 0 2px 0 rgba(255,255,255,.5), inset 0 -1px 0 rgba(0,0,0,.4);-webkit-border-radius: 20px;-moz-border-radius: 20px;border-radius: 20px;
}

浮雕风格 (见 demo)

通过一点儿改变,我可以将卡片风格转换为浮雕。。。

CSS

.embossed .image-wrap {-webkit-box-shadow: inset 0 0 2px rgba(0,0,0,.8), inset 0 2px 0 rgba(255,255,255,.5), inset 0 -7px 0 rgba(0,0,0,.6), inset 0 -9px 0 rgba(255,255,255,.3);-moz-box-shadow: inset 0 0 2px rgba(0,0,0,.8), inset 0 2px 0 rgba(255,255,255,.5), inset 0 -7px 0 rgba(0,0,0,.6), inset 0 -9px 0 rgba(255,255,255,.3);box-shadow: inset 0 0 2px rgba(0,0,0,.8), inset 0 2px 0 rgba(255,255,255,.5), inset 0 -7px 0 rgba(0,0,0,.6), inset 0 -9px 0 rgba(255,255,255,.3);-webkit-border-radius: 20px;-moz-border-radius: 20px;border-radius: 20px;
}

软浮雕风格 (见 demo)

和浮雕风格真的很像,我只是加了1px的虚化~~

CSS

.soft-embossed .image-wrap {-webkit-box-shadow: inset 0 0 4px rgba(0,0,0,1), inset 0 2px 1px rgba(255,255,255,.5), inset 0 -9px 2px rgba(0,0,0,.6), inset 0 -12px 2px rgba(255,255,255,.3);-moz-box-shadow: inset 0 0 4px rgba(0,0,0,1), inset 0 2px 1px rgba(255,255,255,.5), inset 0 -9px 2px rgba(0,0,0,.6), inset 0 -12px 2px rgba(255,255,255,.3);box-shadow: inset 0 0 4px rgba(0,0,0,1), inset 0 2px 1px rgba(255,255,255,.5), inset 0 -9px 2px rgba(0,0,0,.6), inset 0 -12px 2px rgba(255,255,255,.3);-webkit-border-radius: 20px;-moz-border-radius: 20px;border-radius: 20px;
}

剪贴画风格(见 demo)

同样只是inset box-shadow,我可以让它看起来像剪贴画。

CSS

.cut-out .image-wrap {-webkit-box-shadow: 0 1px 0 rgba(255,255,255,.2), inset 0 4px 5px rgba(0,0,0,.6), inset 0 1px 0 rgba(0,0,0,.6);-moz-box-shadow: 0 1px 0 rgba(255,255,255,.2), inset 0 4px 5px rgba(0,0,0,.6), inset 0 1px 0 rgba(0,0,0,.6);box-shadow: 0 1px 0 rgba(255,255,255,.2), inset 0 4px 5px rgba(0,0,0,.6), inset 0 1px 0 rgba(0,0,0,.6);-webkit-border-radius: 20px;-moz-border-radius: 20px;border-radius: 20px;
}

变形和发光 (见 demo)

这个例子中,我为图片外容器增加了变形。mouseover的时候,它将从圆角形状变为圆形,然后增加了发光效果。发光效果通过多重box-shadow实现。

CSS

.morphing-glowing .image-wrap {-webkit-transition: 1s;-moz-transition: 1s;transition: 1s;-webkit-border-radius: 20px;-moz-border-radius: 20px;border-radius: 20px;
}.morphing-glowing .image-wrap:hover {-webkit-box-shadow: 0 0 20px rgba(255,255,255,.6), inset 0 0 20px rgba(255,255,255,1);-moz-box-shadow: 0 0 20px rgba(255,255,255,.6), inset 0 0 20px rgba(255,255,255,1);box-shadow: 0 0 20px rgba(255,255,255,.6), inset 0 0 20px rgba(255,255,255,1);-webkit-border-radius: 60em;-moz-border-radius: 60em;border-radius: 60em;
}

发光遮罩 (见 demo)

发光渐变遮罩是通过:after伪元素实现的。。。

CSS

.glossy .image-wrap {-webkit-box-shadow: inset 0 -1px 0 rgba(0,0,0,.5);-moz-box-shadow: inset 0 -1px 0 rgba(0,0,0,.5);box-shadow: inset 0 -1px 0 rgba(0,0,0,.5);-webkit-border-radius: 20px;-moz-border-radius: 20px;border-radius: 20px;
}.glossy .image-wrap:after {position: absolute;content: ' ';width: 100%;height: 50%;top: 0;left: 0;-webkit-border-radius: 20px;-moz-border-radius: 20px;border-radius: 20px;background: -moz-linear-gradient(top, rgba(255,255,255,0.7) 0%, rgba(255,255,255,.1) 100%);background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(255,255,255,0.7)), color-stop(100%,rgba(255,255,255,.1)));background: linear-gradient(top, rgba(255,255,255,0.7) 0%,rgba(255,255,255,.1) 100%);
}

倒影 (见 demo)

这个例子中,我将遮罩渐变移动到底部,于是它就成了倒影。。。

CSS

.reflection .image-wrap:after {position: absolute;content: ' ';width: 100%;height: 30px;bottom: -31px;left: 0;-webkit-border-top-left-radius: 20px;-webkit-border-top-right-radius: 20px;-moz-border-radius-topleft: 20px;-moz-border-radius-topright: 20px;border-top-left-radius: 20px;border-top-right-radius: 20px;background: -moz-linear-gradient(top, rgba(0,0,0,.3) 0%, rgba(255,255,255,0) 100%);background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(0,0,0,.3)), color-stop(100%,rgba(255,255,255,0)));background: linear-gradient(top, rgba(0,0,0,.3) 0%,rgba(255,255,255,0) 100%);
}.reflection .image-wrap:hover {position: relative;top: -8px;
}

光泽和倒影(见 demo)

这个例子中,我同时使用了:before和:after伪元素来实现带倒影的光泽效果。

CSS

.glossy-reflection .image-wrap {-webkit-box-shadow: inset 0 -1px 0 rgba(0,0,0,.5), inset 0 1px 0 rgba(255,255,255,.6);-moz-box-shadow: inset 0 -1px 0 rgba(0,0,0,.5), inset 0 1px 0 rgba(255,255,255,.6);box-shadow: inset 0 -1px 0 rgba(0,0,0,.5), inset 0 1px 0 rgba(255,255,255,.6);-webkit-transition: 1s;-moz-transition: 1s;transition: 1s;-webkit-border-radius: 20px;-moz-border-radius: 20px;border-radius: 20px;
}.glossy-reflection .image-wrap:before {position: absolute;content: ' ';width: 100%;height: 50%;top: 0;left: 0;-webkit-border-radius: 20px;-moz-border-radius: 20px;border-radius: 20px;background: -moz-linear-gradient(top, rgba(255,255,255,0.7) 0%, rgba(255,255,255,.1) 100%);background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(255,255,255,0.7)), color-stop(100%,rgba(255,255,255,.1)));background: linear-gradient(top, rgba(255,255,255,0.7) 0%,rgba(255,255,255,.1) 100%);
}.glossy-reflection .image-wrap:after {position: absolute;content: ' ';width: 100%;height: 30px;bottom: -31px;left: 0;-webkit-border-top-left-radius: 20px;-webkit-border-top-right-radius: 20px;-moz-border-radius-topleft: 20px;-moz-border-radius-topright: 20px;border-top-left-radius: 20px;border-top-right-radius: 20px;background: -moz-linear-gradient(top, rgba(230,230,230,.3) 0%, rgba(230,230,230,0) 100%);background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(230,230,230,.3)), color-stop(100%,rgba(230,230,230,0)));background: linear-gradient(top, rgba(230,230,230,.3) 0%,rgba(230,230,230,0) 100%);
}

胶带风格(见 demo)

这里使用了:after伪元素来在图片顶部实现了胶带风格的渐变。

CSS

.tape .image-wrap {-webkit-box-shadow: inset 0 0 2px rgba(0,0,0,.7), inset 0 2px 0 rgba(255,255,255,.3), inset 0 -1px 0 rgba(0,0,0,.5), 0 1px 3px rgba(0,0,0,.4);-moz-box-shadow: inset 0 0 2px rgba(0,0,0,.7), inset 0 2px 0 rgba(255,255,255,.3), inset 0 -1px 0 rgba(0,0,0,.5), 0 1px 3px rgba(0,0,0,.4);box-shadow: inset 0 0 2px rgba(0,0,0,.7), inset 0 2px 0 rgba(255,255,255,.3), inset 0 -1px 0 rgba(0,0,0,.5), 0 1px 3px rgba(0,0,0,.4);
}.tape .image-wrap:after {position: absolute;content: ' ';width: 60px;height: 25px;top: -10px;left: 50%;margin-left: -30px;border: solid 1px rgba(137,130,48,.2);background: -moz-linear-gradient(top, rgba(254,243,127,.6) 0%, rgba(240,224,54,.6) 100%);background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(254,243,127,.6)), color-stop(100%,rgba(240,224,54,.6)));background: linear-gradient(top, rgba(254,243,127,.6) 0%,rgba(240,224,54,.6) 100%);-webkit-box-shadow: inset 0 1px 0 rgba(255,255,255,.3), 0 1px 0 rgba(0,0,0,.2);
}

变形和着色 (见 demo)

在下面的这个例子中,我用了 :after元素来在mouseover的时候添加发光渐变。

CSS

.morphing-tinting .image-wrap {position: relative;-webkit-transition: 1s;-moz-transition: 1s;transition: 1s;-webkit-border-radius: 20px;-moz-border-radius: 20px;border-radius: 20px;
}.morphing-tinting .image-wrap:hover {-webkit-border-radius: 30em;-moz-border-radius: 30em;border-radius: 30em;
}.morphing-tinting .image-wrap:after {position: absolute;content: ' ';width: 100%;height: 100%;top: 0;left: 0;-webkit-transition: 1s;-moz-transition: 1s;transition: 1s;-webkit-border-radius: 30em;-moz-border-radius: 30em;border-radius: 30em;
}
.morphing-tinting .image-wrap:hover:after  {background: -webkit-gradient(radial, 50% 50%, 40, 50% 50%, 80, from(rgba(0,0,0,0)), to(rgba(0,0,0,1)));background: -moz-radial-gradient(50% 50%, circle, rgba(0,0,0,0) 40px, rgba(0,0,0,1) 80px);
}

羽化边缘的圆形(见demo)

发散渐变也可以用作遮罩层来实现圆形羽化效果。

CSS

.feather .image-wrap {position: relative;-webkit-border-radius: 30em;-moz-border-radius: 30em;border-radius: 30em;
}.feather .image-wrap:after  {position: absolute;content: ' ';width: 100%;height: 100%;top: 0;left: 0;background: -webkit-gradient(radial, 50% 50%, 50, 50% 50%, 70, from(rgba(255,255,255,0)), to(rgba(255,255,255,1)));background: -moz-radial-gradient(50% 50%, circle, rgba(255,255,255,0) 50px, rgba(255,255,255,1) 70px);
}

浏览器支持

本文的方法可以在支持border-radius、box-shadow、:before和:after伪元素的浏览器上,比如 Chrome/Safari/Firefox等,而在一些落后的浏览器比如IE9(包括IE9)则不能完全支持——IE6/7/8没有任何表现,IE9会 有普通的圆角。

发挥你的创造力

正如你看到的,你几乎可以使用:before和:after伪元素实现任何效果。如果你有用CSS3实现更多的创意图片效果,欢迎通过评论与大家分享。

PS,本文中使用:before/:after来实现伪元素,其实我更建议使用双冒号来实现,虽然单冒号有更多的浏览器支持,但是对于这些CSS3实现的效果来说,双冒号更安全一些。更多的原因,可以参考《:before和::before的区别》

原文:http://www.qianduan.net/css3-image-styles.html

一些上流的CSS3图片样式相关推荐

  1. boostrap 鼠标滚轮滑动图片_16种基于Bootstrap的css3图片hover效果

    bootstrap-image-hover是一款基于Bootstrap的css3图片hover效果.这组图片hover效果共16种特效.同时该特效也支持非Bootstrap版本. 使用方法 在页面中引 ...

  2. css3-3 css3背景样式

    css3-3 css3背景样式 一.总结 一句话总结:网站页面上的小图标集成在一张大图上面,是因为降低服务器负载,网站上的那些图片都可以下载下来,源码那里,或者工具那里. 1.background:# ...

  3. CSS/CSS3常用样式与web移动端资源

    CSS/CSS3常用样式与知识点 IE条件注释 条件注释简介 IE中的条件注释(Conditional comments)对IE的版本和IE非IE有优秀的区分能力,是WEB设计中常用的hack方法. ...

  4. php中图片怎么加样式,html5实现各种图片样式实例用法汇总

    在我们日常Web网页布局中,我们经常会遇到对对象内鼠标指针光标进行控制,比如鼠标经过指针变为手指形状等样式,常见有些网站鼠标指针是各种各样小图片样式,那么如何实现鼠标样式呢?下面我们就给大家来总结下实 ...

  5. [总结]CSS/CSS3常用样式与web移动端资源

    CSS/CSS3常用样式与知识点 IE条件注释 条件注释简介 IE中的条件注释(Conditional comments)对IE的版本和IE非IE有优秀的区分能力,是WEB设计中常用的hack方法. ...

  6. CSS3图片上下移动的动画效果

    CSS3图片上下移动的动画效果 开发工具与关键技术:Adobe Dreamweaver CC 2018 作者:昨夜星辰 撰写时间:2019年1月17日 下面我们使用Adobe Dreamweaver ...

  7. CSS3 属性样式总结记录(图文)

    CSS3 属性样式总结记录 1 .边框-背景 border-image 设置所有边框图像的速记属性. 3 border-radius 一个用于设置所有四个边框- *-半径属性的速记属性 3 box-s ...

  8. 05 react img css修改svg图片样式

    react img css修改svg图片样式 svg图片的相关理论 定义 优点 前端引入svg图片的方式 方式一:<svg>标签引入,内嵌到 HTML 中 方式二,修改svg的颜色 方式三 ...

  9. java 图片插件_[Java教程]10款功能强大的jQuery/CSS3图片特效插件

    [Java教程]10款功能强大的jQuery/CSS3图片特效插件 0 2014-11-26 03:01:04 1.CSS3实现的底部带滚动云彩效果的网站登录页面 CSS3实现的底部带滚动云彩效果的网 ...

最新文章

  1. linux的source命令,linux命令之Source命令
  2. React 16 Jest单元测试 之 Jest工具
  3. Web 趋势榜:上周最有意思、又热门的 10 大 Web 项目 - 210723
  4. SAP Spartacus 里的 .release-it.json 文件
  5. Python特殊函数
  6. SpringBoot返回json和xml
  7. php-5.6.31安装视频教程_php安装图解教程
  8. SQL SERVER 2012 第五章 创建和修改数据表 の CREATE语句
  9. nginx upstream配置_nginx + ingress + gunicorn 环境上传大文件报错问题的解决思路
  10. vue分割开的tab组件效果
  11. i7 8700k linux,性能起飞!I7-8700K 手动超频和一键超频教程
  12. 卡贴机变无锁教程_有锁机变无锁教程 教你轻松设置去掉卡贴有锁机变无锁完美4G...
  13. vue 跳转路由后返回上一页还是当前页面,但是路由地址有改变(已解决)
  14. python valueerror object2_Python使用json.loads 报错 ValueError: No JSON object could be decoded的解决方法...
  15. 网易云课堂uni-app实战社区交友类app开发笔记
  16. 开房信息被黑客“脱裤”以后…
  17. 服务器信息未初始化失败怎么办,初始化服务器失败
  18. 常微分方程(2):一阶线性微分方程
  19. NumPy 基础教程
  20. 雷锋网专访陌陌产品总监雷小亮

热门文章

  1. 信息学奥赛一本通C++语言——1067:整数的个数
  2. 28 SD配置-主数据-信用管理-定义信贷组
  3. h5键盘把页面顶走了_微信h5开发的坑
  4. 一个springboot能支持多少并发_跑滴滴一个月能挣多少?-好不好
  5. 记录几个CentOS安装包(rpm)的下载地址-离线安装必备
  6. [Unity][FlowCanvas] FlowScript 实现切换当前 CineMachine 摄像机
  7. MATLAB中神经网络train函数使用说明
  8. android udp rtp播放器,IjkPlayer For Android(3)-RTP直播硬解码支持
  9. spss 卡方检验_SPSS篇—卡方检验
  10. el-select 多选取值_数值优化|笔记整理(3)——线搜索中的步长选取方法,线性共轭梯度法...