CSSでモーダルを簡単に実装!:modal擬似クラスの解説とサンプルコード
modal 擬似クラスの利点
- 一貫性
モーダル要素のスタイルを統一的に定義できます。 - 保守性
コードが読みやすくなり、メンテナンスが容易になります。 - 簡潔性
JavaScriptによるクラス操作が不要になり、CSS コードが簡潔になります。
modal
擬似クラスの使用方法
modal
擬似クラスは、モーダル要素自体と、モーダル背景(オーバーレイ)をスタイリングするために使用できます。
- モーダル背景
dialog:modal::backdrop { /* モーダル背景のスタイル */ }
- モーダル要素
dialog:modal { /* モーダル要素のスタイル */ }
例
以下の例は、modal
擬似クラスを使用して、モーダルダイアログを中央揃えし、背景を半透明の黒色に設定する方法を示しています。
dialog:modal {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 80%;
max-width: 600px;
background-color: white;
padding: 20px;
border: 1px solid #ccc;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}
dialog:modal::backdrop {
background-color: rgba(0, 0, 0, 0.5);
}
modal
擬似クラスは、現時点ではすべてのブラウザでサポートされているわけではありません。- フルスクリーンモードで表示された要素も
modal
擬似クラスの対象となります。 modal
擬似クラスは、JavaScript でshowModal()
メソッドを使用して表示されたdialog
要素のみを対象とします。
HTML
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS :modal 擬似クラスを使用したモーダルウィンドウ</title>
<style>
/* モーダルダイアログ */
dialog:modal {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 80%;
max-width: 600px;
background-color: white;
padding: 20px;
border: 1px solid #ccc;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}
/* モーダル背景 */
dialog:modal::backdrop {
background-color: rgba(0, 0, 0, 0.5);
}
/* モーダルを開くボタン */
button {
padding: 10px 20px;
background-color: #007bff;
color: white;
border: none;
cursor: pointer;
}
</style>
</head>
<body>
<dialog id="my-modal">
<h2>モーダルウィンドウ</h2>
<p>ここにコンテンツを挿入します。</p>
<button onclick="document.getElementById('my-modal').close()">閉じる</button>
</dialog>
<button onclick="document.getElementById('my-modal').showModal()">モーダルを開く</button>
</body>
</html>
説明
このコードは、以下の機能を提供します。
- "モーダルを開く" ボタン
- 半透明の黒いモーダル背景
- 中央揃えされたモーダルダイアログ
使い方
- 上記のHTMLコードを保存します。
- Web ブラウザでファイルを開きます。
- "モーダルを開く" ボタンをクリックすると、モーダルウィンドウが表示されます。
- モーダルウィンドウを閉じるには、"閉じる" ボタンをクリックします。
カスタマイズ
上記のコードはあくまでも例であり、自由にカスタマイズできます。
- モーダルウィンドウの開閉時に JavaScript コードを実行できます。
- モーダルダイアログにコンテンツを追加できます。
- モーダルダイアログの色を変更できます。
- モーダルダイアログのサイズを変更できます。
代替方法
- JavaScript とクラス
従来の方法として、JavaScript でモーダル要素にクラスを付加し、CSS でそのクラスを対象にスタイルを定義する方法があります。この方法は、すべてのブラウザで動作し、柔軟性の高いスタイリングが可能です。
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>モーダルウィンドウ</title>
<style>
.modal {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 80%;
max-width: 600px;
background-color: white;
padding: 20px;
border: 1px solid #ccc;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
display: none; /* 初期状態では非表示 */
}
.modal.show {
display: block; /* JavaScript で show クラスを追加して表示 */
}
.modal-backdrop {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
display: none; /* 初期状態では非表示 */
}
.modal.show ~ .modal-backdrop {
display: block; /* モーダルが表示されたら背景を表示 */
}
</style>
</head>
<body>
<div id="my-modal" class="modal">
<h2>モーダルウィンドウ</h2>
<p>ここにコンテンツを挿入します。</p>
<button onclick="document.getElementById('my-modal').classList.remove('show')">閉じる</button>
</div>
<div class="modal-backdrop"></div>
<button onclick="document.getElementById('my-modal').classList.add('show')">モーダルを開く</button>
</body>
</html>
- CSS 属性と擬似要素
CSS の position
、top
、left
、width
、height
などの属性と、::backdrop
擬似要素を使用して、モーダルウィンドウをスタイリングする方法もあります。この方法は、JavaScript を必要とせず、比較的シンプルです。
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>モーダルウィンドウ</title>
<style>
#my-modal {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 80%;
max-width: 600px;
background-color: white;
padding: 20px;
border: 1px solid #ccc;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
display: none; /* 初期状態では非表示 */
}
#my-modal.show {
display: block; /* スタイルを変更して表示 */
}
#my-modal::backdrop {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
display: none; /* 初期状態では非表示 */
}
#my-modal.show ~ #my-modal::backdrop {
display: block; /* モーダルが表示されたら背景を表示 */
}
</style