HTML搜索框允许用户搜索内容。Search<input>元素的type属性的一个值。

语法

<input type="search">

示例:以下示例使用不同的CSS代码在网页上显示不同的搜索框。

示例 1:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>搜索框示例</title>
<style>
body {
  text-align: center;
  background-color: blue;
}
form {
  display: block;
  left: 30%;
  position: absolute;
  top: 30%;
}
input[type=search] {
  border: 5px orange;
  box-sizing: border-box;
  font-size: 1em;
  height: 2em;
  margin-left: 10vw;
  padding: .5em;
  transition: all 2s ease-in;
  width: 30vw;
  z-index: 1;
}
input[type=search]:focus {
  border: solid 3px #09f;
  outline: solid #fc0 2000px;
}
</style>
</head>
<body>
<form>
<input type="search" placeholder="Search for any site">
</form>
</body>
</html>

输出结果:

1.png

示例 2:在这个例子中,我们没有使用<input>元素的type属性的search值,而是使用CSS创建了一个文本输入框,它可以作为搜索字段使用。

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>搜索框示例</title>
<style>
* {
  outline: none;
}
form {
  height: 96px;
}
input[type="text"] {
  width: 100%;
  height: 80px;
  font-size: 40px;
  line-height: 1;
}
input[type="text"]::placeholder {
  color: orange;
}
.tb {
  display: table;
  width: 100%;
}
.td {
  display: table-cell;
  vertical-align: middle;
}
input,
button {
  color: #fff;
  font-family: Arial;
  padding: 0;
  margin: 0;
  border: 0;
  background-color: transparent;
}
#cover {
  position: absolute;
  top: 50%;
  width: 550px;
  padding: 35px;
  margin: -83px auto 0 auto;
  background-color: #ff7575;
  border-radius: 20px;
  left: 0;
  right: 0;
  box-shadow: 0 10px 40px #ff7c7c, 0 0 0 20px #ffffffeb;
  transform: scale(0.6);
}
button {
  position: relative;
  display: block;
  width: 84px;
  height: 96px;
  cursor: pointer;
}
#s-circle {
  position: relative;
  top: -8px;
  left: 0;
  width: 43px;
  height: 43px;
  margin-top: 0;
  border-width: 15px;
  border: 15px solid #fff;
  background-color: transparent;
  border-radius: 50%;
  transition: 0.5s ease all;
}
button span {
  position: absolute;
  top: 68px;
  left: 43px;
  display: block;
  width: 45px;
  height: 15px;
  background-color: transparent;
  border-radius: 10px;
  transform: rotateZ(52deg);
  transition: 0.5s ease all;
}
button span:before,
button span:after {
  content: '';
  position: absolute;
  bottom: 0;
  right: 0;
  width: 45px;
  height: 15px;
  background-color: #fff;
  border-radius: 10px;
  transform: rotateZ(0);
  transition: 0.5s ease all;
}
#s-cover:hover #s-circle {
  top: -1px;
  width: 67px;
  height: 15px;
  border-width: 0;
  background-color: #fff;
  border-radius: 20px;
}
#s-cover:hover span {
  top: 50%;
  left: 56px;
  width: 25px;
  margin-top: -9px;
  transform: rotateZ(0);
}
#s-cover:hover button span:before {
  bottom: 11px;
  transform: rotateZ(52deg);
}
#s-cover:hover button span:after {
  bottom: -11px;
  transform: rotateZ(-52deg);
}
#s-cover:hover button span:before,
#s-cover:hover button span:after {
  right: 6px;
  width: 40px;
  background-color: #fff;
}
</style>
</head>
<body>
<div id="cover">
<form method="get" action="">
<div class="tb">
<div class="td">
<input type="text" placeholder="Search" required>
</div>
<div class="td" id="s-cover">
<button type="submit">
<div id="s-circle"></div>
<span></span>
</button>
</div>
</div>
</form>
</div>
</body>
</html>

输出结果:

2.png

浏览器支持

元素chrome browser Chromeie browser IEfirefox browser Firefoxopera browser Operasafari browser Safari
搜索框

标签: html, HTML教程, HTML技术, HTML学习, HTML学习教程, HTML下载, HTML语言, HTML开发, HTML入门教程, HTML进阶教程, HTML面试题, HTML笔试题, HTML编程思想