icon制作css
使用 CSS 制作图标
通过 CSS 可以创建简单的矢量图标,通常利用伪元素、边框和变换属性实现。以下是几种常见方法:
纯 CSS 图标(如三角形)
.triangle {
width: 0;
height: 0;
border-left: 10px solid transparent;
border-right: 10px solid transparent;
border-bottom: 20px solid #333;
}
利用伪元素创建复杂形状
.heart::before, .heart::after {
content: "";
position: absolute;
width: 10px;
height: 16px;
background: red;
border-radius: 10px 10px 0 0;
}
.heart::before {
transform: rotate(-45deg) translateX(5px);
}
.heart::after {
transform: rotate(45deg) translateX(-5px);
}
使用 CSS 图标字体
更高效的方式是使用图标字体库,如 Font Awesome 或 Material Icons:
引入图标字体
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
使用方式
<i class="fas fa-user"></i>
<i class="fas fa-envelope"></i>
SVG 内联方案
对于更复杂的图标,推荐使用内联 SVG 配合 CSS 控制:
HTML 嵌入 SVG
<svg class="icon" viewBox="0 0 24 24">
<path d="M12 2L4 12l8 10 8-10z" fill="currentColor"/>
</svg>
CSS 控制样式
.icon {
width: 24px;
height: 24px;
color: #4285f4;
}
动画图标实现
通过 CSS 动画增强交互性:
![]()
.spinner {
width: 20px;
height: 20px;
border: 3px solid rgba(0,0,0,0.1);
border-radius: 50%;
border-top-color: #09f;
animation: spin 1s linear infinite;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
最佳实践取决于具体需求:简单形状可用纯 CSS,复杂图标建议采用 SVG 或图标字体方案。






