作成方法:検索ボタン

CSSを使って検索ボタンを作成する方法を学ぶ。

全幅:

最も幅の広いフォーム内に中央寄せ:

自分で試してみる

検索ボタンを作成する方法

第1段 - 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="検索.." name="search">
  <button type="submit"><i class="fa fa-search"></i></button>
</form>

第2段 - 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;
}

自分で試してみる