css制作搜索框
基础搜索框样式
使用HTML和CSS创建一个简单的搜索框。HTML部分包含一个<input>元素和一个<button>元素,CSS部分用于美化样式。
<div class="search-box">
<input type="text" placeholder="搜索...">
<button type="submit">搜索</button>
</div>
.search-box {
display: flex;
width: 300px;
margin: 20px auto;
}
.search-box input {
flex: 1;
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px 0 0 4px;
outline: none;
}
.search-box button {
padding: 10px 15px;
background-color: #007bff;
color: white;
border: none;
border-radius: 0 4px 4px 0;
cursor: pointer;
}
.search-box button:hover {
background-color: #0056b3;
}
带图标的搜索框
通过CSS伪元素或背景图添加搜索图标,提升视觉体验。
<div class="search-box">
<input type="text" placeholder="搜索...">
<button type="submit"><i class="icon">🔍</i></button>
</div>
.search-box {
position: relative;
width: 300px;
margin: 20px auto;
}
.search-box input {
width: 100%;
padding: 10px 40px 10px 15px;
border: 1px solid #ddd;
border-radius: 20px;
outline: none;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
.search-box button {
position: absolute;
right: 10px;
top: 50%;
transform: translateY(-50%);
background: none;
border: none;
cursor: pointer;
color: #666;
}
动画效果搜索框
通过CSS过渡或动画实现交互效果,例如输入框展开或图标变色。
.search-box input {
transition: all 0.3s ease;
}
.search-box input:focus {
border-color: #007bff;
box-shadow: 0 0 8px rgba(0,123,255,0.2);
width: 350px;
}
.search-box button i {
transition: color 0.3s;
}
.search-box input:focus + button i {
color: #007bff;
}
响应式搜索框
使用媒体查询确保搜索框在不同设备上表现良好。
.search-box {
width: 80%;
max-width: 500px;
}
@media (max-width: 768px) {
.search-box {
width: 95%;
}
}
高级样式:浮动标签
通过CSS实现标签浮动效果,增强用户体验。

<div class="search-box">
<input type="text" id="search" required>
<label for="search">搜索内容</label>
<button>🔍</button>
</div>
.search-box {
position: relative;
margin: 20px auto;
width: 300px;
}
.search-box input {
width: 100%;
padding: 15px 10px;
border: 1px solid #ddd;
border-radius: 4px;
outline: none;
}
.search-box label {
position: absolute;
top: 15px;
left: 10px;
color: #999;
transition: all 0.3s;
}
.search-box input:focus + label,
.search-box input:valid + label {
top: -10px;
left: 5px;
background: white;
padding: 0 5px;
font-size: 12px;
color: #007bff;
}






