创建一个线性渐变,需要指定两种颜色,还可以实现不同方向(指定为一个角度)的渐变效果,如果不指定方向,默认从上到下渐变。
CSS语法:
background-image: linear-gradient(direction, color-stop1, color-stop2, ...); background: linear-gradient(direction, color-stop1, color-stop2, ...);
- direction:用角度值指定渐变的方向(或角度)。
- color-stop1, color-stop2:用于指定渐变的起止颜色。
/* 从上到下,蓝色渐变到红色 */
linear-gradient(blue, red);
/* 渐变轴为45度,从蓝色渐变到红色 */
linear-gradient(45deg, blue, red);
/* 从右下到左上、从蓝色渐变到红色 */
linear-gradient(to left top, blue, red);
/* 从下到上,从蓝色开始渐变、到高度40%位置是绿色渐变开始、最后以红色结束 */
linear-gradient(0deg, blue, green 40%, red);