css3渐变色

css3渐变色

  CSS3 定义了两种类型的渐变:

    线性渐变(Linear Gradients)- 向下/向上/向左/向右/对角方向

    径向渐变(Radial Gradients)- 由它们的中心定义

1.线性渐变
语法:

background: linear-gradient(direction, color-stop1, color-stop2, ...);

说明:

direction:默认为to bottom,即从上向下的渐变;

stop:颜色的分布位置,默认均匀分布,例如有3个颜色,各个颜色的stop均为33.33%。
1.1 单一方向渐变
	left 		从左边开始
	right		从右边开始
	top			从上边开始
	bottom   	从底部开始
	注意: 需要添加兼容前缀

	to left 到左边(结束位置)
	to right 到右边
	to top	到顶部
	to bottom 到底部
	注意: 不要添加兼容前缀

  代码如下:

  <style>
    .box1 {
      width: 800px;
      height: 200px;
      background: linear-gradient(red, blue);
    }
    .box2 {
      width: 800px;
      height: 200px;
      background:  -webkit-linear-gradient(left, red, blue);
      /* 需加上-webkit-前缀不然没有效果 */
    }
    .box3 {
      width: 800px;
      height: 200px;
      background: linear-gradient(to left, red, blue);
    }
  </style>

  <div class="box1">

  </div>
  <br>
  <div class="box2">

  </div>
  <br>
  <div class="box3">

  </div>

  效果如下:
在这里插入图片描述

1.2 对角渐变
	left top	左上开始
	left bottom 左下开始
	right top	右上开始
	注意: 带兼容前缀
			
	to left top 到左上(结束位置)
	注意: 不带兼容前缀

  代码如下:

  <style>

   .box1 {
      width: 800px;
      height: 600px;
      background:  -webkit-linear-gradient(left, red, blue);
      /* 需加上-webkit-前缀不然没有效果 */
    }

    .box2 {
      width: 800px;
      height: 600px;
      background:  -webkit-linear-gradient(left top, red, blue);
      /* 需加上-webkit-前缀不然没有效果 */
    }

    .box3 {
      width: 800px;
      height: 600px;
      background: linear-gradient(to left top, red, blue);
    }
  </style>
 <div class="box1">

  </div>
  <br>
  <div class="box2">

  </div>
  <br>
  <div class="box3">

  </div>

  效果如下:
在这里插入图片描述

1.3 角度的渐变

  语法:

background: linear-gradient(angle, color-stop1, color-stop2);

角度是指水平线和渐变线之间的角度,逆时针方向计算。换句话说,0deg 将创建一个从下到上的渐变,90deg 将创建一个从左到右的渐变。
但是,请注意很多浏览器(Chrome,Safari,fiefox等)的使用了旧的标准,即 0deg 将创建一个从左到右的渐变,90deg 将创建一个从下到上的渐变。换算公式 90 - x = y 其中 x 为标准角度,y为非标准角度。

在这里插入图片描述
  代码如下:

  <style>
    .box1 {
      width: 800px;
      height: 600px;
      background:  -webkit-linear-gradient(0deg, red, blue);
      /* 需加上-webkit-前缀不然没有效果 */
    }

   .box2{
      width: 800px;
      height: 600px;
      background:  -webkit-linear-gradient(45deg, red, blue);
      /* 需加上-webkit-前缀不然没有效果 */
    }

    .box3 {
      width: 800px;
      height: 600px;
      background:  -webkit-linear-gradient(90deg, red, blue);
      /* 需加上-webkit-前缀不然没有效果 */
    }

    .box4 {
      width: 800px;
      height: 600px;
      background:  -webkit-linear-gradient(left, red, blue);
      /* 需加上-webkit-前缀不然没有效果 */
    }


  </style>
  <div class="box1">

  </div>
  <br>
  <div class="box2">

  </div>
  <br>
  <div class="box3">

  </div>
  <br>
  <div class="box4">

  </div>
  <br>

  效果如下:
在这里插入图片描述

1.4 默认情况下颜色趋于均分

  可以指定颜色分布的百分比,让颜色按照百分比进行渐变

  代码如下:

   <style>
    .box1 {
      width: 800px;
      height: 200px;
      background: -webkit-linear-gradient(left, red, blue);
    }
    .box2 {
      width: 800px;
      height: 200px;
      background:  -webkit-linear-gradient(left, red 50%, blue 50%);
      /* 需加上-webkit-前缀不然没有效果 */
    }
    .box3 {
      width: 800px;
      height: 200px;
      background:  -webkit-linear-gradient(left, red 50%, blue);
      /* 需加上-webkit-前缀不然没有效果 */
    }
  </style>
  
  <div class="box1">

  </div>
  <br>
  <div class="box2">

  </div>
  <br>
  <div class="box3">

  </div>

  效果如下:
在这里插入图片描述

2. 径向渐变

  径向渐变不同于线性渐变,线性渐变是从“一个方向”向“另一个方向”的颜色渐变,而径向渐变是从“一个点”向四周的颜色渐变

语法:

background: radial-gradient(center, shape, size, start-color, ..., last-color);
     
说明:

center:渐变起点的位置,可以为百分比,默认是图形的正中心。

shape:渐变的形状,ellipse表示椭圆形,circle表示圆形。默认为ellipse,如果元素形状为正方形的元素,则ellipse和circle显示一样。

size:渐变的大小,即渐变到哪里停止,它有四个值。 closest-side:最近边; farthest-side:最远边; closest-corner:最近角; farthest-corner:最远角。
2. 1 渐变位置
默认从中心到四周

	left	从左边到四周的渐变
	right
	top
	bottom
				
	left top	从左上角到四周的渐变
	left bottom	
	right top
	...
				
	10px 30px	距离左边10px 距离上边30px


  代码如下:

  <style>
    .box1 {
      width: 400px;
      height: 400px;
      background: -webkit-radial-gradient(center, circle, red, blue);
    }
    .box2 {
      width: 400px;
      height: 400px;
      background: -webkit-radial-gradient(left, circle, red, blue);
      /* 需加上-webkit-前缀不然没有效果 */
    }
    .box3 {
      width: 400px;
      height: 400px;
      background: -webkit-radial-gradient(left top, circle, red, blue);
      /* 需加上-webkit-前缀不然没有效果 */
    }
  </style>
 <div class="box1">

  </div>
  <br>
  <div class="box2">

  </div>
  <br>
  <div class="box3">

  </div>

  效果如下:
在这里插入图片描述

2. 2 形状
默认椭圆	ellipse
	正圆		circle
	注意: 元素是正方形,则都是正圆

  代码如下:

  <style>
    .box1 {
      width: 400px;
      height: 400px;
      background: -webkit-radial-gradient(center, red, blue);
    }
    .box2 {
      width: 800px;
      height: 400px;
      background: -webkit-radial-gradient(center, circle, red, blue);
      /* 需加上-webkit-前缀不然没有效果 */
    }
    .box3 {
      width: 800px;
      height: 400px;
      background: -webkit-radial-gradient(center, ellipse, red, blue);
      /* 需加上-webkit-前缀不然没有效果 */
    }
  </style>

  <div class="box1">

  </div>
  <br>
  <div class="box2">

  </div>
  <br>
  <div class="box3">

  </div>

  效果如下:
在这里插入图片描述

2. 3 大小
size:渐变的大小,即渐变到哪里停止,它有四个值。 
		closest-side:最近边; 
		farthest-side:最远边; 
		closest-corner:最近角; 
		farthest-corner:最远角。

  代码如下:

  <style>
    .box1 {
      width: 800px;
      height: 400px;
      background: -webkit-radial-gradient(60% 40%, closest-side, blue, green, yellow, black);
    }
    .box2 {
      width: 800px;
      height: 400px;
      background: -webkit-radial-gradient(60% 40%, farthest-side, blue, green, yellow, black);
      /* 需加上-webkit-前缀不然没有效果 */
    }
    .box3 {
      width: 800px;
      height: 400px;
      background: -webkit-radial-gradient(60% 40%, closest-corner, blue, green, yellow, black);;
      /* 需加上-webkit-前缀不然没有效果 */
    }
    .box4 {
      width: 800px;
      height: 400px;
      background: -webkit-radial-gradient(60% 40%, farthest-corner, blue, green, yellow, black);;
      /* 需加上-webkit-前缀不然没有效果 */
    }
    .box5 {
      width: 800px;
      height: 400px;
      background: -webkit-radial-gradient(60% 40%, blue, green, yellow, black);
      /* 需加上-webkit-前缀不然没有效果 */
    }
  </style>

  效果如下:
在这里插入图片描述

3.重复性渐变
3.1 重复性线性渐变

  代码如下:

    .box1 {
      width: 800px;
      height: 400px;
      background: repeating-linear-gradient(red, blue 10%);;
    }

  效果如下:
在这里插入图片描述

3.2

  代码如下:

    .box1 {
      width: 800px;
      height: 400px;
      background:  repeating-radial-gradient(red, blue 10%);;
    }

  效果如下:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值