css制作搜索图标
使用伪元素创建搜索图标
通过CSS的::before或::after伪元素结合边框旋转可以绘制放大镜图标:
.search-icon {
position: relative;
width: 20px;
height: 20px;
}
.search-icon::before {
content: "";
position: absolute;
width: 12px;
height: 12px;
border: 2px solid #333;
border-radius: 50%;
left: 0;
top: 0;
}
.search-icon::after {
content: "";
position: absolute;
width: 2px;
height: 8px;
background: #333;
transform: rotate(-45deg);
right: 0;
bottom: 0;
}
使用背景渐变实现
CSS线性渐变可创建简洁的搜索图标:
.search-box {
width: 24px;
height: 24px;
background:
radial-gradient(circle, transparent 30%, #000 30%, #000 38%, transparent 38%),
linear-gradient(to bottom right, transparent 45%, #000 45%, #000 55%, transparent 55%);
transform: rotate(45deg);
}
SVG内联方案
直接在HTML中嵌入SVG并通过CSS控制样式:
<span class="search-svg">
<svg viewBox="0 0 24 24" width="24" height="24">
<path d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 1 0-.7.7l.27.28v.79l5 5 1.5-1.5-5-5zm-6 0a4.5 4.5 0 1 1 0-9 4.5 4.5 0 0 1 0 9z"/>
</svg>
</span>
.search-svg svg {
fill: currentColor;
transition: transform 0.2s;
}
.search-svg:hover svg {
transform: scale(1.1);
}
字体图标方案
使用现成的图标字体库(如Font Awesome):
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
<i class="fas fa-search"></i>
通过CSS自定义样式:
.fa-search {
color: #555;
font-size: 1.2rem;
transition: all 0.3s ease;
}
.fa-search:hover {
color: #000;
transform: rotate(10deg);
}
动画交互效果
为CSS创建的图标添加悬停动画:

.animated-search {
position: relative;
width: 24px;
height: 24px;
transition: all 0.5s;
}
.animated-search::before,
.animated-search::after {
content: "";
position: absolute;
transition: all 0.5s;
}
.animated-search::before {
width: 12px;
height: 12px;
border: 2px solid #3498db;
border-radius: 50%;
left: 0;
top: 0;
}
.animated-search::after {
width: 2px;
height: 8px;
background: #3498db;
transform: rotate(-45deg);
right: 3px;
bottom: 1px;
}
.animated-search:hover {
transform: scale(1.2);
}
.animated-search:hover::before {
border-color: #e74c3c;
}
.animated-search:hover::after {
background: #e74c3c;
}






