W3.CSS Modalne


Modalny to okno dialogowe/wyskakujące okienko, które jest wyświetlane na górze bieżącej strony:

×

Modal Header

Hello World!

Go back to W3.CSS Modal to learn more!

Modal Footer Close


W3.Klasy modalne CSS

W3.CSS udostępnia następujące klasy dla okien modalnych:

Klasa Definiuje
w3-modalny Kontener modalny
w3-modal-treść Treść modalna

Utwórz modalny

Klasa w3-modal definiuje kontener dla modu.

Klasa w3-modal-content definiuje zawartość modalną.

Treścią modalną może być dowolny element HTML (elementy div, nagłówki, akapity, obrazy itp.).

Przykład

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


Otwórz modalne

Użyj dowolnego elementu HTML, aby otworzyć modalny. Często jest to jednak przycisk lub link.

Dodaj atrybut onclick i wskaż identyfikator modułu modalnego ( w naszym przykładzie id01 ), używając metody document.getElementById().


Zamknij modal

Aby zamknąć modalny, dodaj klasę przycisku w3 do elementu wraz z atrybutem onclick, który wskazuje na id modalnego ( id01 ). Możesz go również zamknąć, klikając poza modem (patrz przykład poniżej).

Wskazówka: × to preferowana jednostka HTML dla zamykających się ikon, a nie litera „x”.


Modalny nagłówek i stopka

Użyj klas kontenerów w3 , aby utworzyć różne sekcje w treści modalnej:

×

Modal Header

Some text..

Some text..

Modal Footer

Przykład

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

Modalne jako karta

Aby wyświetlić modalny jako kartę, dodaj jedną z klas w3-card-* do kontenera w3-modal-content :

×

Modal Header

Some text..

Some text..

Modal Footer

Przykład

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

Animowane Modale

Użyj dowolnej z klas w3-animate-zoom|top|bottom|right|left , aby przesunąć się w trybie modalnym z określonego kierunku:

×

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

Przykład

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

Możesz również zanikać w kolorze tła modalu, ustawiając klasę w3-animate-opacity na elemencie w3-modal:

Przykład

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

Obraz modalny

Kliknij obraz, aby wyświetlić go jako modalny w pełnym rozmiarze:

Norwegia
×
Norway

Przykład

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

Galeria zdjęć modalnych

Kliknij obraz, aby wyświetlić go w pełnym rozmiarze:

×

Przykład

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

Formularz logowania modalnego

Ten przykład tworzy modalny do logowania:


× Avatar
Remember me
Forgot password?

Przykład


Modalne z zawartością na kartach

Ten przykład tworzy modalny z zawartością na kartach:

×

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.

Przykład


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.