2024-01-06 23:32:55
在CSS中,没有background-gradient属性,实现背景渐变需使用background或background-image配合gradient函数,具体分为以下三类:
1. 线性渐变(Linear Gradient)语法:
background: linear-gradient(direction, color-stop1, color-stop2, ...);默认从上到下:background: linear-gradient(red, blue);
指定方向:
水平方向:to right(从左到右)background: linear-gradient(to right, red, blue);
对角线方向:to bottom right(从左上到右下)background: linear-gradient(to bottom right, yellow, green);
角度控制:45deg(顺时针旋转45度)background: linear-gradient(45deg, red, orange, yellow);
语法:
background: radial-gradient(shape size at position, start-color, ..., last-color);圆形:circlebackground: radial-gradient(circle, red, yellow);
椭圆形:ellipsebackground: radial-gradient(ellipse, red, yellow);
通过repeating-linear-gradient或repeating-radial-gradient创建周期性图案:
控制渐变区域尺寸:background-size: 200px 200px;
禁止重复:background-repeat: no-repeat;
线性渐变的方向需明确(如to right或角度值),否则默认从上到下。
现代浏览器均支持gradient函数,但旧版本可能需要前缀(如-webkit-linear-gradient)。
复杂渐变可能影响渲染性能,避免在移动端过度使用。
掌握linear-gradient和radial-gradient即可满足大多数设计需求,结合颜色停靠点、方向与重复模式可实现丰富效果。