css图制作
CSS 图形制作基础
CSS 图形制作主要通过 border、box-shadow、gradient 等属性结合伪元素实现。以下是一些常见图形的实现方法:
圆形
.circle {
width: 100px;
height: 100px;
border-radius: 50%;
background: #ff6b6b;
}
三角形 通过设置边框的透明部分实现:
.triangle {
width: 0;
height: 0;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
border-bottom: 100px solid #4ecdc4;
}
复杂图形实现
心形 结合伪元素和旋转变换:
.heart {
width: 50px;
height: 50px;
position: relative;
transform: rotate(-45deg);
}
.heart::before, .heart::after {
content: "";
width: 50px;
height: 50px;
border-radius: 50%;
background: #ff6b6b;
position: absolute;
}
.heart::before {
top: -25px;
left: 0;
}
.heart::after {
top: 0;
left: 25px;
}
对话框气泡
使用 clip-path 属性:
.bubble {
width: 150px;
height: 100px;
background: #70c1b3;
border-radius: 10px;
position: relative;
}
.bubble::after {
content: "";
position: absolute;
bottom: -10px;
left: 20px;
border-width: 10px 10px 0;
border-style: solid;
border-color: #70c1b3 transparent;
}
动画效果增强
为图形添加悬停动画:
.pulse-circle {
width: 80px;
height: 80px;
background: #f25f5c;
border-radius: 50%;
animation: pulse 1.5s infinite;
}
@keyframes pulse {
0% { transform: scale(0.95); box-shadow: 0 0 0 0 rgba(242,95,92,0.7); }
70% { transform: scale(1); box-shadow: 0 0 0 10px rgba(242,95,92,0); }
100% { transform: scale(0.95); box-shadow: 0 0 0 0 rgba(242,95,92,0); }
}
响应式图形技巧
使用 aspect-ratio 保持比例:
.responsive-shape {
width: 20vw;
aspect-ratio: 1/1;
background: #247ba0;
clip-path: polygon(50% 0%, 100% 50%, 50% 100%, 0% 50%);
}
高级技巧组合
渐变边框图形
.gradient-border {
width: 150px;
height: 150px;
border: 5px solid transparent;
border-radius: 50%;
background:
linear-gradient(white, white) padding-box,
linear-gradient(to right, #ff9a9e, #fad0c4) border-box;
}
这些方法可以通过组合和调整创造出各种复杂的视觉效果,CSS 图形的优势在于不需要额外加载图像资源,且易于修改和维护。






