制作css 图标
使用 CSS 制作图标的方法
纯 CSS 绘制基础形状
通过 CSS 的 border、transform 和伪元素可以绘制简单图标,例如三角形、圆形或箭头:
.triangle {
width: 0;
height: 0;
border-left: 10px solid transparent;
border-right: 10px solid transparent;
border-bottom: 20px solid #3498db;
}
.circle {
width: 30px;
height: 30px;
border-radius: 50%;
background: #e74c3c;
}
.arrow::after {
content: "";
display: inline-block;
width: 8px;
height: 8px;
border-top: 2px solid #333;
border-right: 2px solid #333;
transform: rotate(45deg);
}
使用 CSS 伪元素组合复杂图标
通过 ::before 和 ::after 伪元素叠加创建更复杂的图形:

.menu-icon {
width: 20px;
height: 2px;
background: #333;
position: relative;
}
.menu-icon::before,
.menu-icon::after {
content: "";
position: absolute;
width: 20px;
height: 2px;
background: #333;
}
.menu-icon::before {
top: -6px;
}
.menu-icon::after {
top: 6px;
}
利用 box-shadow 创建多元素效果
单个元素配合 box-shadow 可生成重复图案:
.bars-icon {
width: 18px;
height: 2px;
background: #333;
box-shadow:
0 5px 0 #333,
0 10px 0 #333;
}
SVG 背景嵌入法
将 SVG 代码转为 Data URL 作为背景:

.search-icon {
width: 24px;
height: 24px;
background: url("data:image/svg+xml,%3Csvg viewBox='0 0 24 24'%3E%3Cpath d='M15.5 14h-.8l-.3-.3c1-1.1 1.6-2.6 1.6-4.2C16 5.9 13.1 3 9.5 3S3 5.9 3 9.5 5.9 16 9.5 16c1.6 0 3.1-.6 4.2-1.6l.3.3v.8l5 5 1.5-1.5-5-5zm-6 0C7 14 5 12 5 9.5S7 5 9.5 5 14 7 14 9.5 12 14 9.5 14z'/%3E%3C/svg%3E") no-repeat center;
}
动画图标实现
通过 CSS 动画让图标具有交互效果:
.loading-spinner {
width: 24px;
height: 24px;
border: 3px solid rgba(0,0,0,0.1);
border-radius: 50%;
border-top-color: #3498db;
animation: spin 1s linear infinite;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
响应式图标适配
使用 em 或 rem 单位使图标随字体大小缩放:
.adaptive-icon {
width: 1em;
height: 1em;
font-size: inherit;
vertical-align: middle;
}
注意事项
- 复杂图标建议使用 SVG 方案
- 移动端优先考虑矢量方案
- 高对比度模式下测试可访问性
- 使用 CSS 变量方便主题切换:
:root {
--icon-color: #2c3e50;
}
.icon {
color: var(--icon-color);
}






