css 制作icon
使用CSS制作图标的方法
使用伪元素和边框
通过CSS的伪元素和边框属性可以创建简单的几何形状图标。例如制作一个三角形图标:
.triangle {
width: 0;
height: 0;
border-left: 10px solid transparent;
border-right: 10px solid transparent;
border-bottom: 20px solid #333;
}
利用transform旋转
结合transform属性可以制作更复杂的形状,比如旋转矩形形成菱形:
.diamond {
width: 20px;
height: 20px;
background-color: #333;
transform: rotate(45deg);
}
线性渐变背景
使用CSS渐变可以创建特殊形状的图标,比如圆形箭头:
.arrow-circle {
width: 24px;
height: 24px;
border-radius: 50%;
background: linear-gradient(135deg, transparent 45%, #333 45%, #333 55%, transparent 55%);
}
组合多个元素
通过组合多个HTML元素和CSS样式可以创建更复杂的图标:
<div class="menu-icon">
<span></span>
<span></span>
<span></span>
</div>
.menu-icon {
display: flex;
flex-direction: column;
gap: 5px;
}
.menu-icon span {
width: 24px;
height: 3px;
background-color: #333;
}
使用box-shadow
利用box-shadow属性可以创建多个形状而无需额外HTML元素:
.bars-icon {
position: relative;
width: 24px;
height: 18px;
}
.bars-icon::before {
content: "";
position: absolute;
width: 24px;
height: 2px;
background: #333;
box-shadow: 0 8px 0 #333, 0 16px 0 #333;
}
SVG与CSS结合
虽然SVG本身不是纯CSS,但可以配合CSS样式使用:
<svg class="heart-icon" viewBox="0 0 24 24">
<path d="M12 21.35l-1.45-1.32C5.4 15.36 2 12.28 2 8.5 2 5.42 4.42 3 7.5 3c1.74 0 3.41.81 4.5 2.09C13.09 3.81 14.76 3 16.5 3 19.58 3 22 5.42 22 8.5c0 3.78-3.4 6.86-8.55 11.54L12 21.35z"/>
</svg>
.heart-icon {
width: 24px;
height: 24px;
fill: #ff0000;
transition: fill 0.3s;
}
.heart-icon:hover {
fill: #cc0000;
}
动画效果
为CSS图标添加交互效果:
.loading-spinner {
width: 24px;
height: 24px;
border: 3px solid rgba(0,0,0,0.1);
border-radius: 50%;
border-top-color: #333;
animation: spin 1s linear infinite;
}
@keyframes spin {
to { transform: rotate(360deg); }
}![]()





