当前位置:首页 > CSS

css制作搜索图标

2026-03-12 15:48:21CSS

使用伪元素创建搜索图标

通过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创建的图标添加悬停动画:

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;
}

标签: 图标css
分享给朋友:

相关文章

css制作导航条

css制作导航条

导航条的基本结构 使用HTML的<nav>标签和<ul>列表创建导航条的基本结构。导航项通常包含链接,示例代码如下: <nav> <ul>…

css 制作表单

css 制作表单

使用 CSS 制作表单 表单是网页交互的重要组成部分,通过 CSS 可以美化表单元素,提升用户体验。以下是常见的表单样式设计方法。 基础表单结构 HTML 表单通常包含输入框、标签、按钮等元素。以下…

css网页制作教程下载

css网页制作教程下载

CSS网页制作教程下载资源推荐 免费在线教程平台 W3Schools(www.w3schools.com/css/)提供交互式CSS教程,涵盖基础到高级内容,支持在线练习。 MDN Web Docs(…

css怎么制作三角形

css怎么制作三角形

使用边框制作三角形 通过设置元素的边框宽度和颜色,可以创建不同方向的三角形。将元素的宽度和高度设为0,利用边框的透明属性实现。 .triangle-up { width: 0; he…

css简历制作

css简历制作

使用CSS制作简历的基本方法 选择简洁的布局结构,单列或双列布局适合大多数简历。使用display: flex或display: grid实现响应式设计,确保在不同设备上显示良好。 字体选择上,标题…

div css制作

div css制作

使用 DIV 和 CSS 制作网页布局 DIV 是 HTML 中的块级元素,结合 CSS 可以灵活地创建各种网页布局。以下是常见的使用方法: 基础 DIV 结构 <div class="con…