Modal W3.CSS


Modal adalah kotak dialog/jendela popup yang ditampilkan di atas halaman saat ini:

×

Modal Header

Hello World!

Go back to W3.CSS Modal to learn more!

Modal Footer Close


Kelas Modal W3.CSS

W3.CSS menyediakan kelas-kelas berikut untuk modal windows:

Kelas Mendefinisikan
w3-modal Wadah modal
w3-modal-konten Konten modal

Buat Modal

Kelas w3-modal mendefinisikan wadah untuk modal.

Kelas w3-modal-content mendefinisikan konten modal.

Konten modal dapat berupa elemen HTML apa pun (div, heading, paragraf, gambar, dll.).

Contoh

<!-- Trigger/Open the Modal -->
<button onclick="document.getElementById('id01').style.display='block'"
class="w3-button">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-button w3-display-topright">&times;</span>
      <p>Some text in the Modal..</p>
      <p>Some text in the Modal..</p>
    </div>
  </div>
</div>


Buka Modal

Gunakan elemen HTML apa pun untuk membuka modal. Namun, ini sering berupa tombol atau tautan.

Tambahkan atribut onclick dan arahkan ke id modal ( id01 dalam contoh kita), menggunakan metode document.getElementById() .


Tutup Modal

Untuk menutup modal, tambahkan kelas w3-button ke elemen bersama dengan atribut onclick yang menunjuk ke id modal ( id01 ). Anda juga dapat menutupnya dengan mengklik di luar modal (lihat contoh di bawah).

Kiat: × adalah entitas HTML yang disukai untuk ikon dekat, daripada huruf "x".


Modal Header dan Footer

Gunakan kelas w3-container untuk membuat bagian berbeda di dalam konten modal:

×

Modal Header

Some text..

Some text..

Modal Footer

Contoh

<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-button w3-display-topright">&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>

Modal Sebagai Kartu

Untuk menampilkan modal sebagai kartu, tambahkan salah satu kelas w3-card-* ke wadah w3-modal-content :

×

Modal Header

Some text..

Some text..

Modal Footer

Contoh

<div class="w3-modal-content w3-card-4">

Modal Animasi

Gunakan salah satu kelas w3-animate-zoom|top|bottom|right|left untuk meluncur di modal dari arah tertentu:

×

Modal Header

Some text..

Some text..

Modal Footer

×

Modal Header

Some text..

Some text..

Modal Footer

×

Modal Header

Some text..

Some text..

Modal Footer

×

Modal Header

Some text..

Some text..

Modal Footer

×

Modal Header

Some text..

Some text..

Modal Footer

×

Modal Header

Some text..

Some text..

Modal Footer

×

Modal Header

Some text..

Some text..

Modal Footer

Contoh

<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">

Anda juga dapat memudarkan warna latar belakang modal dengan menyetel kelas w3-animate-opacity pada elemen w3-modal:

Contoh

<div class="w3-modal w3-animate-opacity">

Gambar Modal

Klik pada gambar untuk menampilkannya sebagai modal, dalam ukuran penuh:

Norway
×
Norway

Contoh

<img src="img_snowtops.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_snowtops.jpg">
</div>

Galeri Gambar Modal

Klik pada gambar untuk menampilkannya dalam ukuran penuh:

×

Contoh

<div class="w3-row-padding">
  <div class="w3-container w3-third">
    <img src="img_snowtops.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>

Formulir Masuk Modal

Contoh ini membuat modal untuk login:


× Avatar
Remember me
Forgot password?

Contoh


Modal Dengan Konten Bertab

Contoh ini membuat modal dengan konten tab:

×

Header

London

London is the most populous city in the United Kingdom, with a metropolitan area of over 9 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.

Contoh


Close the Modal

In the examples above, we use a button to close the modal. However, with a little bit of JavaScript, you can also close the modal when clicking outside of the modal box:

Example

// 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";
  }
}

Advanced: Lightbox (Modal Image Gallery)

This example shows how to add an image slideshow inside a modal, to create a "lightbox":

×

Nature and sunrise

Nature and sunrise
French Alps
Mountains and fjords

Example

Click on an image:

Tip: To learn more about slideshows, visit our W3.CSS Slideshow chapter.