【CSS3】渐变背景


一、线性渐变

1. 渐变方向

可设值
描述
to top从下 向上 渐变
to bottom从上 向下 渐变
to left从右 向左 渐变
to right从左 向右 渐变
to top left(顺序可换)从右下 向左上对角 渐变(不等于 - 45°)
to top right(顺序可换)从左下 向右上对角 渐变(不等于 45°)
to bottom left(顺序可换)从右上 向左下对角 渐变(不等于 - 135°)
to bottom right(顺序可换)从左上 向右下对角 渐变(不等于 135°)
从下 向上 渐变
90°从左 向右 渐变
180°从上 向下 渐变
-90° 或 270°从右 向左 渐变
45°从左下向右上渐变(区别 to top right)
-45° 或 315°从右下向左上渐变(区别 to top left)
135°从左上向右下渐变(区别 to bottom right)
-135° 或 225°从右上向左下渐变(区别 to bottom left)

在这里插入图片描述
在这里插入图片描述

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8" />
	<title></title>
	<style type="text/css">
		.wrap {float: left;text-align: center;border: 1px solid #c3c3c3;margin-right: 10px;}
		.box {width: 248px;height: 148px;}
		.lg-tb {background-image: linear-gradient(red, yellow, blue);}
		.lg-tt {background-image: linear-gradient(to top, red, yellow, blue);}
		.lg-tr {background-image: linear-gradient(to right, red, yellow, blue);}
		.lg-tl {background-image: linear-gradient(to left, red, yellow, blue);}
		.lg-tbr {background-image: linear-gradient(to bottom right, red, yellow, blue);}
		.lg-45 {background-image: linear-gradient(45deg, red, yellow, blue);}
		.lg-n45 {background-image: linear-gradient(-45deg, red, yellow, blue);}
		.lg-135 {background-image: linear-gradient(135deg, red, yellow, blue);}
		.lg-n135 {background-image: linear-gradient(-135deg, red, yellow, blue);}
		.lg-0 {background-image: linear-gradient(0deg, red, yellow, blue);}
	</style>
</head>
<body>
	<div style="overflow: hidden;">
		<h2>指向</h2>
		<div class="wrap">
			<h4>从上到下(默认)</h4>
			<div class="box lg-tb"></div>
		</div>
		<div class="wrap">
			<h4>从下到上(to top)</h4>
			<div class="box lg-tt"></div>
		</div>
		<div class="wrap">
			<h4>从左到右(to right)</h4>
			<div class="box lg-tr"></div>
		</div>
		<div class="wrap">
			<h4>从右到左(to left)</h4>
			<div class="box lg-tl"></div>
		</div>
		<div class="wrap">
			<h4>左上到右下(to bottom right)</h4>
			<div class="box lg-tbr"></div>
		</div>
	</div>
	<div style="overflow: hidden;">
		<h2>角度</h2>
		<div class="wrap">
			<h4>45°</h4>
			<div class="box lg-45"></div>
		</div>
		<div class="wrap">
			<h4>-45°</h4>
			<div class="box lg-n45"></div>
		</div>
		<div class="wrap">
			<h4>135°</h4>
			<div class="box lg-135"></div>
		</div>
		<div class="wrap">
			<h4>-135°</h4>
			<div class="box lg-n135"></div>
		</div>
		<div class="wrap">
			<h4></h4>
			<div class="box lg-0"></div>
		</div>
	</div>
</body>
</html>

2. 节点分布

在这里插入图片描述

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8" />
	<title></title>
	<style type="text/css">
		.wrap {float: left;text-align: center;border: 1px solid #c3c3c3;margin-right: 10px;}
		.box {width: 248px;height: 148px;}
		.lg {background-image: linear-gradient(red, yellow, blue, green, purple);}
		.lg-ratio {background-image: linear-gradient(red 5%, yellow 15%, blue 40%, green 65%, purple 90%);}
	</style>
</head>
<body>
	<div style="overflow: hidden;">
		<div class="wrap">
			<h3>均匀(默认)</h3>
			<div class="box lg"></div>
		</div>
		<div class="wrap">
			<h3>不均匀</h3>
			<div class="box lg-ratio"></div>
		</div>
	</div>
</body>
</html>

3. 重复线性渐变

在这里插入图片描述

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8" />
	<title></title>
	<style type="text/css">
		.wrap {float: left;text-align: center;border: 1px solid #c3c3c3;margin-right: 10px;}
		.box {width: 248px;height: 148px;}
		.lg-tb {background-image: repeating-linear-gradient(red, yellow 10%, blue 20%);}
		.lg-tt {background-image: repeating-linear-gradient(to top, red, yellow 10%, blue 20%);}
		.lg-tr {background-image: repeating-linear-gradient(to right, red, yellow 10%, blue 20%);}
		.lg-tl {background-image: repeating-linear-gradient(to left, red, yellow 10%, blue 20%);}
		.lg-tbr {background-image: repeating-linear-gradient(to bottom right, red, yellow 10%, blue 20%);}
		.lg-45 {background-image: repeating-linear-gradient(45deg, red, yellow 10%, blue 20%);}
		.lg-n45 {background-image: repeating-linear-gradient(-45deg, red, yellow 10%, blue 20%);}
		.lg-135 {background-image: repeating-linear-gradient(135deg, red, yellow 10%, blue 20%);}
		.lg-n135 {background-image: repeating-linear-gradient(-135deg, red, yellow 10%, blue 20%);}
		.lg-0 {background-image: repeating-linear-gradient(0deg, red, yellow 10%, blue 20%);}
	</style>
</head>
<body>
	<div style="overflow: hidden;">
		<h2>方向</h2>
		<div class="wrap">
			<h4>从上到下(默认)</h4>
			<div class="box lg-tb"></div>
		</div>
		<div class="wrap">
			<h4>从下到上(to top)</h4>
			<div class="box lg-tt"></div>
		</div>
		<div class="wrap">
			<h4>从左到右(to right)</h4>
			<div class="box lg-tr"></div>
		</div>
		<div class="wrap">
			<h4>从右到左(to left)</h4>
			<div class="box lg-tl"></div>
		</div>
		<div class="wrap">
			<h4>左上到右下(to bottom right)</h4>
			<div class="box lg-tbr"></div>
		</div>
	</div>
	<div style="overflow: hidden;">
		<h2>角度</h2>
		<div class="wrap">
			<h4>45°</h4>
			<div class="box lg-45"></div>
		</div>
		<div class="wrap">
			<h4>-45°</h4>
			<div class="box lg-n45"></div>
		</div>
		<div class="wrap">
			<h4>135°</h4>
			<div class="box lg-135"></div>
		</div>
		<div class="wrap">
			<h4>-135°</h4>
			<div class="box lg-n135"></div>
		</div>
		<div class="wrap">
			<h4></h4>
			<div class="box lg-0"></div>
		</div>
	</div>
</body>
</html>


二、径向渐变

1. 可选属性值【shape size at position】

background-image: repeating-radial-gradient(shape size at position, color1, color2, ...);
可设值
描述
shape 圆的形状
ellipse椭圆(默认)
circle正圆
size 半径大小
farthest-corner圆心到 最远角 的距离作为半径(默认)
farthest-side圆心到 最远边 的距离作为半径
closest-corner圆形到 最近角 的距离作为半径
closest-side圆心到 最近边 的距离作为半径
position 圆心的位置
center盒子中间为圆心(默认)
top盒子上边为圆心
bottom盒子下边为圆心
left盒子左边为圆心
right盒子右边为圆心
top left(顺序可换)盒子左上角为圆心
top right(顺序可换)盒子右上角为圆心
bottom left(顺序可换)盒子左下角为圆心
bottom right(顺序可换)盒子右下角为圆心
x y【可负值】基于父元素的水平与垂直坐标(px、cm等尺寸单位)
如:"0 0" 表示左上角,设置单值 [默认 x],另一个值 [即 y] 默认为 "center"

在这里插入图片描述

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8" />
	<title></title>
	<style type="text/css">
		.wrap {float: left;text-align: center;border: 1px solid #c3c3c3;margin: 0 10px 10px 0;}
		.box {width: 308px;height: 148px;}
		.a {background: rgba(220, 10, 160, 0.4);}
		.b {background: rgba(20, 30, 230, 0.4);}
		.c {background: rgba(255, 0, 140, 0.4);}
		.n-f {float: none;background: rgba(0, 230, 230, 0.4);}
		.e {background: rgba(20, 100, 0, 0.4);}
		.rg-aa {background-image: radial-gradient(ellipse farthest-corner at center, red, yellow, blue);}
		.rg-ab {background-image: radial-gradient(ellipse farthest-corner at top, red, yellow, blue);}
		.rg-ac {background-image: radial-gradient(ellipse farthest-corner at right, red, yellow, blue);}
		.rg-ba {background-image: radial-gradient(ellipse farthest-side at center, red, yellow, blue);}
		.rg-bb {background-image: radial-gradient(ellipse closest-corner at center, red, yellow, blue);}
		.rg-bc {background-image: radial-gradient(ellipse closest-side at center, red, yellow, blue);}
		.rg-ca {background-image: radial-gradient(circle farthest-corner at center, red, yellow, blue);}
		.rg-cb {background-image: radial-gradient(circle farthest-corner at top, red, yellow, blue);}
		.rg-cc {background-image: radial-gradient(circle farthest-corner at right, red, yellow, blue);}
		.rg-da {background-image: radial-gradient(circle farthest-side at center, red, yellow, blue);}
		.rg-db {background-image: radial-gradient(circle closest-corner at center, red, yellow, blue);}
		.rg-dc {background-image: radial-gradient(circle closest-side at center, red, yellow, blue);}
		.rg-tl {background-image: radial-gradient(ellipse farthest-corner at top left, red, yellow, blue);}
		.rg-0 {background-image: radial-gradient(ellipse farthest-corner at 0px, red, yellow, blue);}
		.rg-310-150 {background-image: radial-gradient(ellipse farthest-corner at 310px 150px, red, yellow, blue);}
		.rg-n310-n150 {background-image: radial-gradient(ellipse farthest-corner at -310px -150px, red, yellow, blue);}
	</style>
</head>
<body>
	<div style="float: left;">
		<div style="overflow: hidden;">
			<div class="wrap a">
				<p>ellipse farthest-corner at center(默认)</p>
				<div class="box rg-aa"></div>
			</div>
			<div class="wrap a">
				<p>ellipse farthest-corner at top</p>
				<div class="box rg-ab"></div>
			</div>
			<div class="wrap a">
				<p>ellipse farthest-corner at right</p>
				<div class="box rg-ac"></div>
			</div>
		</div>
		<div style="overflow: hidden;">
			<div class="wrap b">
				<p>ellipse farthest-side at center</p>
				<div class="box rg-ba"></div>
			</div>
			<div class="wrap b">
				<p>ellipse closest-corner at center</p>
				<div class="box rg-bb"></div>
			</div>
			<div class="wrap b">
				<p>ellipse closest-side at center</p>
				<div class="box rg-bc"></div>
			</div>
		</div>
		<div style="overflow: hidden;">
			<div class="wrap c">
				<p>circle farthest-corner at center</p>
				<div class="box rg-ca"></div>
			</div>
			<div class="wrap c">
				<p>circle farthest-corner at top</p>
				<div class="box rg-cb"></div>
			</div>
			<div class="wrap c">
				<p>circle farthest-corner at right</p>
				<div class="box rg-cc"></div>
			</div>
		</div>
	</div>
	<div style="float: left;">
		<div class="wrap n-f">
			<p>circle farthest-side at center</p>
			<div class="box rg-da"></div>
		</div>
		<div class="wrap n-f">
			<p>circle closest-corner at center</p>
			<div class="box rg-db"></div>
		</div>
		<div class="wrap n-f">
			<p>circle closest-side at center</p>
			<div class="box rg-dc"></div>
		</div>
	</div>
	<div>
		<div class="wrap e">
			<p>以左上角为圆心</p>
			<div class="box rg-tl"></div>
		</div>
		<div class="wrap e">
			<p>单值 0,即 x 轴(y 轴自动为 "center")</p>
			<div class="box rg-0"></div>
		</div>
		<div class="wrap e">
			<p>310px 150px(等于宽高时,为右下角)</p>
			<div class="box rg-310-150"></div>
		</div>
		<div class="wrap e">
			<p>-310px -150px(负值时,向左/上偏移)</p>
			<div class="box rg-n310-n150"></div>
		</div>
	</div>
</body>
</html>

2. 节点分布

在这里插入图片描述

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8" />
	<title></title>
	<style type="text/css">
		.wrap {float: left;text-align: center;border: 1px solid #c3c3c3;margin: 0 10px 10px 0;}
		.box {width: 308px;height: 148px;}
		.rg-a {background-image: radial-gradient(ellipse farthest-corner at center, red, yellow, blue);}
		.rg-b {background-image: radial-gradient(ellipse farthest-corner at center, red 20%, yellow 30%, blue 65%);}
	</style>
</head>
<body>
	<div style="overflow: hidden;">
		<div class="wrap">
			<p>均匀</p>
			<div class="box rg-a"></div>
		</div>
		<div class="wrap">
			<p>不均匀</p>
			<div class="box rg-b"></div>
		</div>
	</div>
</body>
</html>

3. 重复径向渐变

在这里插入图片描述

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8" />
	<title></title>
	<style type="text/css">
		.wrap {float: left;text-align: center;border: 1px solid #c3c3c3;margin: 0 10px 10px 0;}
		.box {width: 308px;height: 148px;}
		.a {background: rgba(220, 10, 160, 0.4);}
		.b {background: rgba(20, 30, 230, 0.4);}
		.c {background: rgba(255, 0, 140, 0.4);}
		.n-f {float: none;background: rgba(0, 230, 230, 0.4);}
		.rg-aa {background-image: repeating-radial-gradient(ellipse farthest-corner at center, red 5%, yellow 10%, blue 15%);}
		.rg-ab {background-image: repeating-radial-gradient(ellipse farthest-corner at top, red 5%, yellow 10%, blue 15%);}
		.rg-ac {background-image: repeating-radial-gradient(ellipse farthest-corner at right, red 5%, yellow 10%, blue 15%);}
		.rg-ba {background-image: repeating-radial-gradient(ellipse farthest-side at center, red 5%, yellow 10%, blue 15%);}
		.rg-bb {background-image: repeating-radial-gradient(ellipse closest-corner at center, red 5%, yellow 10%, blue 15%);}
		.rg-bc {background-image: repeating-radial-gradient(ellipse closest-side at center, red 5%, yellow 10%, blue 15%);}
		.rg-ca {background-image: repeating-radial-gradient(circle farthest-corner at center, red 5%, yellow 10%, blue 15%);}
		.rg-cb {background-image: repeating-radial-gradient(circle farthest-corner at top, red 5%, yellow 10%, blue 15%);}
		.rg-cc {background-image: repeating-radial-gradient(circle farthest-corner at right, red 5%, yellow 10%, blue 15%);}
		.rg-da {background-image: repeating-radial-gradient(circle farthest-side at center, red 5%, yellow 10%, blue 15%);}
		.rg-db {background-image: repeating-radial-gradient(circle closest-corner at center, red 5%, yellow 10%, blue 15%);}
		.rg-dc {background-image: repeating-radial-gradient(circle closest-side at center, red 5%, yellow 10%, blue 15%);}
	</style>
</head>
<body>
	<div style="float: left;">
		<div style="overflow: hidden;">
			<div class="wrap a">
				<p>ellipse farthest-corner at center(默认)</p>
				<div class="box rg-aa"></div>
			</div>
			<div class="wrap a">
				<p>ellipse farthest-corner at top</p>
				<div class="box rg-ab"></div>
			</div>
			<div class="wrap a">
				<p>ellipse farthest-corner at right</p>
				<div class="box rg-ac"></div>
			</div>
		</div>
		<div style="overflow: hidden;">
			<div class="wrap b">
				<p>ellipse farthest-side at center</p>
				<div class="box rg-ba"></div>
			</div>
			<div class="wrap b">
				<p>ellipse closest-corner at center</p>
				<div class="box rg-bb"></div>
			</div>
			<div class="wrap b">
				<p>ellipse closest-side at center</p>
				<div class="box rg-bc"></div>
			</div>
		</div>
		<div style="overflow: hidden;">
			<div class="wrap c">
				<p>circle farthest-corner at center</p>
				<div class="box rg-ca"></div>
			</div>
			<div class="wrap c">
				<p>circle farthest-corner at top</p>
				<div class="box rg-cb"></div>
			</div>
			<div class="wrap c">
				<p>circle farthest-corner at right</p>
				<div class="box rg-cc"></div>
			</div>
		</div>
	</div>
	<div style="float: left;">
		<div class="wrap n-f">
			<p>circle farthest-side at center</p>
			<div class="box rg-da"></div>
		</div>
		<div class="wrap n-f">
			<p>circle closest-corner at center</p>
			<div class="box rg-db"></div>
		</div>
		<div class="wrap n-f">
			<p>circle closest-side at center</p>
			<div class="box rg-dc"></div>
		</div>
	</div>
</body>
</html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值