当前位置:首页 > CSS

css制作搜索框

2026-02-27 00:01:04CSS

基础搜索框样式

创建一个简单的搜索框需要结合HTML和CSS。HTML部分定义结构,CSS负责样式设计。

<input type="text" placeholder="搜索..." class="search-box">
.search-box {
  padding: 8px 15px;
  border: 1px solid #ddd;
  border-radius: 20px;
  outline: none;
  font-size: 14px;
  width: 200px;
  transition: all 0.3s ease;
}

.search-box:focus {
  border-color: #4a90e2;
  box-shadow: 0 0 5px rgba(74, 144, 226, 0.5);
}

带图标的搜索框

在搜索框内添加搜索图标可以提升视觉体验。使用背景图片或伪元素实现。

<div class="search-container">
  <input type="text" placeholder="搜索..." class="search-input">
  <button class="search-button">🔍</button>
</div>
.search-container {
  display: flex;
  border: 1px solid #ccc;
  border-radius: 20px;
  overflow: hidden;
  width: 300px;
}

.search-input {
  flex: 1;
  padding: 10px;
  border: none;
  outline: none;
}

.search-button {
  background: #4a90e2;
  color: white;
  border: none;
  padding: 10px 15px;
  cursor: pointer;
}

动画效果搜索框

为搜索框添加展开动画效果,增强交互体验。

<div class="animated-search">
  <input type="text" placeholder="搜索..." class="animated-input">
</div>
.animated-search {
  position: relative;
  width: 50px;
  height: 50px;
  transition: width 0.4s ease;
}

.animated-search:hover {
  width: 200px;
}

.animated-input {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  border: 2px solid #4a90e2;
  border-radius: 25px;
  padding: 0 20px;
  outline: none;
  font-size: 16px;
}

响应式搜索框

确保搜索框在不同设备上都能良好显示。

.responsive-search {
  width: 100%;
  max-width: 500px;
  margin: 0 auto;
  padding: 12px 20px;
  border: 1px solid #ddd;
  border-radius: 25px;
  font-size: 16px;
}

@media (max-width: 600px) {
  .responsive-search {
    padding: 10px 15px;
    font-size: 14px;
  }
}

高级搜索框样式

结合现代CSS特性创建更复杂的搜索框设计。

css制作搜索框

<div class="modern-search">
  <input type="search" placeholder="输入关键词..." required>
  <span class="search-border"></span>
</div>
.modern-search {
  position: relative;
  margin: 20px;
}

.modern-search input {
  width: 100%;
  padding: 12px;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  background-color: #f5f5f5;
  transition: all 0.3s;
}

.modern-search input:focus {
  background-color: white;
  box-shadow: 0 0 10px rgba(0,0,0,0.1);
}

.search-border {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 0;
  height: 2px;
  background-color: #4a90e2;
  transition: width 0.3s;
}

.modern-search input:focus ~ .search-border {
  width: 100%;
}

标签: css
分享给朋友:

相关文章

css 制作字体

css 制作字体

使用CSS自定义字体 在CSS中,可以通过@font-face规则引入自定义字体,并使用font-family属性应用这些字体。 @font-face { font-family: 'MyCus…

如何制作css

如何制作css

创建CSS文件 新建一个纯文本文件,将文件扩展名改为.css,例如style.css。确保文件名不含空格或特殊字符,使用连字符或下划线分隔单词。 编写CSS语法 CSS规则由选择器和声明块组成。选择…

css制作圆

css制作圆

使用CSS制作圆形元素 通过CSS的border-radius属性可以轻松将元素变为圆形。以下是具体实现方法: 基本圆形实现 .circle { width: 100px; height:…

css图形制作

css图形制作

CSS 图形制作方法 使用 CSS 可以创建各种基本和复杂的图形,主要通过 border、transform、clip-path 等属性实现。以下是几种常见图形的实现方式: 三角形 通过设置元素的…

怎么制作风水罗盘css

怎么制作风水罗盘css

制作风水罗盘CSS 使用CSS创建一个风水罗盘(八卦罗盘)需要结合HTML和CSS技术,通过层叠样式表绘制罗盘的各个部分。以下是实现方法: 基本结构 创建HTML结构,包含罗盘的外圆、内圆、八卦符号…

css 边框制作

css 边框制作

基础边框设置 使用 border 属性可快速设置边框样式、宽度和颜色: .element { border: 1px solid #000; /* 宽度 | 样式 | 颜色 */ } 样式选…