conic-gradient用于创建围绕中心点旋转的圆弧形渐变,适合饼图、颜色轮等设计。其语法为background: conic-gradient(from 角度 at 位置, 颜色停靠点),支持精确角度控制和repeating-conic-gradient实现重复图案,常配合background-size等属性制作棋盘格等纹理。与linear-gradient(线性)、radial-gradient(径向)不同,conic-gradient沿圆周变化颜色。现代浏览器支持良好,可用@supports做兼容处理,静态使用性能优异,需注意可访问性中的颜色对比度问题。

CSS的
conic-gradient
说起
conic-gradient
linear
radial
conic-gradient
background: conic-gradient(from <angle> at <position>, <color-stop-list>);
from <angle>
0deg
90deg
at <position>
center
top left
50% 50%
<color-stop-list>
red
#f00
举个例子,如果我们想创建一个简单的四色饼图:
.pie-chart {
background: conic-gradient(
red 0deg 90deg, /* 红色从0度到90度 */
yellow 90deg 180deg, /* 黄色从90度到180度 */
blue 180deg 270deg, /* 蓝色从180度到270度 */
green 270deg 360deg /* 绿色从270度到360度 */
);
border-radius: 50%; /* 让它变成圆形 */
width: 200px;
height: 200px;
}这里我直接给出了每个颜色段的起始和结束角度,这样看起来就非常明确。当然,你也可以只给颜色,让浏览器自动分配空间,比如
conic-gradient(red, yellow, blue)
立即学习“前端免费学习笔记(深入)”;
你也可以只指定一个角度,让颜色从该角度开始。比如:
.simple-gradient {
background: conic-gradient(red 45deg, blue);
border-radius: 50%;
width: 200px;
height: 200px;
}这个例子里,红色会从0度持续到45度,然后蓝色从45度开始,一直到360度。这种方式更简洁,适合简单的颜色过渡。
我发现很多初学者会把这几个搞混,其实它们“渐变方向”的原理完全不同。
linear-gradient
background: linear-gradient(to right, red, blue);
radial-gradient
background: radial-gradient(circle at center, red, blue);
conic-gradient
conic-gradient
什么时候用哪个?
linear-gradient
radial-gradient
conic-gradient
要实现更复杂的
conic-gradient
repeating-conic-gradient()
实现环形条纹: 最直接的重复效果就是环形条纹。
.striped-circle {
background: repeating-conic-gradient(
#f00 0deg 10deg, /* 红色从0度到10度 */
#00f 10deg 20deg /* 蓝色从10度到20度 */
);
border-radius: 50%;
width: 200px;
height: 200px;
}这段代码会生成一个红蓝相间的环形条纹图案,因为渐变模式(红10度,蓝10度)会每隔20度重复一次。
实现棋盘格效果: 棋盘格的实现稍微有点技巧,因为它需要交错的方块。一种常见的做法是利用
repeating-conic-gradient
background-size
.checkerboard {
background: repeating-conic-gradient(
#eee 0% 25%, /* 浅色块占据前25%的扇形区域 */
#333 0% 50% /* 深色块也从0%开始,但占据前50%的扇形区域,覆盖了浅色块一部分 */
);
background-size: 50px 50px; /* 控制重复单元的大小,让它看起来像棋盘格 */
width: 200px;
height: 200px;
}这个棋盘格的例子可能有点反直觉,但它的原理是,
repeating-conic-gradient
#eee
#333
background-size
要理解这种复杂效果,核心在于:
repeating-conic-gradient
360deg
background-size
background-position
background-image
conic-gradient
有时候,你需要一点点试错才能得到想要的效果,这很正常。多尝试不同的角度和颜色组合,你会发现
conic-gradient
老实说,
conic-gradient
caniuse.com
兼容性问题及应对: 如果你的项目需要兼容IE这类老旧浏览器,那肯定得准备一个降级方案,因为它们是完全不支持
conic-gradient
@supports
.element-with-gradient {
background-color: #ccc; /* 降级方案:为不支持的浏览器提供一个纯色背景 */
/* 也可以使用一张预设的图片作为降级背景 */
/* background-image: url('fallback-gradient.png'); */
}
@supports (background: conic-gradient(red, blue)) {
.element-with-gradient {
background: conic-gradient(red, blue); /* 现代浏览器方案 */
}
}这样,不支持
conic-gradient
#ccc
性能考量: 至于性能,我个人觉得对于大多数非动画的、静态的
conic-gradient
conic-gradient
conic-gradient
可访问性: 更需要注意的是,如果你把
conic-gradient
以上就是css conic-gradient属性设置技巧的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号