W3.CSS モーダル

❮ 前章へ 次章へ ❯

W3.CSS モーダル

モーダルは、現在のページの最上部に表示されるダイアログ・ボックス/ポップアップウィンドウです:

×

Modal Header

Hello World!

Go back to W3.CSS Modal to learn more!


モーダルの作成方法

<!-- Trigger/Open the Modal -->
<button onclick="document.getElementById('id01').style.display='block'"
class="w3-btn">Open Modal</button>

<!-- The Modal -->
<div id="id01" class="w3-modal">
  <div class="w3-modal-content">
    <div class="w3-container">
      <span onclick="document.getElementById('id01').style.display='none'"
      class="w3-closebtn">&times;</span>
      <p>Some text in the Modal..</p>
      <p>Some text in the Modal..</p>
    </div>
  </div>
</div>
Try It Yourself ❯

"w3-modal" クラス

class="w3-modal" を持つ全ての HTML コンテナ (例えば <div>) をモーダルにすることができます。


"w3-modal-content" クラス

すべてのモーダル・コンテンツは、class="w3-modal-content" を持つ HTML コンテナに配置する必要があります。

モーダル・コンテンツは、全ての HTML 要素 (heading、paragraph、image、等) することができます。


モーダルを開く

モーダルを開くには、全ての HTML 要素を使用できますが、大抵はボタンまたはリンクを使用します。

onclick 属性を追加し、document.getElementById() メソッドを使用してモーダル id (この例では id01)をポイントして、"trigger" ボタンに一致するユニークな ID(id01)を指定します。


Closing a Modal

モーダルを閉じる

モーダルを閉じるには、モーダルの id (id01)を指す onclick 属性を持つ要素と一緒に w3-closebtn クラス を追加します。モーダルの外側をクリックしても閉じることができます(下の例を参照)

&times; (×) は、文字 "x" よりも閉じるボタンに適したアイコンの HTML エンティティです。


モーダルのヘッダー & フッター

class="w3-modal-content" を持つ <div> 内で、モーダルに異なったセクションを作るためには w3-container クラスを使用します:

×

Modal Header

Some text..

Some text..

<div id="id01" class="w3-modal">
  <div class="w3-modal-content">
    <header class="w3-container w3-teal">
      <span onclick="document.getElementById('id01').style.display='none'"
      class="w3-closebtn">&times;</span>
      <h2>Modal Header</h2>
    </header>
    <div class="w3-container">
      <p>Some text..</p>
      <p>Some text..</p>
    </div>
    <footer class="w3-container w3-teal">
      <p>Modal Footer</p>
    </footer>
  </div>
</div>
Try It Yourself ❯

カードのようなモーダル

カードのようなモーダルを表示するには、w3-modal-content コンテナに w3-card-* クラスを 追加します:

×

Modal Header

Some text..

Some text..

<div class="w3-modal-content w3-card-8">
Try It Yourself ❯

アニメ付きモーダルs

指定の方向からモーダルへスライドインさせるために、w3-animate-zoom|top|bottom|right|left クラスの いずれかを使用します:

×

Modal Header

Some text..

Some text..

×

Modal Header

Some text..

Some text..

×

Modal Header

Some text..

Some text..

×

Modal Header

Some text..

Some text..

×

Modal Header

Some text..

Some text..

×

Modal Header

Some text..

Some text..

×

Modal Header

Some text..

Some text..

<div class="w3-modal-content w3-animate-zoom">
<div class="w3-modal-content w3-animate-top">
<div class="w3-modal-content w3-animate-bottom">
<div class="w3-modal-content w3-animate-left">
<div class="w3-modal-content w3-animate-right">
<div class="w3-modal-content w3-animate-opacity">
Try It Yourself ❯

また、モーダルの背景色をフェードインすることもできます(w3-modal):

<div class="w3-modal w3-animate-opacity">
Try It Yourself ❯

モーダル画像

フルサイズで表示するには画像をクリックしてください:

Norway
×
Norway

<img src="img_fjords.jpg" onclick="document.getElementById('modal01').style.display='block'" class="w3-hover-opacity">

<div id="modal01" class="w3-modal w3-animate-zoom" onclick="this.style.display='none'">
  <img class="w3-modal-content" src="img_fjords.jpg">
</div>
Try It Yourself ❯

モーダル・イメージ・ギャラリー

フルサイズで表示するには画像をクリックしてください:

×

<div class="w3-row-padding">
  <div class="w3-container w3-third">
    <img src="img_fjords.jpg" style="width:100%" onclick="onClick(this)">
  </div>
  <div class="w3-container w3-third">
    <img src="img_lights.jpg" style="width:100%" onclick="onClick(this)">
  </div>
  <div class="w3-container w3-third">
    <img src="img_mountains.jpg" style="width:100%" onclick="onClick(this)">
  </div>
</div>

<div id="modal01" class="w3-modal" onclick="this.style.display='none'">
  <img class="w3-modal-content" id="img01" style="width:100%">
</div>

<script>
function onClick(element) {
  document.getElementById("img01").src = element.src;
  document.getElementById("modal01").style.display = "block";
}
</script>
Try It Yourself ❯

モーダル・ログイン・フォーム

この例では、ログイン用のモーダルを作成します:


× Avatar
Remember me
Forgot password?

Try It Yourself ❯

モーダル・タブ

この例では、タブ付きコンテンツを持つモーダルを作成します:

×

Header

London

London is the capital city of England. It is the most populous city in the United Kingdom, with a metropolitan area of over 13 million inhabitants.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

Paris

Paris is the capital of France.

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Tokyo

Tokyo is the capital of Japan.


Try It Yourself ❯

モーダルを閉じる

上の例では、モーダルを閉じるためにボタンを使用しています。しかし、わずかな JavaScript を使用すれば、 モーダルボックスの外側をクリックしたときにモーダルを閉じることができるようになります:

// Get the modal
var modal = document.getElementById('id01');

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
  if (event.target == modal) {
    modal.style.display = "none";
  }
}
Try It Yourself ❯

高度:ライトボックスモーダル

この例では、"lightbox" を作成するために、モーダル内の画像のスライドショーを使用する方法を示しています:

×

Nature and sunrise
Trolltunga, Norway
Mountains and fjords

Click on an image:
Try It Yourself ❯

チップ: スライドショーの詳細については、W3.CSS スライドショー の章をご覧ください。


❮ 前章へ 次章へ ❯