CSS linear-gradient() 函数 详解
linear-gradient()
函数是 CSS 中用于创建线性渐变背景的函数。它可以在两个或多个颜色之间创建平滑过渡的效果。
linear-gradient()
函数的语法如下:
linear-gradient([<angle> | to <side-or-corner>], <color-stop1>, <color-stop2>, ...);
其中:
<angle>
:可选参数,表示渐变的角度。可以使用角度值(如45deg
)或关键字(如to top
、to bottom
、to left
、to right
等)。默认值是to bottom
,表示从上到下的垂直渐变。<side-or-corner>
:可选参数,表示渐变的方向。可以使用关键字top
、bottom
、left
、right
、top left
、top right
、bottom left
、bottom right
等。<color-stop>
:表示颜色的位置和值。可以是一个颜色值,也可以是一个带有位置的颜色值。位置可以是一个百分比值(如50%
)或一个长度值(如10px
)。
下面是一些示例:
- 垂直渐变:
background: linear-gradient(to bottom, red, blue);
这将创建一个从红色到蓝色的垂直渐变背景。
- 水平渐变:
background: linear-gradient(to right, red, blue);
这将创建一个从红色到蓝色的水平渐变背景。
- 对角线渐变:
background: linear-gradient(to bottom right, red, blue);
这将创建一个从红色到蓝色的对角线渐变背景。
- 自定义角度渐变:
background: linear-gradient(45deg, red, blue);
这将创建一个从红色到蓝色的 45 度角渐变背景。
- 使用颜色位置:
background: linear-gradient(to bottom, red, yellow 50%, green);
这将创建一个从红色到绿色的垂直渐变背景,其中黄色出现在 50% 的位置。
希望这个解释对你有帮助!