【css】渐变

渐变是设置一种颜色或者多种颜色之间的过度变化。

两种渐变类型:

  • 线性渐变(向下/向上/向左/向右/对角线)

  • 径向渐变(由其中心定义)

1、线性渐变

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

其中direction是方向,值可以有bottom、right、top、left 。如果该参数参数值前面添加了to,则是渐变沿着相应的方向移动。如果省略了to,则为两个参数组合的方向,比如bottom right:右下角方向。

其中从上到下是默认的方向,可以不用添加direction参数。
1.1 默认方向

代码:

xml 复制代码
<head>
<style>
#grad1 {
  height: 200px;
  background-color: blue; /* 针对不支持渐变的浏览器 */
  background-image: linear-gradient(blue, yellow);
}
</style>
</head>
<body>

<div id="grad1"></div>

</body>

渲染效果:

1.2、从左到右

代码:

xml 复制代码
<head>
<style>
#grad1 {
  height: 200px;
  background-color: blue; /* 针对不支持渐变的浏览器 */
  background-image: linear-gradient(to right,blue, yellow);
}
</style>
</head>
<body>

<div id="grad1"></div>

</body>

渲染效果:

1.3 右下角

代码:

xml 复制代码
<style>
#grad1 {
  height: 200px;
  background-color: blue; /* 针对不支持渐变的浏览器 */
  background-image: linear-gradient(to bottom right,blue, yellow);
}
</style>
</head>
<body>

<div id="grad1"></div>

</body>

渲染效果:

2、径向渐变

径是指圆形的直径的径,以圆心为出发点,沿着径向渐变,和箭靶的效果差不多。

语法:

xml 复制代码
/* 在容器中心的渐变,从红色开始,变成蓝色,最后变成绿色 */
radial-gradient(circle at center, red 0, blue, green 100%)

边缘形状可以是圆形(circle)或椭圆形(ellipse)

形式语法

xml 复制代码
<radial-gradient()> = 
  radial-gradient( [ <ending-shape> || <size> ]? [ at <position> ]? , <color-stop-list> )  

<size> = 
  <extent-keyword>              |
  <length [0,∞]>                |
  <length-percentage [0,∞]>{2}  

<position> = 
  [ left | center | right ] || [ top | center | bottom ]  |
  [ left | center | right | <length-percentage> ] [ top | center | bottom | <length-percentage> ]?  |
  [ [ left | right ] <length-percentage> ] && [ [ top | bottom ] <length-percentage> ]  

<color-stop-list> = 
  <linear-color-stop> , [ <linear-color-hint>? , <linear-color-stop> ]#  

<extent-keyword> = 
  closest-corner   |
  closest-side     |
  farthest-corner  |
  farthest-side    

<length-percentage> = 
  <length>      |
  <percentage>  

<linear-color-stop> = 
  <color>               &&
  <length-percentage>?  

<linear-color-hint> = 
  <length-percentage>  

代码:

xml 复制代码
<style>
#grad1 {
  height: 200px;
  width:200px;
  background-color: blue; /* 针对不支持渐变的浏览器 */
  background-image: radial-gradient(circle at center,#33691e, yellow);
}
</style>
</head>
<body>

<div id="grad1"></div>

</body>

渲染效果:

关于径向渐变的详细介绍可以参考MDN Web Docs社区

相关推荐
AC赳赳老秦8 分钟前
外文文献精读:DeepSeek翻译并解析顶会论文核心技术要点
前端·flutter·zookeeper·自动化·rabbitmq·prometheus·deepseek
小宇的天下15 分钟前
Calibre 3Dstack --每日一个命令day18【floating_trace】(3-18)
服务器·前端·数据库
毕设源码-钟学长21 分钟前
【开题答辩全过程】以 基于web技术的酒店信息管理系统设计与实现-为例,包含答辩的问题和答案
前端
css趣多多29 分钟前
this.$watch
前端·javascript·vue.js
Code小翊33 分钟前
JS语法速查手册,一遍过JS
javascript
干前端1 小时前
Vue3虚拟滚动列表组件进阶:不定高度及原理分析!!!
前端·前端组件
子春一1 小时前
Flutter for OpenHarmony:构建一个 Flutter 天气卡片组件,深入解析动态 UI、响应式布局与语义化设计
javascript·flutter·ui
雨季6661 小时前
Flutter 三端应用实战:OpenHarmony “极简文本行数统计器”
开发语言·前端·flutter·ui·交互
MAHATMA玛哈特科技1 小时前
以曲求直:校平技术中的反直觉哲学
前端·数据库·制造·校平机·矫平机·液压矫平机
C澒1 小时前
前端技术核心领域与实践方向
前端·系统架构