css制作icon
使用CSS制作图标的方法
纯CSS绘制基础形状
通过CSS的border、width、height等属性可以绘制简单图标:
.heart {
width: 20px;
height: 20px;
background-color: red;
transform: rotate(45deg);
position: relative;
}
.heart::before, .heart::after {
content: "";
width: 20px;
height: 20px;
background-color: red;
border-radius: 50%;
position: absolute;
}
.heart::before {
top: -10px;
left: 0;
}
.heart::after {
top: 0;
left: -10px;
}
利用伪元素增强效果
伪元素可扩展图标的复杂性:
.arrow {
width: 0;
height: 0;
border-left: 10px solid transparent;
border-right: 10px solid transparent;
border-bottom: 10px solid black;
position: relative;
}
.arrow::after {
content: "";
position: absolute;
border-left: 8px solid transparent;
border-right: 8px solid transparent;
border-bottom: 8px solid white;
top: 2px;
left: -8px;
}
CSS渐变创建复杂图案
线性渐变和径向渐变可生成更精细的图标:
![]()
.sun {
width: 30px;
height: 30px;
background: radial-gradient(circle, gold 30%, transparent 40%),
linear-gradient(0deg, gold 20%, transparent 20%),
linear-gradient(90deg, gold 20%, transparent 20%),
linear-gradient(180deg, gold 20%, transparent 20%),
linear-gradient(270deg, gold 20%, transparent 20%);
}
动画效果增强交互
添加悬停或点击动画:
.spinner {
width: 30px;
height: 30px;
border: 3px solid rgba(0,0,0,0.1);
border-radius: 50%;
border-top-color: blue;
animation: spin 1s linear infinite;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
使用CSS变量控制样式
便于主题切换和尺寸调整:
![]()
:root {
--icon-color: #3498db;
--icon-size: 24px;
}
.gear {
width: var(--icon-size);
height: var(--icon-size);
background: conic-gradient(
var(--icon-color) 0% 25%,
transparent 25% 50%,
var(--icon-color) 50% 75%,
transparent 75% 100%
);
border-radius: 50%;
}
响应式图标设计
结合媒体查询适应不同屏幕:
.menu-icon {
width: 20px;
height: 2px;
background: black;
position: relative;
}
.menu-icon::before, .menu-icon::after {
content: "";
position: absolute;
width: 20px;
height: 2px;
background: black;
}
.menu-icon::before { top: -6px; }
.menu-icon::after { bottom: -6px; }
@media (min-width: 768px) {
.menu-icon {
width: 30px;
}
.menu-icon::before, .menu-icon::after {
width: 30px;
}
}
组合多个元素创建复杂图标
构建多部分组成的图标:
.camera {
width: 40px;
height: 30px;
background: #333;
border-radius: 5px;
position: relative;
}
.camera::before {
content: "";
width: 15px;
height: 15px;
background: #555;
border-radius: 50%;
position: absolute;
top: 5px;
left: 12px;
}
.camera::after {
content: "";
width: 8px;
height: 5px;
background: #222;
position: absolute;
bottom: -5px;
left: 16px;
}






