モーダル
BootstrapのJavaScriptモーダルプラグインを使用して、ライトボックス、ユーザー通知、または完全にカスタムのコンテンツのためにダイアログをサイトに追加します。
動作の仕組み
Bootstrapのモーダルコンポーネントを使い始める前に、メニューオプションが最近変更されたため、以下を必ずお読みください。
- モーダルは、HTML、CSS、およびJavaScriptで構築されています。ドキュメント内の他のすべての上に配置され、
<body>
からスクロールを削除して、代わりにモーダルコンテンツがスクロールするようにします。 - モーダル「背景」をクリックすると、モーダルが自動的に閉じます。
- Bootstrapは、一度に1つのモーダルウィンドウのみをサポートします。ネストされたモーダルは、ユーザーエクスペリエンスが低いと考えるため、サポートされていません。
- モーダルは
position: fixed
を使用しますが、これはレンダリングについて少し特殊な場合があります。可能な場合は常に、他の要素からの干渉を避けるために、モーダルHTMLをトップレベルの位置に配置してください。.modal
を別の固定要素内にネストすると、問題が発生する可能性があります。 - 再度、
position: fixed
のため、モバイルデバイスでモーダルを使用する際にいくつかの注意点があります。詳細については、ブラウザサポートのドキュメントを参照してください。 - HTML5がそのセマンティクスを定義する方法により、
autofocus
HTML属性はBootstrapモーダルでは効果がありません。同じ効果を実現するには、カスタムJavaScriptを使用します。
const myModal = document.getElementById('myModal')
const myInput = document.getElementById('myInput')
myModal.addEventListener('shown.bs.modal', () => {
myInput.focus()
})
prefers-reduced-motion
メディアクエリに依存します。アクセシビリティドキュメントのモーションの削減セクションを参照してください。デモと使用ガイドラインについては、読み進めてください。
例
モーダルコンポーネント
以下は、静的なモーダルの例です(つまり、そのposition
とdisplay
が上書きされています)。モーダルヘッダー、モーダルボディ(padding
に必要)、およびモーダルフッター(オプション)が含まれています。可能な限り、破棄アクションを含むモーダルヘッダーを含めるか、別の明示的な破棄アクションを提供することを推奨します。
<div class="modal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Modal title</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<p>Modal body text goes here.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
<h5>
を使用しています。ただし、構造的には、モーダルダイアログは独自の別個のドキュメント/コンテキストを表すため、.modal-title
は理想的には<h1>
である必要があります。必要に応じて、フォントサイズユーティリティを使用して、ヘッダーの外観を制御できます。以下のすべてのライブ例では、このアプローチを使用しています。ライブデモ
下のボタンをクリックして、動作するモーダルデモを切り替えます。ページの上部からスライドダウンし、フェードインします。
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal">
Launch demo modal
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title fs-5" id="exampleModalLabel">Modal title</h1>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
静的背景
背景が静的に設定されている場合、モーダルの外側をクリックしてもモーダルは閉じません。下のボタンをクリックして試してみてください。
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#staticBackdrop">
Launch static backdrop modal
</button>
<!-- Modal -->
<div class="modal fade" id="staticBackdrop" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title fs-5" id="staticBackdropLabel">Modal title</h1>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Understood</button>
</div>
</div>
</div>
</div>
長いコンテンツのスクロール
モーダルがユーザーのビューポートまたはデバイスに対して長すぎる場合、ページ自体とは独立してスクロールします。以下のデモを試して、それが何を意味するかを確認してください。
.modal-dialog
に.modal-dialog-scrollable
を追加することで、モーダルボディをスクロールできるスクロール可能なモーダルを作成することもできます。
<!-- Scrollable modal -->
<div class="modal-dialog modal-dialog-scrollable">
...
</div>
垂直方向の中央配置
モーダルを垂直方向に中央配置するには、.modal-dialog
に.modal-dialog-centered
を追加します。
<!-- Vertically centered modal -->
<div class="modal-dialog modal-dialog-centered">
...
</div>
<!-- Vertically centered scrollable modal -->
<div class="modal-dialog modal-dialog-centered modal-dialog-scrollable">
...
</div>
ツールチップとポップオーバー
ツールチップとポップオーバーは、必要に応じてモーダル内に配置できます。モーダルが閉じられると、内部のツールチップとポップオーバーもすべて自動的に破棄されます。
<div class="modal-body">
<h2 class="fs-5">Popover in a modal</h2>
<p>This <button class="btn btn-secondary" data-bs-toggle="popover" title="Popover title" data-bs-content="Popover body content is set in this attribute.">button</button> triggers a popover on click.</p>
<hr>
<h2 class="fs-5">Tooltips in a modal</h2>
<p><a href="#" data-bs-toggle="tooltip" title="Tooltip">This link</a> and <a href="#" data-bs-toggle="tooltip" title="Tooltip">that link</a> have tooltips on hover.</p>
</div>
グリッドの使用
.modal-body
内に.container-fluid
をネストすることにより、モーダル内でBootstrapグリッドシステムを利用します。次に、他の場所と同じように、通常のグリッドシステムクラスを使用します。
<div class="modal-body">
<div class="container-fluid">
<div class="row">
<div class="col-md-4">.col-md-4</div>
<div class="col-md-4 ms-auto">.col-md-4 .ms-auto</div>
</div>
<div class="row">
<div class="col-md-3 ms-auto">.col-md-3 .ms-auto</div>
<div class="col-md-2 ms-auto">.col-md-2 .ms-auto</div>
</div>
<div class="row">
<div class="col-md-6 ms-auto">.col-md-6 .ms-auto</div>
</div>
<div class="row">
<div class="col-sm-9">
Level 1: .col-sm-9
<div class="row">
<div class="col-8 col-sm-6">
Level 2: .col-8 .col-sm-6
</div>
<div class="col-4 col-sm-6">
Level 2: .col-4 .col-sm-6
</div>
</div>
</div>
</div>
</div>
</div>
モーダルコンテンツの変更
すべて少し異なるコンテンツで同じモーダルをトリガーするボタンが多数ありますか?event.relatedTarget
とHTML data-bs-*
属性を使用して、クリックされたボタンに応じてモーダルのコンテンツを変更します。
以下はライブデモで、次にHTMLとJavaScriptの例が続きます。詳細については、relatedTarget
の詳細についてモーダルイベントドキュメントをお読みください。
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal" data-bs-whatever="@mdo">Open modal for @mdo</button>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal" data-bs-whatever="@fat">Open modal for @fat</button>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal" data-bs-whatever="@getbootstrap">Open modal for @getbootstrap</button>
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title fs-5" id="exampleModalLabel">New message</h1>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<form>
<div class="mb-3">
<label for="recipient-name" class="col-form-label">Recipient:</label>
<input type="text" class="form-control" id="recipient-name">
</div>
<div class="mb-3">
<label for="message-text" class="col-form-label">Message:</label>
<textarea class="form-control" id="message-text"></textarea>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Send message</button>
</div>
</div>
</div>
</div>
const exampleModal = document.getElementById('exampleModal')
if (exampleModal) {
exampleModal.addEventListener('show.bs.modal', event => {
// Button that triggered the modal
const button = event.relatedTarget
// Extract info from data-bs-* attributes
const recipient = button.getAttribute('data-bs-whatever')
// If necessary, you could initiate an Ajax request here
// and then do the updating in a callback.
// Update the modal's content.
const modalTitle = exampleModal.querySelector('.modal-title')
const modalBodyInput = exampleModal.querySelector('.modal-body input')
modalTitle.textContent = `New message to ${recipient}`
modalBodyInput.value = recipient
})
}
モーダル間の切り替え
data-bs-target
属性とdata-bs-toggle
属性を巧みに配置して、複数のモーダルを切り替えます。たとえば、すでに開いているサインインモーダル内からパスワードリセットモーダルを切り替えることができます。複数のモーダルを同時に開くことはできません—このメソッドは、2つの別々のモーダルを切り替えるだけです。
<div class="modal fade" id="exampleModalToggle" aria-hidden="true" aria-labelledby="exampleModalToggleLabel" tabindex="-1">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title fs-5" id="exampleModalToggleLabel">Modal 1</h1>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
Show a second modal and hide this one with the button below.
</div>
<div class="modal-footer">
<button class="btn btn-primary" data-bs-target="#exampleModalToggle2" data-bs-toggle="modal">Open second modal</button>
</div>
</div>
</div>
</div>
<div class="modal fade" id="exampleModalToggle2" aria-hidden="true" aria-labelledby="exampleModalToggleLabel2" tabindex="-1">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title fs-5" id="exampleModalToggleLabel2">Modal 2</h1>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
Hide this modal and show the first with the button below.
</div>
<div class="modal-footer">
<button class="btn btn-primary" data-bs-target="#exampleModalToggle" data-bs-toggle="modal">Back to first</button>
</div>
</div>
</div>
</div>
<button class="btn btn-primary" data-bs-target="#exampleModalToggle" data-bs-toggle="modal">Open first modal</button>
アニメーションの変更
$modal-fade-transform
変数は、モーダルのフェードインアニメーション前の .modal-dialog
の変換状態を決定し、$modal-show-transform
変数は、モーダルのフェードインアニメーション終了時の .modal-dialog
の変換状態を決定します。
たとえば、ズームインアニメーションが必要な場合は、$modal-fade-transform: scale(.8)
と設定できます。
アニメーションの削除
フェードインせずに単純に表示するモーダルの場合は、モーダルマークアップから .fade
クラスを削除します。
<div class="modal" tabindex="-1" aria-labelledby="..." aria-hidden="true">
...
</div>
動的な高さ
モーダルが開いている間に高さが変更された場合は、スクロールバーが表示された場合に備えて、myModal.handleUpdate()
を呼び出してモーダルの位置を再調整する必要があります。
アクセシビリティ
モーダルタイトルを参照する aria-labelledby="..."
を .modal
に必ず追加してください。さらに、.modal
の aria-describedby
でモーダルダイアログの説明を提供できます。JavaScript 経由で既に role="dialog"
を追加しているため、role="dialog"
を追加する必要はないことに注意してください。
YouTubeビデオの埋め込み
モーダルにYouTubeビデオを埋め込むには、自動的に再生を停止するための追加のJavaScriptが必要です。詳細は、この有益なStack Overflowの投稿を参照してください。
オプションのサイズ
モーダルには3つのオプションサイズがあり、.modal-dialog
に配置する修飾子クラスを介して利用できます。これらのサイズは、狭いビューポートで水平スクロールバーが発生するのを防ぐために、特定のブレークポイントで有効になります。
サイズ | クラス | モーダルの最大幅 |
---|---|---|
小 |
|
300px |
デフォルト | なし | 500px |
大 |
|
800px |
特大 |
|
1140px |
修飾子クラスのないデフォルトのモーダルは、「中」サイズのモーダルを構成します。
<div class="modal-dialog modal-xl">...</div>
<div class="modal-dialog modal-lg">...</div>
<div class="modal-dialog modal-sm">...</div>
フルスクリーンモーダル
もう1つのオーバーライドは、.modal-dialog
に配置する修飾子クラスを介して利用できる、ユーザービューポートを覆うモーダルをポップアップするオプションです。
クラス | 可用性 |
---|---|
|
常に |
|
576px |
|
768px |
|
992px |
|
1200px |
|
1400px |
<!-- Full screen modal -->
<div class="modal-dialog modal-fullscreen-sm-down">
...
</div>
CSS
変数
v5.2.0で追加Bootstrapの進化するCSS変数アプローチの一環として、モーダルは、リアルタイムのカスタマイズを強化するために、.modal
および .modal-backdrop
でローカルCSS変数を使用するようになりました。CSS変数の値はSassを介して設定されるため、Sassのカスタマイズも引き続きサポートされています。
--#{$prefix}modal-zindex: #{$zindex-modal};
--#{$prefix}modal-width: #{$modal-md};
--#{$prefix}modal-padding: #{$modal-inner-padding};
--#{$prefix}modal-margin: #{$modal-dialog-margin};
--#{$prefix}modal-color: #{$modal-content-color};
--#{$prefix}modal-bg: #{$modal-content-bg};
--#{$prefix}modal-border-color: #{$modal-content-border-color};
--#{$prefix}modal-border-width: #{$modal-content-border-width};
--#{$prefix}modal-border-radius: #{$modal-content-border-radius};
--#{$prefix}modal-box-shadow: #{$modal-content-box-shadow-xs};
--#{$prefix}modal-inner-border-radius: #{$modal-content-inner-border-radius};
--#{$prefix}modal-header-padding-x: #{$modal-header-padding-x};
--#{$prefix}modal-header-padding-y: #{$modal-header-padding-y};
--#{$prefix}modal-header-padding: #{$modal-header-padding}; // Todo in v6: Split this padding into x and y
--#{$prefix}modal-header-border-color: #{$modal-header-border-color};
--#{$prefix}modal-header-border-width: #{$modal-header-border-width};
--#{$prefix}modal-title-line-height: #{$modal-title-line-height};
--#{$prefix}modal-footer-gap: #{$modal-footer-margin-between};
--#{$prefix}modal-footer-bg: #{$modal-footer-bg};
--#{$prefix}modal-footer-border-color: #{$modal-footer-border-color};
--#{$prefix}modal-footer-border-width: #{$modal-footer-border-width};
--#{$prefix}backdrop-zindex: #{$zindex-modal-backdrop};
--#{$prefix}backdrop-bg: #{$modal-backdrop-bg};
--#{$prefix}backdrop-opacity: #{$modal-backdrop-opacity};
Sass変数
$modal-inner-padding: $spacer;
$modal-footer-margin-between: .5rem;
$modal-dialog-margin: .5rem;
$modal-dialog-margin-y-sm-up: 1.75rem;
$modal-title-line-height: $line-height-base;
$modal-content-color: null;
$modal-content-bg: var(--#{$prefix}body-bg);
$modal-content-border-color: var(--#{$prefix}border-color-translucent);
$modal-content-border-width: var(--#{$prefix}border-width);
$modal-content-border-radius: var(--#{$prefix}border-radius-lg);
$modal-content-inner-border-radius: subtract($modal-content-border-radius, $modal-content-border-width);
$modal-content-box-shadow-xs: var(--#{$prefix}box-shadow-sm);
$modal-content-box-shadow-sm-up: var(--#{$prefix}box-shadow);
$modal-backdrop-bg: $black;
$modal-backdrop-opacity: .5;
$modal-header-border-color: var(--#{$prefix}border-color);
$modal-header-border-width: $modal-content-border-width;
$modal-header-padding-y: $modal-inner-padding;
$modal-header-padding-x: $modal-inner-padding;
$modal-header-padding: $modal-header-padding-y $modal-header-padding-x; // Keep this for backwards compatibility
$modal-footer-bg: null;
$modal-footer-border-color: $modal-header-border-color;
$modal-footer-border-width: $modal-header-border-width;
$modal-sm: 300px;
$modal-md: 500px;
$modal-lg: 800px;
$modal-xl: 1140px;
$modal-fade-transform: translate(0, -50px);
$modal-show-transform: none;
$modal-transition: transform .3s ease-out;
$modal-scale-transform: scale(1.02);
Sassループ
レスポンシブフルスクリーンモーダルは、$breakpoints
マップと scss/_modal.scss
のループを介して生成されます。
@each $breakpoint in map-keys($grid-breakpoints) {
$infix: breakpoint-infix($breakpoint, $grid-breakpoints);
$postfix: if($infix != "", $infix + "-down", "");
@include media-breakpoint-down($breakpoint) {
.modal-fullscreen#{$postfix} {
width: 100vw;
max-width: none;
height: 100%;
margin: 0;
.modal-content {
height: 100%;
border: 0;
@include border-radius(0);
}
.modal-header,
.modal-footer {
@include border-radius(0);
}
.modal-body {
overflow-y: auto;
}
}
}
}
使用法
モーダルプラグインは、データ属性またはJavaScriptを介して、オンデマンドで非表示のコンテンツを切り替えます。また、デフォルトのスクロール動作をオーバーライドし、モーダルの外側をクリックしたときに表示されたモーダルを閉じるためのクリック領域を提供する .modal-backdrop
を生成します。
データ属性経由
切り替え
JavaScriptを記述せずにモーダルをアクティブにします。ボタンなどのコントローラー要素に data-bs-toggle="modal"
を設定し、切り替える特定のモーダルをターゲットにするために data-bs-target="#foo"
または href="#foo"
を設定します。
<button type="button" data-bs-toggle="modal" data-bs-target="#myModal">Launch modal</button>
閉じる
閉じる動作は、以下に示すように、モーダル内のボタンに data-bs-dismiss
属性を使用することで実現できます
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
または、以下に示すように、追加の data-bs-target
を使用して、モーダルの外側のボタンに設定することもできます
<button type="button" class="btn-close" data-bs-dismiss="modal" data-bs-target="#my-modal" aria-label="Close"></button>
JavaScript経由
1行のJavaScriptでモーダルを作成します
const myModal = new bootstrap.Modal(document.getElementById('myModal'), options)
// or
const myModalAlternative = new bootstrap.Modal('#myModal', options)
オプション
オプションはデータ属性またはJavaScriptを介して渡すことができるため、data-bs-animation="{value}"
のようにオプション名を data-bs-
に追加できます。データ属性を介してオプションを渡す場合は、オプション名のケースタイプを「camelCase」から「kebab-case」に変更してください。たとえば、data-bs-customClass="beautifier"
の代わりに data-bs-custom-class="beautifier"
を使用します。
Bootstrap 5.2.0以降、すべてのコンポーネントは、JSON文字列として単純なコンポーネント構成を格納できる実験的な予約済みデータ属性 data-bs-config
をサポートしています。要素に data-bs-config='{"delay":0, "title":123}'
および data-bs-title="456"
属性がある場合、最終的な title
値は 456
になり、個別のデータ属性が data-bs-config
で指定された値をオーバーライドします。さらに、既存のデータ属性は、data-bs-delay='{"show":0,"hide":150}'
のようにJSON値を格納できます。
最終的な構成オブジェクトは、data-bs-config
、data-bs-
、および js object
のマージされた結果であり、最後に指定されたキーと値が他の値をオーバーライドします。
名前 | 型 | デフォルト | 説明 |
---|---|---|---|
backdrop |
ブール値、'static' |
true |
modal-backdrop要素を含めます。または、クリック時にモーダルを閉じないbackdropの場合は static を指定します。 |
focus |
ブール値 | true |
初期化時にモーダルにフォーカスを当てます。 |
keyboard |
ブール値 | true |
Escキーを押すとモーダルを閉じます。 |
メソッド
オプションの引き渡し
コンテンツをモーダルとしてアクティブにします。オプションの object
を受け入れます。
const myModal = new bootstrap.Modal('#myModal', {
keyboard: false
})
メソッド | 説明 |
---|---|
dispose |
要素のモーダルを破棄します。(DOM要素に格納されたデータを削除します) |
getInstance |
静的 メソッド。DOM要素に関連付けられたモーダルインスタンスを取得できます。 |
getOrCreateInstance |
静的 メソッド。DOM要素に関連付けられたモーダルインスタンスを取得するか、初期化されていない場合は新しいインスタンスを作成できます。 |
handleUpdate |
モーダルが開いている間にモーダルの高さが変更された場合(つまり、スクロールバーが表示された場合)、モーダルの位置を手動で再調整します。 |
hide |
モーダルを手動で非表示にします。モーダルが実際に非表示になる前(つまり、hidden.bs.modal イベントが発生する前)に呼び出し元に返ります。 |
show |
モーダルを手動で開きます。モーダルが実際に表示される前(つまり、shown.bs.modal イベントが発生する前)に呼び出し元に返ります。また、モーダルイベントで(relatedTarget プロパティとして)受信できるDOM要素を引数として渡すこともできます。(例:const modalToggle = document.getElementById('toggleMyModal'); myModal.show(modalToggle) )。 |
toggle |
モーダルを手動で切り替えます。モーダルが実際に表示または非表示になる前(つまり、shown.bs.modal または hidden.bs.modal イベントが発生する前)に呼び出し元に返ります。 |
イベント
Bootstrapのモーダルクラスは、モーダル機能にフックするためのいくつかのイベントを公開しています。すべてのモーダルイベントは、モーダル自体(つまり、<div class="modal">
)で発生します。
イベント | 説明 |
---|---|
hide.bs.modal |
このイベントは、hide インスタンスメソッドが呼び出された直後に発生します。 |
hidden.bs.modal |
このイベントは、モーダルがユーザーから非表示になるのを完了したときに発生します(CSSトランジションが完了するのを待ちます)。 |
hidePrevented.bs.modal |
このイベントは、モーダルが表示され、そのbackdropが static であり、モーダルの外側をクリックしたときに発生します。このイベントは、Escキーが押され、keyboard オプションが false に設定されている場合にも発生します。 |
show.bs.modal |
このイベントは、show インスタンスメソッドが呼び出された直後に発生します。クリックによって引き起こされた場合、クリックされた要素はイベントの relatedTarget プロパティとして使用できます。 |
shown.bs.modal |
このイベントは、モーダルがユーザーに表示されたときに発生します(CSSトランジションが完了するのを待ちます)。クリックによって引き起こされた場合、クリックされた要素はイベントの relatedTarget プロパティとして使用できます。 |
const myModalEl = document.getElementById('myModal')
myModalEl.addEventListener('hidden.bs.modal', event => {
// do something...
})