css3渐变

分类

  1. 线性渐变(Linear Gradients)- 向下/向上/向左/向右/对角方向
  2. 径向渐变(Radial Gradients)- 由它们的中心定义

线性渐变
语法:

background: linear-gradient( [ | ,]? [, ]+ );

解读:
  • angle–角度 用角度指定渐变的方向(角度)–逆时针
    • 0deg 从左到右
    • 90deg 从下到上
    • 180deg 从右到左
    • 270deg 从上到下
  • side-or-corner [left | right] || [top | bottom]
  • color-stop [|]?
    • color 指定颜色
    • length 指定起止色的位置,不允许负值
    • percentage 用百分比指定起止色位置
例子
  • 默认从上到下渐变
background: -webkit-linear-gradient(red, blue); /* Safari 5.1 - 6.0 */
background: -o-linear-gradient(red, blue); /* Opera 11.1 - 12.0 */
background: -moz-linear-gradient(red, blue); /* Firefox 3.6 - 15 */
background: linear-gradient(red, blue); /* 标准的语法 */
  • 从左到右,指定方向(注意不同浏览器内核方向解析不同)
background: -webkit-linear-gradient(left, red , blue); /* Safari 5.1 - 6.0 */
background: -o-linear-gradient(right, red, blue); /* Opera 11.1 - 12.0 */
background: -moz-linear-gradient(right, red, blue); /* Firefox 3.6 - 15 */
background: linear-gradient(to right, red , blue); /* 标准的语法 */
  • 对角(左上角到右下角)
background: -webkit-linear-gradient(left top, red , blue); /* Safari 5.1 - 6.0 */
background: -o-linear-gradient(bottom right, red, blue); /* Opera 11.1 - 12.0 */
background: -moz-linear-gradient(bottom right, red, blue); /* Firefox 3.6 - 15 */
background: linear-gradient(to bottom right, red , blue); /* 标准的语法 */

在这里插入图片描述

  • 多个颜色
background: -webkit-linear-gradient(red, green, blue); /* Safari 5.1 - 6.0 */
background: -o-linear-gradient(red, green, blue); /* Opera 11.1 - 12.0 */
background: -moz-linear-gradient(red, green, blue); /* Firefox 3.6 - 15 */
background: linear-gradient(red, green, blue); /* 标准的语法 */

在这里插入图片描述

  • 使用透明色 rgba(255,0,0,0)可替换为transparent
background: -webkit-linear-gradient(left,rgba(255,0,0,0),rgba(255,0,0,1)); /* Safari 5.1 - 6 */
background: -o-linear-gradient(right,rgba(255,0,0,0),rgba(255,0,0,1)); /* Opera 11.1 - 12*/
background: -moz-linear-gradient(right,rgba(255,0,0,0),rgba(255,0,0,1)); /* Firefox 3.6 - 15*/
background: linear-gradient(to right, rgba(255,0,0,0), rgba(255,0,0,1)); /* 标准的语法 */

在这里插入图片描述

  • 使用角度
background: -webkit-linear-gradient(45deg, red, blue); /* Safari 5.1 - 6.0 */
background: -o-linear-gradient(45deg, red, blue); /* Opera 11.1 - 12.0 */
background: -moz-linear-gradient(45deg, red, blue); /* Firefox 3.6 - 15 */
background: linear-gradient(45deg, red, blue); /* 标准的语法 */

在这里插入图片描述

  • 颜色占比50px-150px间,红色和黄色渐变
background: -webkit-linear-gradient(left, red 50px, yellow 150px);

在这里插入图片描述

  • 重复
background: -webkit-repeating-linear-gradient(red, yellow 10%, green 40%);

在这里插入图片描述

  • 酷炫
.box1 {
	background: linear-gradient(45deg, #dca 12%, transparent 0, transparent 88%, #dca 0),
    linear-gradient(135deg, transparent 37%, #a85 0, #a85 63%, transparent 0),
    linear-gradient(45deg, transparent 37%, #dca 0, #dca 63%, transparent 0) #753;
    background-size: 25px 25px;
}
.box2 {
	background:
		linear-gradient(45deg, #92baac 45px, transparent 45px)64px 64px,
		linear-gradient(45deg, #92baac 45px, transparent 45px,transparent 91px, #e1ebbd 91px, #e1ebbd 135px, transparent 135px),
		linear-gradient(-45deg, #92baac 23px, transparent 23px, transparent 68px,#92baac 68px,#92baac 113px,transparent 113px,transparent 158px,#92baac 158px);
	background-color:#e1ebbd;
	background-size: 128px 128px;
}
.box3 {
	background:
		linear-gradient(135deg, #708090 21px, #d9ecff 22px, #d9ecff 24px, transparent 24px, transparent 67px, #d9ecff 67px, #d9ecff 69px, transparent 69px),
		linear-gradient(225deg, #708090 21px, #d9ecff 22px, #d9ecff 24px, transparent 24px, transparent 67px, #d9ecff 67px, #d9ecff 69px, transparent 69px)0 64px;
	background-color:#708090;
	background-size: 64px 128px;
}
.box4 {
	background-color:black;
	background-image:
		radial-gradient(white, rgba(255,255,255,.2) 2px, transparent 40px),
		radial-gradient(white, rgba(255,255,255,.15) 1px, transparent 30px),
		radial-gradient(white, rgba(255,255,255,.1) 2px, transparent 40px),
		radial-gradient(rgba(255,255,255,.4), rgba(255,255,255,.1) 2px, transparent 30px);
	background-size: 550px 550px, 350px 350px, 250px 250px, 150px 150px;
	background-position: 0 0, 40px 60px, 130px 270px, 70px 100px;
}
.box5 {
	   background-image: repeating-linear-gradient(0deg, rgba(0, 0, 0, 0.04), rgba(0, 0, 0, 0.04) 1px, transparent 1px, transparent 20px),
					     repeating-linear-gradient(-90deg, rgba(0, 0, 0, 0.04), rgba(0, 0, 0, 0.04) 1px, transparent 1px, transparent 20px);
       background-size: 10px 10px;
       background-position: 1px 1px;
       background-color: rgb(242,242, 242);
       box-shadow: rgba(0, 0, 0, 0.24) 0px 2px 4px 0px;
}

在这里插入图片描述


径向渐变
语法

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:最远角
例子
  • 多颜色均匀分布
background: -webkit-radial-gradient(#71FF5E, #FFF175, #FF0000); /* Safari 5.1 - 6.0 */
background: -o-radial-gradient(#71FF5E, #FFF175, #FF0000); /* Opera 11.6 - 12.0 */
background: -moz-radial-gradient(#71FF5E, #FFF175, #FF0000); /* Firefox 3.6 - 15 */
background: radial-gradient(#71FF5E, #FFF175, #FF0000); /* 标准的语法 */
  • 颜色节点不均匀分布
background: -webkit-radial-gradient(#71FF5E 5%, #FFF175 15%, #FF0000 60%); /* Safari 5.1 - 6.0 */
background: -o-radial-gradient(#71FF5E 5%, #FFF175 15%, #FF0000 60%); /* Opera 11.6 - 12.0 */
background: -moz-radial-gradient(#71FF5E 5%, #FFF175 15%, #FF0000 60%); /* Firefox 3.6 - 15 */
background: radial-gradient(#71FF5E 5%, #FFF175 15%, #FF0000 60%); /* 标准的语法 */
  • 设置形状
// circle
background: radial-gradient(circle, red, yellow, green)
// ellipse
background: radial-gradient(ellipse, red, yellow, green)

在这里插入图片描述

  • 不同尺寸的渐变
background: radial-gradient(60% 55%, closest-side, blue, green, yellow, black) 
background: radial-gradient(60% 55%, farthest-side, blue, green, yellow, black)
background: radial-gradient(60% 55%, closest-corner, blue, green, yellow, black)
background: radial-gradient(60% 55%, farthest-corner, blue, green, yellow, black)

在这里插入图片描述

  • 重复
background: repeating-radial-gradient(red, yellow 10%, green 20%)
  • 设置方向
background: radial-gradient(bottom left, #fff 0%, #fff 60%, green 60%, green 100%)
  • 重复半圆角内边框–左
background: #BCD0C5;
background-size: 15px 22px;
background-repeat: repeat-y;
background-image: -webkit-radial-gradient(left, circle, #fff 45%, transparent 45%); 
  • 重复半圆角内边框–右
background: #BCD0C5;
background-size: 15px 22px;
background-repeat: repeat-y;
background-position: right;
background-image: -webkit-radial-gradient(right, circle, #fff 45%, transparent 45%);

在这里插入图片描述

  • 实现内倒角
background-image: radial-gradient(circle at right top, #fff, #fff 10px, transparent 11px),
       			radial-gradient(circle at right bottom, #fff, #fff 10px, transparent 11px);
background-color: red;
  • 上下花边
background: #BCD0C5;
position: relative;
::before {
    content: '';
    position: absolute;
    height: 10px;
    left: 0;
    right: 0;
    top: -10px;
    background: radial-gradient(20px 15px ellipse at bottom, #BCD0C5 10px, transparent 11px);
    background-size: 20px 10px;
}
::after {
    content: '';
    position: absolute;
    height: 10px;
    left:0 ; right: 0;
    bottom: -10px;
    background: radial-gradient(20px 15px ellipse at top, #BCD0C5 10px, transparent 11px);
    background-size: 20px 10px;
}
  • 磁带
width: 200px;
height: 200px;
position: relative;
border-radius: 50%;
background: linear-gradient(30deg, transparent 40%, rgba(42, 41, 40, .85) 40%) no-repeat 100% 0, linear-gradient(60deg, rgba(42, 41, 40, .85) 60%, transparent 60%) no-repeat 0 100%, repeating-radial-gradient(#2a2928, #2a2928 4px, #ada9a0 5px, #2a2928 6px);
background-size: 50% 100%, 100% 50%, 100% 100%;
:after {
	position: absolute;
    top: 50%; left: 50%;
    margin: -35px;
    border: solid 1px #d9a388;
    width: 68px; height: 68px;
    border-radius: 50%;
    box-shadow: 0 0 0 4px #da5b33, inset 0 0 0 27px #da5b33;
    background: #b5ac9a;
    content: '';
}
  • 右上角
background: radial-gradient(100px at right top,transparent 50%,#BCD0C5 50%);

在这里插入图片描述

  • 左右外角
background: #BCD0C5;
border: none;
border-top-left-radius: 20px;
border-top-right-radius: 20px;
position: relative;
::before {
	content: '';
	position: absolute;
	bottom: 0px;
	left: -13px;
	height: 15px;
	width: 14px;
	background: radial-gradient(27px at left top, transparent 50%, #BCD0C5 50%);
}
::after {
	content: '';
	position: absolute;
	bottom: 0px;
	right: -13px;
	width: 14px;
	height: 15px;
	background: radial-gradient(27px at right top, transparent 50%, #BCD0C5 50%);
}

在这里插入图片描述

注:颜色渐变位置图解:具体请看https://www.zhangxinxu.com/wordpress/2013/09/%E6%B7%B1%E5%85%A5%E7%90%86%E8%A7%A3css3-gradient%E6%96%9C%E5%90%91%E7%BA%BF%E6%80%A7%E6%B8%90%E5%8F%98/

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: CSS3渐变背景颜色动画是通过使用CSS3的transition和animation属性来实现的。首先,我们可以使用linear-gradient函数定义一个渐变背景颜色: div { background: linear-gradient(to right, red, blue); } 这将创建一个从左到右的红色到蓝色的渐变背景。然后,我们可以使用transition属性来定义过渡效果: div { transition: background 0.5s ease-in-out; } 这将使背景颜色的变化在0.5秒内平滑过渡,并且动画效果的速度采用了"ease-in-out"的缓动函数,使过渡更加自然。接下来,我们可以使用animation属性来创建一个动画: @keyframes colorChange { 0% { background: red; } 50% { background: blue; } 100% { background: red; } } div { animation: colorChange 3s infinite; } 这里我们定义了一个名为colorChange的动画,它会在3秒钟内循环播放,并且背景颜色会在动画的不同阶段中从红色到蓝色再返回红色。最后,我们将动画应用到div元素上。通过这些CSS属性和函数的组合,我们就可以实现一个具有渐变背景颜色动画的效果。 ### 回答2: CSS3渐变背景颜色动画可以通过使用@keyframes和animation属性来实现。首先,我们需要定义一个@keyframes规则,用于指定渐变动画的关键帧。 在@keyframes规则中,我们可以通过调整背景颜色的透明度或色值来创建渐变效果。例如,我们可以从一个颜色过渡到另一个颜色,或者让背景颜色从透明变为不透明。通过定义多个关键帧,我们可以创造出更加复杂的渐变效果。 接下来,我们将@keyframes规则应用到要应用渐变动画的元素上,通过animation属性设置动画的名称、时间和动画类型等属性。 例如,下面是一个使用CSS3渐变背景颜色动画的示例代码: ``` <style> @keyframes gradientAnimation { 0% { background-color: red; } 50% { background-color: yellow; } 100% { background-color: blue; } } .box { width: 200px; height: 200px; animation: gradientAnimation 5s linear infinite; } </style> <div class="box"></div> ``` 在上面的代码中,我们定义了一个名为gradientAnimation的@keyframes规则,它将背景颜色从红色过渡到黄色,再过渡到蓝色。然后,我们在class为box的元素上应用了这个动画,并设置动画的持续时间为5秒,动画类型为线性动画,且无限循环播放。 通过上述代码,我们可以在浏览器中看到一个长方形元素,它的背景颜色会不断从红色过渡到黄色,再过渡到蓝色,然后重新开始。这就是一个简单的CSS3渐变背景颜色动画的实现。 ### 回答3: CSS3渐变背景颜色动画是通过使用CSS3渐变属性和动画属性来实现的。首先,我们可以使用渐变属性来定义一个渐变背景,例如使用linear-gradient(线性渐变)或radial-gradient(径向渐变)函数来定义渐变方向和颜色范围。 然后,我们可以使用动画属性来创建一个背景颜色的动画效果。我们可以定义动画的持续时间、动画类型和动画延迟等属性。通过这些属性的组合,我们可以创建出各种不同的渐变背景颜色动画效果。 例如,我们可以创建一个线性渐变背景颜色动画,让背景颜色从红色渐变到蓝色。可以使用以下CSS代码实现: ```css div { width: 200px; height: 200px; background: linear-gradient(to right, red, blue); animation: bg-animation 3s linear infinite; } @keyframes bg-animation { 0% { background: linear-gradient(to right, red, blue); } 50% { background: linear-gradient(to right, blue, green); } 100% { background: linear-gradient(to right, green, red); } } ``` 在上面的代码中,我们创建了一个div元素,并且设置了它的宽度和高度。然后,我们使用linear-gradient函数来定义一个红色到蓝色的线性渐变作为背景颜色。 接下来,我们使用@keyframes规则创建了一个名为bg-animation的动画。在动画的关键帧中,我们定义了背景颜色在0%、50%和100%时的值,分别是红色到蓝色、蓝色到绿色和绿色到红色的线性渐变。 最后,我们把动画应用到了div元素上,设置了动画的持续时间为3秒,动画类型为线性(linear),并且让动画无限循环播放。 通过这种方式,我们就可以创建出一个使用CSS3渐变背景颜色动画的效果。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值