この章では、CSS を使用して検索ボックスを作成する方法を学びます。

アイコンリソースには Font Awesome 4.7 バージョンのアイコンを使用します。

Font Awesome アイコンのチュートリアルは以下を参照してください:Font Awesome アイコン

サードパーティの CDN ライブラリを直接導入することもできます:

<link rel="stylesheet" href="../cdn.staticfile.org/font-awesome/4.7.0/css/font-awesome.css">

基本的な HTML コード

検索フォームの HTML コード

<!-- 検索フォーム -->
<form class="example" action="action_page.php">
  <input type="text" placeholder="検索.." 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; }

試してみる »

2 番目の検索ボックスの例

例

body{ background: #f2f2f2; font-family: 'Open Sans', sans-serif; } .search { width: 100%; position: relative; display: flex; } .searchTerm { width: 100%; border: 3px solid #00B4CC; border-right: none; padding: 5px; height: 20px; border-radius: 5px 0 0 5px; outline: none; color: #9DAEBF; } .searchTerm:focus{ color: #00B4CC; } .searchButton { width: 40px; height: 36px; border: 1px solid #00B4CC; background: #00B4CC; text-align: center; color: #fff; border-radius: 0 5px 5px 0; cursor: pointer; font-size: 20px; } /*サイズを変更して検索バーの変化を確認*/ .wrap{ width: 30%; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); }

試してみる »