线性渐变
CSS3提供了linear-gradient属性实现背景颜色的渐变功能。在以前,这种效果必须采用图片才能实现的。
方位可选参数,渐变的方位。可以使用的值有:to top、to top right、to right、to bottom、to bottom left、to left、to top left。
起始色必选参数,颜色值
末尾色必选参数,颜色值
//两个必须参数
div {
width: 500px;
height: 200px;
background-image: linear-gradient(orange, green);
}
//增加一个方位
div {
width: 500px;
height: 200px;
background-image: linear-gradient(to bottom right,orange, green);
}
通过top、left、right、bottom这四组实现的渐变方向有时比较单一,我们可以使用以角度单位的数值来设置方位。比如0度(0deg)相当于to top;角度会沿逆时针方向随着你的角度的增加而增加。
//通过角度设置方位,0~360度之间,可以是负值background-image:linear-gradient(45deg,orange,green);
//多色线性渐变background-image:linear-gradient(-45deg,orange,green,blue,red);
//通过百分比设置多色线性位置background-image:linear-gradient(-45deg,orange 0%,green 20%,
blue 40%,red 100%);
默认情况下:起始颜色的百分比位置是0%,末尾颜色的百分比位置是100%,其他位置按照平均值分配。也可以使用px像素来设定,但计算麻烦点。
//结合背景,并使用透明渐变实现强大层次感
background-color:red;
background-image:linear-gradient(
to top right,rgba(0,0,0,0.6),rgba(0,0,0,0));
//重复渐变属性值
background-image:repeating-linear-gradient(
to top,orange 10px,green 30px);
//加上兼容前缀
background-image:-webkit-linear-gradient(to top,orange,green);
background-image:-moz-linear-gradient(to top,orange,green);
background-image:-o-linear-gradient(to top,orange,green);
background-image:linear-gradient(to top,orange,green);
repeating-linear-gradient属性值和linear-gradient基本相同
径向渐变
CSS3提供了径向渐变,也叫做放射性渐变:radial-gradient属性值。它是从一个点向四周发散的方式扩展。
方位可选参数,径向的方位。可以使用的值有:像素、百分比、固定值,或复合搭配使用
起始色必选参数,颜色值
末尾色必选参数,颜色值
//两个必选参数
background-image:radial-gradient(orange,green);
//如果想设置第一个可选参数,有一种做法是设置为:cirlce(圆形)或ellipse(椭圆形)。默认是椭圆形。
background-image:radial-gradient(circle,orange,green);
circle圆形
ellipse椭圆形,默认值
//不单单可以设置形状,还可以设置形状的发散方向
background-image:radial-gradient(circle at top,orange,green)
//圆心从顶点发散
top从顶部发散
left从左侧发散
right从右侧发散
bottom从底部发散
center从中间发散
//也可以复合方向,比如右下方background-image:radial-gradient(circleatrightbottom,orange,green);
//可以设置发散的距离,即圆的半径长度
background-image:radial-gradient(circle closest-side,orange,green);
closest-side指定径向渐变的半径长度为从圆心到离圆心最近的边
closest-corner指定径向渐变的半径长度为从圆心到离圆心最近的角
farthest-side指定径向渐变的半径长度为从圆心到离圆心最远的边
farthest-corner指定径向渐变的半径长度为从圆心到离圆心最远的角
//关键字有点拗口,可以用像素表示半径,但不接受百分比background-image:radial-gradient(circle 50px,orange,green);
//同样,也有重复背景方式
background-image:repeating-radial-gradient(circle 50px,orange,green);
//兼容模式
background-image:-webkit-radial-gradient(circle,orange,green);
background-image:-moz-radial-gradient(circle,orange,green);
background-image:-o-radial-gradient(circle,orange,green);
background-image:radial-gradient(circle,orange,green);
//两个重复背景只要加上前缀就是兼容模式了
background-image:-webkit-repeating-radial-gradient
background-image:-moz-repeating-radial-gradient
background-image:-o-repeating-radial-gradient
background-image:repeating-radial-gradient