css制作球形
使用 CSS 制作球形
通过 CSS 的 border-radius 属性和一些阴影效果,可以轻松创建球形效果。以下是几种常见的方法:
基础球形
创建一个简单的球形,使用 border-radius: 50% 将正方形元素变为圆形,并通过 box-shadow 添加立体感:
.sphere {
width: 100px;
height: 100px;
border-radius: 50%;
background: radial-gradient(circle at 30% 30%, #ff0000, #990000);
box-shadow: inset 0 0 20px rgba(0, 0, 0, 0.5), 0 0 10px rgba(0, 0, 0, 0.3);
}
渐变球形
使用 radial-gradient 添加渐变效果,模拟光源照射的立体感:
.gradient-sphere {
width: 150px;
height: 150px;
border-radius: 50%;
background: radial-gradient(circle at 30% 30%, #ffffff, #3498db);
}
3D 球形
通过多重阴影和渐变增强 3D 效果:
.three-d-sphere {
width: 120px;
height: 120px;
border-radius: 50%;
background: radial-gradient(circle at 25% 25%, #ffffff, #e74c3c);
box-shadow: inset -10px -10px 20px rgba(0, 0, 0, 0.3), 5px 5px 15px rgba(0, 0, 0, 0.2);
}
动画球形
添加 CSS 动画让球形旋转或闪烁:
.animated-sphere {
width: 100px;
height: 100px;
border-radius: 50%;
background: radial-gradient(circle at 40% 40%, #ffffff, #2ecc71);
animation: pulse 2s infinite;
}
@keyframes pulse {
0% { transform: scale(1); }
50% { transform: scale(1.1); }
100% { transform: scale(1); }
}
使用伪元素增强效果
通过 ::before 或 ::after 添加高光或反射效果:
.highlight-sphere {
width: 100px;
height: 100px;
border-radius: 50%;
background: #3498db;
position: relative;
}
.highlight-sphere::before {
content: '';
position: absolute;
top: 10%;
left: 10%;
width: 20%;
height: 20%;
border-radius: 50%;
background: rgba(255, 255, 255, 0.6);
}
这些方法可以根据需求组合使用,调整颜色、大小和阴影参数以达到更逼真的球形效果。







