如何創建:搜索按鈕

學習如何使用 CSS 創建搜索按鈕。

全寬:

在具有最大寬度的表單內居中:

親自試一試

如何創建搜索按鈕

第一步 - 添加 HTML:

<!-- 加載圖標庫 -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<!-- 表單 -->
<form class="example" action="action_page.php">
  <input type="text" placeholder="Search.." name="search">
  <button type="submit"><i class="fa fa-search"></i></button>
</form>

第二步 - 添加 CSS:

* {
  box-sizing: border-box;
}
/* 設置搜索字段的樣式 */
form.example input[type=text] {
  padding: 10px;
  font-size: 17px;
  border: 1px solid grey;
  float: left;
  width: 80%;
  background: #f1f1f1;
}
/* 設置提交按鈕的樣式 */
form.example button {
  float: left;
  width: 20%;
  padding: 10px;
  background: #2196F3;
  color: white;
  font-size: 17px;
  border: 1px solid grey;
  border-left: none; /* 防止雙邊框 */
  cursor: pointer;
}
form.example button:hover {
  background: #0b7dda;
}
/* 清除浮動 */
form.example::after {
  content: "";
  clear: both;
  display: table;
}

親自試一試