使用CSS实现背景色渐变、边框渐变,字体渐变的效果。css
.bg-block { background: linear-gradient(to bottom, #F80, #2ED); }
效果如图:html
linear-gradient: ([ <angle> | to <side-or-corner>, ]? <color-stop> [, <color-stop> + ])web
angle | side-to corner 定义了渐变线,to-bottom 等同于180deg, to-top 等同于0deg。
color-stop 定义渐变的颜色,可使用百分比指定渐变长度。好比:浏览器
background: linear-gradient(180deg, #F80 70%, #2ED);
则变成了酱子:ide
背景色渐变很是简单,但上面的css代码中,linear-gradient是加在background属性上的。因而测试下具体是加在了哪一个属性上,首先感性上就以为是加在了background-color上,修改代码background为background-color以后,果真,渐变色没有了。
仔细看下linear-gradient的定义:post
Thelinear-gradient()
function creates an image consisting of a progressive transition between two or more colors along a straight line. Its result is an object of the<gradient>
data type, which is a special kind of [<image>
]
因而,这应该是个image了,修改代码background为background-image,结果渐变色保持如上图。测试
字体渐变没那么容易想到了,参考了张鑫旭大神的文章,实现以下:字体
.font-block { font-size: 48px; background-image: linear-gradient(to bottom,#F80, #2ED); -webkit-background-clip: text; -webkit-text-fill-color: transparent; }
效果以下:url
这种字体渐变的方法能够这么理解:字体自己是有颜色的,先让字体自己的颜色变透明(text-fill-color为transparent),而后添加渐变的背景色(background-image: line-gradient...),可是得让背景色做用在字体上(background-clip: text)。spa
要注意的是:
text-overflow: ellipsis;
这个时候是看不到点点点的。.border-block { border: 10px solid transparent; border-image: linear-gradient(to top, #F80, #2ED); border-image-slice: 10; }
实现效果以下:
给border-image加linear-gradient不难理解,可是若是单纯使用border-image,会发现效果是这样的:
因此关键做用是border-image-slice属性。
先看下border-image属性。
border-image是border-image-source, border-image-slice, border-image-width, border-image-outset, border-image-repeat的简写。
border-image-source 属性能够给定一个url做为边框图像,相似background-image: url的用法;
border-image-slice是指将边框图片切成9份,四个角四个边,和一个中心区域。被切割的9个部分分布在边框的9个区域。
当盒子宽度和被切图像的宽度不相等时,四个顶角的变化具备必定的拉伸效果。border-image-slice属性默认值是100%,这个百分比是相对于边框图像的宽高来的,当左右切片宽度之和>100%时,5号7号就显示空白,所以使用默认值没法看到被填满的边框图片。关于boder-image具体能够参考这篇References第一篇文章,讲的比较详细。
1.CSS3边框图片详解:http://www.360doc.com/content...
2.linear-gradient MDN:
https://developer.mozilla.org...