検索フォーム付きモーダルウインドウの作り方【HTML/CSS/JavaScript・サンプルコード付き】
モーダルウインドウ
検索フォーム付きモーダルウインドウの作り方
検索フォーム付きモーダルウインドウをHTML・CSS・JavaScriptで作る方法を紹介します。ボタンをクリックするとモーダルウインドウが開き、入力したキーワードでJavaScript内に用意した項目を絞り込めるサンプルです。検索画面を常時表示せず、必要なときだけ呼び出したい場合に利用できます。
このサンプルでは、モーダルの開閉、検索フォームの送信、キーワードに一致する項目の表示、該当項目がない場合のメッセージ表示を実装しています。商品一覧や記事一覧、管理画面、会員サイトなど、ページを移動せずに簡単な検索機能を提供したい場面に適しています。
現在のコードはJavaScript内のデータを検索するフロントエンド用サンプルです。PHPと組み合わせる場合は、フォームの送信先へ検索処理を行うPHPファイルを指定し、データベースやCSVなどから検索結果を取得する構成へ変更できます。モーダルの見た目や開閉処理はそのまま利用できます。
コードについて
本記事のコードはサンプルコードです。ご利用前に必ず動作確認を行ってください。
免責事項
本コードの利用により発生した損害について、当サイトは一切の責任を負いません。
デモ
「検索フォームを表示」をクリックし、「モーダル」「タブ」などのキーワードを入力して検索結果を確認できます。
コードをコピーして使おう!
<!DOCTYPE html>
<html lang="ja">
<head>
<!-- 文字コード -->
<meta charset="UTF-8">
<!-- スマートフォン表示対応 -->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- ページタイトル -->
<title>検索フォーム付きモーダルウインドウ</title>
<!-- CSSファイルを読み込み -->
<link rel="stylesheet" href="style.css">
</head>
<body>
<!--
==================================================
このサンプルについて
==================================================
このサンプルはHTML・CSS・JavaScriptのみで動作し、
JavaScript内に用意した項目をキーワードで絞り込みます。
PHPと組み合わせて実際の検索処理を行う場合
・form要素のaction属性へ検索処理を行うPHPを指定する
・method属性をgetまたはpostへ変更する
・検索入力欄のname="keyword"をPHPで受け取る
例
action="search.php"
method="get"
PHP側では検索キーワードの受け取り、
データベースやCSVからの検索、
検索結果の生成などを実装してください。
JavaScriptによるサンプル検索とPHP検索を
同時に使用しない場合は、
script.js内のフォーム送信処理を調整してください。
==================================================
-->
<!-- 検索モーダルを開く操作エリア -->
<div class="search-form-modal-window_wrap">
<!-- 操作エリアのタイトル -->
<div class="search-form-modal-window_title">
検索フォーム付きモーダル
</div>
<!-- 操作エリアの説明文 -->
<p class="search-form-modal-window_text">
ボタンをクリックすると検索フォーム付きモーダルウインドウを表示します。
</p>
<!-- 検索モーダルを開くボタン -->
<button
class="search-form-modal-window_open"
type="button"
data-search-form-modal-open>
検索フォームを表示
</button>
</div>
<!-- モーダル背景 -->
<div
class="search-form-modal-window_overlay"
data-search-form-modal-overlay>
<!-- モーダル本体 -->
<div
class="search-form-modal-window_modal"
role="dialog"
aria-modal="true"
aria-labelledby="search-form-modal-window-title">
<!-- モーダルを閉じるボタン -->
<button
class="search-form-modal-window_close"
type="button"
data-search-form-modal-close
aria-label="閉じる">
×
</button>
<!-- モーダルタイトル -->
<div
class="search-form-modal-window_heading"
id="search-form-modal-window-title">
サイト内検索
</div>
<!-- 検索フォーム -->
<form
class="search-form-modal-window_form"
action="#"
method="get"
data-search-form-modal-form>
<!-- 検索キーワード入力欄 -->
<input
class="search-form-modal-window_input"
type="search"
name="keyword"
placeholder="キーワードを入力"
aria-label="検索キーワード"
data-search-form-modal-input>
<!-- 検索ボタン -->
<button
class="search-form-modal-window_submit"
type="submit">
検索
</button>
</form>
<!-- JavaScriptで検索結果を追加する一覧 -->
<ul
class="search-form-modal-window_results"
data-search-form-modal-results>
</ul>
</div>
</div>
<!-- JavaScriptファイルを読み込み -->
<script src="script.js"></script>
</body>
</html>
/* ページ全体 */
body{
margin:0; /* ページ外側の余白をなくす */
padding:40px 20px; /* 上下40px・左右20pxの余白。値を大きくするとページ端の余白が広がる */
background:#ffffff; /* ページ全体の背景色 */
color:#111827; /* ページ全体の基本文字色 */
font-family:sans-serif; /* ページ全体のフォント */
box-sizing:border-box; /* paddingと枠線を要素サイズに含める */
}
/* 検索モーダルを開く操作エリア */
.search-form-modal-window_wrap{
max-width:700px; /* 最大横幅。値を大きくすると操作エリアが広がる */
margin:40px auto; /* 上下40pxの余白と左右中央配置 */
padding:32px; /* 内側余白。値を大きくすると内容周囲の余白が広がる */
border:1px solid #e5e7eb; /* 枠線の太さと色 */
border-radius:18px; /* 角丸。値を大きくすると丸みが強くなる */
background:#f8fafc; /* 操作エリアの背景色 */
text-align:center; /* タイトル・説明文・ボタンを中央揃え */
box-sizing:border-box; /* paddingと枠線を横幅に含める */
}
/* 操作エリアのタイトル */
.search-form-modal-window_title{
margin:0 0 12px; /* 下側12pxの余白 */
color:#111827; /* タイトルの文字色 */
font-size:22px; /* 文字サイズ。値を大きくするとタイトルが大きくなる */
font-weight:700; /* 文字の太さ */
line-height:1.5; /* 行間。値を大きくすると行間が広がる */
}
/* 操作エリアの説明文 */
.search-form-modal-window_text{
margin:0 0 24px; /* 下側24pxの余白 */
color:#4b5563; /* 説明文の文字色 */
font-size:16px; /* 説明文の文字サイズ */
line-height:1.8; /* 行間。値を大きくすると行間が広がる */
}
/* 検索モーダルを開くボタン */
.search-form-modal-window_open{
padding:12px 24px; /* ボタン内側の上下・左右余白。値を大きくするとボタンが大きくなる */
border:none; /* ボタンの枠線を非表示 */
border-radius:8px; /* ボタンの角丸。値を大きくすると丸みが強くなる */
background:#2563eb; /* ボタンの背景色 */
color:#ffffff; /* ボタンの文字色 */
font-size:16px; /* ボタンの文字サイズ */
font-weight:700; /* ボタン文字の太さ */
line-height:1.5; /* ボタン文字の行間 */
cursor:pointer; /* マウスカーソルを指型へ変更 */
}
/* モーダル背景 */
.search-form-modal-window_overlay{
display:none; /* 初期状態ではモーダルを非表示 */
position:fixed; /* モーダル背景を画面全体へ固定 */
inset:0; /* 上下左右を0にして画面全体へ広げる */
z-index:9999; /* 他の要素より前面へ表示 */
align-items:center; /* モーダル本体を縦中央へ配置 */
justify-content:center; /* モーダル本体を横中央へ配置 */
padding:20px; /* 画面端との余白。値を大きくすると端との間隔が広がる */
background:rgba(0,0,0,.6); /* 背景色。最後の.6を大きくすると暗く、小さくすると薄くなる */
box-sizing:border-box; /* paddingを画面幅に含める */
}
/* モーダル表示状態 */
.search-form-modal-window_overlay.is-active{
display:flex; /* 表示用クラスが付いた時にモーダルを表示 */
}
/* モーダル本体 */
.search-form-modal-window_modal{
position:relative; /* 閉じるボタンの絶対配置基準 */
width:100%; /* 画面幅に合わせて縮小 */
max-width:520px; /* 最大横幅。値を大きくするとモーダルが広がる */
max-height:90vh; /* 最大高さを画面高さの90%に制限 */
overflow-y:auto; /* 高さを超えた場合に縦スクロールを表示 */
padding:32px; /* 内側余白。値を大きくすると検索フォーム周囲の余白が広がる */
border-radius:18px; /* モーダル本体の角丸 */
background:#ffffff; /* モーダル本体の背景色 */
box-shadow:0 20px 50px rgba(0,0,0,.25); /* モーダルの影。数値を大きくすると影が広がる */
box-sizing:border-box; /* paddingを横幅に含める */
}
/* モーダルを閉じるボタン */
.search-form-modal-window_close{
position:absolute; /* モーダル右上へ固定配置 */
top:12px; /* 上端からの位置。値を大きくすると下へ移動する */
right:12px; /* 右端からの位置。値を大きくすると左へ移動する */
width:36px; /* ボタンの横幅。値を大きくするとボタンが大きくなる */
height:36px; /* ボタンの高さ。値を大きくするとボタンが大きくなる */
padding:0; /* ボタン内側の余白をなくす */
border:none; /* ボタンの枠線を非表示 */
border-radius:50%; /* ボタンを円形にする */
background:#111827; /* ボタンの背景色 */
color:#ffffff; /* ×印の文字色 */
font-size:22px; /* ×印の文字サイズ */
line-height:1; /* ×印の行間を詰める */
cursor:pointer; /* マウスカーソルを指型へ変更 */
}
/* モーダル内のタイトル */
.search-form-modal-window_heading{
margin:0 42px 24px; /* 左右42px・下24pxの余白 */
color:#111827; /* タイトルの文字色 */
font-size:24px; /* 文字サイズ。値を大きくするとタイトルが大きくなる */
font-weight:700; /* 文字の太さ */
line-height:1.5; /* 行間 */
text-align:center; /* 中央揃え */
}
/* 検索フォーム */
.search-form-modal-window_form{
display:flex; /* 入力欄と検索ボタンを横並びにする */
gap:10px; /* 入力欄と検索ボタンの間隔。値を大きくすると間隔が広がる */
}
/* 検索キーワード入力欄 */
.search-form-modal-window_input{
width:100%; /* 利用できる横幅いっぱいに表示 */
min-width:0; /* Flexbox内で入力欄がはみ出すのを防ぐ */
padding:12px 14px; /* 内側余白。値を大きくすると入力欄が高くなる */
border:1px solid #cbd5e1; /* 入力欄の枠線 */
border-radius:8px; /* 入力欄の角丸 */
background:#ffffff; /* 入力欄の背景色 */
color:#111827; /* 入力文字の色 */
font:inherit; /* ページと同じフォントを使用 */
font-size:16px; /* 入力文字のサイズ */
line-height:1.5; /* 入力文字の行間 */
box-sizing:border-box; /* paddingと枠線を横幅に含める */
}
/* 入力欄へフォーカスした時 */
.search-form-modal-window_input:focus{
border-color:#2563eb; /* フォーカス時の枠線色 */
outline:3px solid rgba(37,99,235,.15); /* フォーカス時の外側ライン */
}
/* 検索ボタン */
.search-form-modal-window_submit{
flex-shrink:0; /* ボタンが横幅不足で縮むのを防ぐ */
padding:12px 20px; /* ボタン内側の余白。値を大きくするとボタンが大きくなる */
border:none; /* ボタンの枠線を非表示 */
border-radius:8px; /* ボタンの角丸 */
background:#2563eb; /* ボタンの背景色 */
color:#ffffff; /* ボタンの文字色 */
font-size:16px; /* ボタンの文字サイズ */
font-weight:700; /* ボタン文字の太さ */
line-height:1.5; /* ボタン文字の行間 */
cursor:pointer; /* マウスカーソルを指型へ変更 */
}
/* 検索結果一覧 */
.search-form-modal-window_results{
display:grid; /* 検索結果を縦方向へ並べる */
gap:10px; /* 検索結果同士の間隔。値を大きくすると間隔が広がる */
margin:24px 0 0; /* 上側24pxの余白 */
padding:0; /* リスト内側の余白をなくす */
list-style:none; /* リスト記号を非表示 */
text-align:left; /* 検索結果を左揃え */
}
/* 検索結果の各項目 */
.search-form-modal-window_item{
padding:12px 14px; /* 項目内側の余白。値を大きくすると項目が大きくなる */
border:1px solid #e5e7eb; /* 項目の枠線 */
border-radius:8px; /* 項目の角丸 */
background:#f8fafc; /* 項目の背景色 */
color:#374151; /* 項目の文字色 */
font-size:15px; /* 項目の文字サイズ */
line-height:1.6; /* 項目の行間 */
}
/* 検索結果がない時のメッセージ */
.search-form-modal-window_empty{
margin:24px 0 0; /* 上側24pxの余白 */
padding:12px 14px; /* メッセージ内側の余白 */
border-radius:8px; /* メッセージの角丸 */
background:#fef2f2; /* メッセージの背景色 */
color:#b91c1c; /* メッセージの文字色 */
font-size:15px; /* メッセージの文字サイズ */
line-height:1.6; /* メッセージの行間 */
text-align:left; /* 左揃え */
}
/* 画面幅640px以下 */
@media (max-width:640px){
/* スマートフォン用の操作エリア */
.search-form-modal-window_wrap{
padding:24px 18px; /* 上下24px・左右18pxへ余白を縮小 */
}
/* スマートフォン用のモーダル本体 */
.search-form-modal-window_modal{
padding:48px 18px 20px; /* 閉じるボタン分の上余白と左右・下余白を設定 */
}
/* スマートフォン用の検索フォーム */
.search-form-modal-window_form{
flex-direction:column; /* 入力欄と検索ボタンを縦並びにする */
}
/* スマートフォン用の検索ボタン */
.search-form-modal-window_submit{
width:100%; /* ボタンをフォーム横幅いっぱいに表示 */
}
}
// 検索モーダルを開くボタンを取得
const searchFormModalWindowOpen=document.querySelector(
"[data-search-form-modal-open]"
);
// 検索モーダルを閉じるボタンを取得
const searchFormModalWindowClose=document.querySelector(
"[data-search-form-modal-close]"
);
// モーダル背景を取得
const searchFormModalWindowOverlay=document.querySelector(
"[data-search-form-modal-overlay]"
);
// 検索フォームを取得
const searchFormModalWindowForm=document.querySelector(
"[data-search-form-modal-form]"
);
// 検索入力欄を取得
const searchFormModalWindowInput=document.querySelector(
"[data-search-form-modal-input]"
);
// 検索結果表示エリアを取得
const searchFormModalWindowResults=document.querySelector(
"[data-search-form-modal-results]"
);
// デモ用検索データ
const searchFormModalWindowData=[
"モーダルウインドウ",
"アコーディオン",
"タブ切り替え",
"ドロワーメニュー",
"カルーセル",
"スライダー",
"ツールチップ",
"ポップオーバー"
];
// 検索結果を表示
function searchFormModalWindowShowResults(keyword){
// 検索結果を初期化
searchFormModalWindowResults.innerHTML="";
// 前後の空白を削除
const value=keyword.trim();
// キーワード未入力
if(value===""){
const item=document.createElement("li");
item.className="search-form-modal-window_empty";
item.textContent="検索キーワードを入力してください。";
searchFormModalWindowResults.appendChild(item);
return;
}
// 一致した項目を抽出
const result=searchFormModalWindowData.filter(function(text){
return text.includes(value);
});
// 一致しなかった場合
if(result.length===0){
const item=document.createElement("li");
item.className="search-form-modal-window_empty";
item.textContent="一致する項目は見つかりませんでした。";
searchFormModalWindowResults.appendChild(item);
return;
}
// 一致した項目を表示
result.forEach(function(text){
const item=document.createElement("li");
item.className="search-form-modal-window_item";
item.textContent=text;
searchFormModalWindowResults.appendChild(item);
});
}
// 開くボタンをクリックした時
searchFormModalWindowOpen.addEventListener("click",function(){
// モーダルを表示
searchFormModalWindowOverlay.classList.add("is-active");
// 検索欄へカーソルを移動
searchFormModalWindowInput.focus();
});
// 閉じるボタンをクリックした時
searchFormModalWindowClose.addEventListener("click",function(){
// モーダルを閉じる
searchFormModalWindowOverlay.classList.remove("is-active");
});
// 背景クリック時
searchFormModalWindowOverlay.addEventListener("click",function(event){
// 背景のみクリックした場合
if(event.target===searchFormModalWindowOverlay){
// モーダルを閉じる
searchFormModalWindowOverlay.classList.remove("is-active");
}
});
// 検索フォーム送信時
searchFormModalWindowForm.addEventListener("submit",function(event){
// ページ遷移を停止
event.preventDefault();
// 検索結果を表示
searchFormModalWindowShowResults(
searchFormModalWindowInput.value
);
});
ファイル構成と設置方法
このサンプルはHTML・CSS・JavaScriptの3ファイルで構成されています。HTMLには検索フォーム付きモーダルウインドウの構造、CSSにはフォームやモーダルのデザイン、JavaScriptにはモーダルの開閉処理と検索結果の表示処理を記述しています。ライブラリを使用せず、HTML・CSS・JavaScriptだけで動作するため、そのままコピーして利用できます。
PHPと組み合わせて実際の検索機能を作成する場合は、index.htmlのform要素に送信先PHPを指定し、method属性をgetまたはpostへ変更します。検索キーワードにはname属性が設定されているため、PHP側では検索文字列として受け取れます。データベース検索やCSV検索などの処理は、送信先PHPで別途実装してください。
このサンプルは検索フォーム付きモーダルウインドウの表示と検索画面のUIを確認するためのコードです。PHPと組み合わせる場合も、HTML・CSS・JavaScriptの構成はそのまま利用でき、フォームのaction属性とmethod属性を変更するだけで検索処理へ接続できます。
sample/
├── index.html
├── style.css
└── script.js
各ファイルの役割
index.html
検索フォーム付きモーダルウインドウのHTML構造を記述します。タイトル、説明文、検索フォーム、検索ボタン、検索結果表示エリアを配置しています。PHPと組み合わせる場合は、form要素のaction属性へ送信先PHPを指定し、method属性をgetまたはpostへ変更します。検索キーワードはname属性を利用してPHP側で受け取れます。
style.css
モーダルウインドウ全体のデザインを管理します。背景色、入力欄、検索ボタン、検索結果一覧、余白、角丸、影などを自由に変更できます。PHPと組み合わせた場合でもそのまま利用できます。
script.js
モーダルを開く処理、閉じる処理、背景クリックによる閉じる処理、デモ用検索結果の表示処理を管理します。実際の検索処理やデータ取得は行いません。
設置方法・使い方
sampleフォルダへindex.html、style.css、script.jsを配置します。sample/index.htmlをブラウザで開くと動作を確認できます。「検索フォームを表示」をクリックすると検索フォーム付きモーダルウインドウが表示されます。
PHPと組み合わせる場合は、index.htmlのform要素を次のように変更します。
action=”#”
method=”get”
を、
action=”search.php”
method=”get”
または
action=”search.php”
method=”post”
へ変更します。search.phpでは検索キーワードを受け取り、CSVやデータベースなどの検索処理を実装してください。
カスタマイズできる内容
index.htmlではタイトル、説明文、検索フォーム、プレースホルダー、検索ボタン、送信先PHPを変更できます。style.cssではモーダルの横幅、背景色、入力欄のサイズ、検索ボタン、検索結果一覧、余白、角丸、影などを変更できます。script.jsでは検索対象データや検索処理、モーダル開閉処理を編集できます。
数値調整
style.cssのmax-widthを変更するとモーダルの最大横幅が変わります。paddingを変更するとモーダル内部の余白が変わります。gapを変更するとフォーム項目同士の間隔が変わります。animation:0.3s;の0.3秒を変更するとモーダル表示アニメーションの速度が変わります。
レスポンシブ対応
style.cssではwidth:100%、max-width、padding、@mediaを使用してレスポンシブ対応しています。画面サイズに応じてモーダルや入力欄のサイズ、余白が自動調整されるため、スマートフォン・タブレット・PCでも利用できます。
検索フォーム付きモーダルウインドウの作り方【HTML/CSS/JavaScript・サンプルコード付き】
コメント