Main content ah kal rawh Docs navigation ah kal rawh
in English

Modal a ni

Bootstrap-a JavaScript modal plugin hmangin i site-ah dialog dah la, lightbox, user notification emaw, custom content kimchang emaw a awm thei ang.

A hnathawh dan

Bootstrap-a modal component i hman tan hmain a hnuaia mi hi chhiar hmasa phawt ang che, tun hnaia kan menu option te a danglam tawh avangin.

  • Modal te hi HTML, CSS, leh JavaScript hmanga siam a ni. Document chhunga thil dang zawng zawng chungah an dah a, scroll atang chuan an paih chhuak a, <body>chutiang chuan modal content chu a scroll zawk ang.
  • Modal “backdrop” click chuan modal chu a khar nghal vek ang.
  • Bootstrap hian modal window pakhat chauh a support a. Nested modal te hi user experience tha lo tak an nih kan rin avangin an support lo.
  • Modals use position: fixed, chu chu a rendering chungchangah hian a chang chuan a particular deuh thei. A theih phawt chuan element dangte’n an tihbuai theih loh nan i modal HTML chu top-level position-ah dah la. .modalFixed element dang chhunga a nest hunah issue i tawk mai thei .
  • Vawi khat chu, due to position: fixed, mobile device-a modal hman chungchangah caveat thenkhat a awm bawk. A chipchiar zawkna chu kan browser support docs ah hian en rawh .
  • HTML5 hian a semantics a sawifiah dan avang hian HTML autofocusattribute hian Bootstrap modal-ah nghawng a nei lo. Chutiang bawka effect nei tur chuan custom JavaScript thenkhat hmang la:
var myModal = document.getElementById('myModal')
var myInput = document.getElementById('myInput')

myModal.addEventListener('shown.bs.modal', function () {
  myInput.focus()
})
He component hian animation effect hi prefers-reduced-motionmedia query ah a innghat a ni. Kan accessibility documentation a reduced motion tih hi en la .

Demo leh hman dan tur kaihhruaina chhiar chhunzawm zel ang che.

Entirna te

A hnuaia tarlan te hi static modal entirnan (a awmzia chu its positionand displayhave been overridden tihna a ni). A chhungah hian modal header, modal body (a mamawh tur padding), leh modal footer (optional) te pawh a tel. A theih phawt chuan dismiss action nen modal header te dah tel turin kan ngen a, a nih loh leh dismiss action chiang tak dang pe turin kan ngen bawk.

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

Live demo a ni ang

A hnuaia button hi click la, working modal demo chu toggle rawh. A hnuai lam a pan ang a, page chunglam atang chuan a fade in ang.

<!-- 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">
        <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
        <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>

Static backdrop a ni

Backdrop chu static-a dah a nih chuan modal chu a pawn lama click-naah a khar dawn lo. A hnuaia button hi click la, enchhin rawh.

<!-- 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">
        <h5 class="modal-title" id="staticBackdropLabel">Modal title</h5>
        <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>

Content thui tak tak scroll

Modal te chu user viewport emaw device emaw atan a sei lutuk chuan page ngei atanga innghat lovin an scroll thin. Kan sawi tum chu a hnuaia demo hi han en teh.

Scrollable modal pawh i siam thei a, chu chuan modal body chu scroll theihna a siam a, chu .modal-dialog-scrollablechu .modal-dialog.

<!-- Scrollable modal -->
<div class="modal-dialog modal-dialog-scrollable">
  ...
</div>

Vertically centered a ni

Add .modal-dialog-centeredto .modal-dialogah chuan modal chu vertically center ah dah rawh.

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

Tooltips leh popovers te pawh a awm bawk

Tooltip leh popover te chu a tul angin modal chhungah dah theih a ni. Modal khar a nih chuan a chhunga tooltip leh popover awm apiang chu automatic in a dismis vek bawk.

<div class="modal-body">
  <h5>Popover in a modal</h5>
  <p>This <a href="#" role="button" class="btn btn-secondary popover-test" title="Popover title" data-bs-content="Popover body content is set in this attribute.">button</a> triggers a popover on click.</p>
  <hr>
  <h5>Tooltips in a modal</h5>
  <p><a href="#" class="tooltip-test" title="Tooltip">This link</a> and <a href="#" class="tooltip-test" title="Tooltip">that link</a> have tooltips on hover.</p>
</div>

Grid hmangin

Bootstrap grid system chu modal chhungah hmang la .container-fluid, .modal-body. Tichuan, hmun dangah i tih ang bawkin grid system class pangngai te chu hmang ang che.

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

Modal content hrang hrang

Button bunch khat, modal khata trigger vek, content hrang hrang tlem nei? Use event.relatedTargetleh HTML data-bs-*attribute hmangin modal chhunga thu awmte chu eng button nge i click tih a zirin a danglam thei.

A hnuaiah hian live demo a awm a, chu chu entirnan HTML leh JavaScript a ni. Hriat belh duh chuan modal events docs chhiar la , 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">
        <h5 class="modal-title" id="exampleModalLabel">New message</h5>
        <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>
var exampleModal = document.getElementById('exampleModal')
exampleModal.addEventListener('show.bs.modal', function (event) {
  // Button that triggered the modal
  var button = event.relatedTarget
  // Extract info from data-bs-* attributes
  var 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.
  var modalTitle = exampleModal.querySelector('.modal-title')
  var modalBodyInput = exampleModal.querySelector('.modal-body input')

  modalTitle.textContent = 'New message to ' + recipient
  modalBodyInput.value = recipient
})

Modal hrang hrangah toggle theih a ni

data-bs-targetModal tam tak inkarah toggle la , leh data-bs-toggleattribute te chu clever placement engemaw zat nen . Entirnan, modal-a sign inhawng tawh chhung atang chuan password reset modal chu i toggle thei ang. Khawngaihin modal tam tak chu a rualin hawn theih a ni lo tih hre reng ang che —he method hian modal hrang hrang pahnih inkarah a toggle mai mai a ni.

Open hmasa ber modal
<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">
        <h5 class="modal-title" id="exampleModalToggleLabel">Modal 1</h5>
        <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">
        <h5 class="modal-title" id="exampleModalToggleLabel2">Modal 2</h5>
        <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>
<a class="btn btn-primary" data-bs-toggle="modal" href="#exampleModalToggle" role="button">Open first modal</a>

Animation thlak danglam rawh

Variable hian modal fade-in animation hmaa $modal-fade-transformtransform state a tichiang a, variable chuan modal fade-in animation tawp lamah transform of a ruat bawk..modal-dialog$modal-show-transform.modal-dialog

Entirnan zoom-in animation i duh chuan $modal-fade-transform: scale(.8).

Animation chu paih chhuak rawh

En tur fade in ai chuan modal lang mai mai tur tan chuan .fadei modal markup atang khan class chu paih chhuak rawh.

<div class="modal" tabindex="-1" aria-labelledby="..." aria-hidden="true">
  ...
</div>

Dynamic sang tak tak a awm

Modal hawn laiin a san zawng a inthlak myModal.handleUpdate()chuan scrollbar a rawn lang palh hlauh chuan modal awmna tur siamthat leh turin i call tur a ni.

A thlen theihna tur

aria-labelledby="...", modal title reference-in, -ah hian add ngei ang che .modal. Tin, i modal dialog chungchang sawifiahna chu aria-describedbyon hmangin i pe thei .modalbawk. role="dialog"JavaScript hmanga kan add tawh avangin add a ngai lo tih hre reng ang che .

YouTube video te embedding a ni

YouTube video modal-a embede tur chuan Bootstrap-a awm lo JavaScript dang a ngai a, chu chuan playback chu automatic-in a titawp thei a, thil dang tam tak a titawp thei bawk. Hrechiang duh tan he Stack Overflow post \angkai tak hi en la .

A duh duh size te pawh a awm bawk

Modal hian optional size pathum a nei a, modifier class kaltlangin a .modal-dialog. Heng size te hi breakpoint thenkhatah an kick in thin a, chu chuan viewport tawi zawka horizontal scrollbar awm loh nan.

Lenzawng tehna Pawl Modal max-a zau zawng a ni
Te .modal-sm 300px
Hlawhchhamna Pakhatmah 500px
Hrawl .modal-lg 800px
Extra lian tak a ni .modal-xl 1140px

Kan default modal awm lo modifier class hian “medium” size modal a siam a.

<div class="modal-dialog modal-xl">...</div>
<div class="modal-dialog modal-lg">...</div>
<div class="modal-dialog modal-sm">...</div>

Fullscreen Modal hmanga siam a ni

Override dang chu user viewport khuhtu modal pop up theihna option a ni a, chu chu a -a dah modifier class hmanga hmuh theih a .modal-dialogni.

Pawl A awm theihna tur
.modal-fullscreen Englaipawhin
.modal-fullscreen-sm-down Hnuai576px
.modal-fullscreen-md-down Hnuai768px
.modal-fullscreen-lg-down Hnuai992px
.modal-fullscreen-xl-down Hnuai1200px
.modal-fullscreen-xxl-down Hnuai1400px
<!-- Full screen modal -->
<div class="modal-dialog modal-fullscreen-sm-down">
  ...
</div>

Sass a ni

Variables te pawh a awm

$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:                  $white;
$modal-content-border-color:        rgba($black, .2);
$modal-content-border-width:        $border-width;
$modal-content-border-radius:       $border-radius-lg;
$modal-content-inner-border-radius: subtract($modal-content-border-radius, $modal-content-border-width);
$modal-content-box-shadow-xs:       $box-shadow-sm;
$modal-content-box-shadow-sm-up:    $box-shadow;

$modal-backdrop-bg:                 $black;
$modal-backdrop-opacity:            .5;
$modal-header-border-color:         $border-color;
$modal-footer-border-color:         $modal-header-border-color;
$modal-header-border-width:         $modal-content-border-width;
$modal-footer-border-width:         $modal-header-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-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);

Suih

Responsive fullscreen modals$breakpoints chu map leh loop in hmangin siam a scss/_modal.scssni.

@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 {
        @include border-radius(0);
      }

      .modal-body {
        overflow-y: auto;
      }

      .modal-footer {
        @include border-radius(0);
      }
    }
  }
}

Hman dan tur

Modal plugin hian i thup thup chu a mamawh angin a toggle a, data attribute emaw JavaScript hmangin a toggle thei bawk. Default scrolling behavior pawh a override a .modal-backdrop, modal pawn lama click hunah shown modals dismiss theihna tur click area pe turin a siam bawk.

Data attribute hmangin a ni

Toggle rawh

JavaScript ziak lovin modal pakhat activate rawh. data-bs-toggle="modal"Controller element, button ang chi, a data-bs-target="#foo"or href="#foo"toggle tur modal bik target tur nen set rawh .

<button type="button" data-bs-toggle="modal" data-bs-target="#myModal">Launch modal</button>

Hnawl

Dismissal hi a hnuaia tarlan ang hian modal chhungadata button pakhata attribute hmangin tih theih a ni :

<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>

emaw, a hnuaia tarlan ang hian modal pawn lama button-ah emaw data-bs-target:

<button type="button" class="btn-close" data-bs-dismiss="modal" data-bs-target="#my-modal" aria-label="Close"></button>

JavaScript hmangin

JavaScript line khat hmangin modal siam rawh:

var myModal = new bootstrap.Modal(document.getElementById('myModal'), options)

Thil thlan theih a ni

Options chu data attribute emaw JavaScript hmangin pass theih a ni. Data attribute atan chuan option hming chu , ah hian append la data-bs-, data-bs-backdrop="".

Hming Lampang Hlawhchhamna Hrilhfiahna
backdrop boolean emaw a string emaw a ni'static' true Modal-backdrop element a awm bawk. staticA nih loh leh, click-a modal khar lo backdrop atan specify bawk ang che.
keyboard boolean a ni true Escape key i hmet chuan modal a khar thin
focus boolean a ni true Initialized hunah modal ah focus a dah thin.

Thiltih dan tur

Asynchronous method leh inthlak danglamna te

API method zawng zawng hi asynchronous a ni a , transition a tan vek a ni . Transition an tan veleh mahse a tawp hmain call tu hnenah an kir leh thin . Chu bakah, transitioning component-a method call chu ngaihthah a ni ang .

Hriat belh duh chuan kan JavaScript documentation en rawh .

Passing option hrang hrang a awm

I content chu modal angin a activate thin. A pawm a, optional options a awm objectbawk .

var myModal = new bootstrap.Modal(document.getElementById('myModal'), {
  keyboard: false
})

toggle a ni

Modal pakhat chu manual in a toggle thin. Modal a lan tak tak hma emaw a thup hma emaw (chu chu shown.bs.modalor hidden.bs.modalevent a thlen hma) caller hnenah a kir leh thin.

myModal.toggle()

lantir

Manual takin modal a hawng thin. Modal a lan tak takshown.bs.modal hmain (chu chu event a thlen hmain ) caller hnenah a kir leh thin.

myModal.show()

Tin, DOM element chu modal events-a dawn theih tur argument atan i pass thei bawk ( relatedTargetproperty angin).

var modalToggle = document.getElementById('toggleMyModal') // relatedTarget
myModal.show(modalToggle)

biru

Manual takin modal a thup thin. Modal a thup tak tak hmain (chu chu hidden.bs.modalevent a thlen hma) caller hnenah a kir leh thin.

myModal.hide()

handleUpdate a ni

Modal hawn laiin a san zawng a inthlak chuan (chu chu scrollbar a rawn lang a nih chuan) modal awmna tur chu manual-in siam \ha leh rawh.

myModal.handleUpdate()

paih chhuak rawh

Element pakhat modal a tichhia. (DOM element-a data dahkhawmte a paih chhuak)

myModal.dispose()

GetInstance tih a ni

Static method hmanga DOM element nena inzawm modal instance hmuh theihna tur

var myModalEl = document.getElementById('myModal')
var modal = bootstrap.Modal.getInstance(myModalEl) // Returns a Bootstrap modal instance

GetOrCreateInstance tih theih a ni

Static method hmanga DOM element nena inzawm modal instance hmuh theihna tur, a nih loh leh initialized a nih loh chuan a thar siam theihna tur

var myModalEl = document.querySelector('#myModal')
var modal = bootstrap.Modal.getOrCreateInstance(myModalEl) // Returns a Bootstrap modal instance

Thil thlengte

Bootstrap-a modal class hian modal functionality-a hooking-na tur event tlemte a pholang a ni. Modal event zawng zawng hi modal ngeiah (ie at the <div class="modal">).

Event chi hrang hrang Hrilhfiahna
show.bs.modal showHe event hi instance method kan koh chuan a fire nghal vek a ni. Click avanga lo awm a nih chuan click element chu relatedTargetevent property atan a awm thei.
shown.bs.modal He event hi modal chu user hmuh theiha siam a nih hunah fire a ni (CSS transition zawh hun a nghak ang). Click avanga lo awm a nih chuan click element chu relatedTargetevent property atan a awm thei.
hide.bs.modal hideHe event hi instance method koh a nih chuan a fire nghal a ni.
hidden.bs.modal He event hi modal chu user hnen atanga thup a nih zawh chuan a fire thin (CSS transitions zawh hun a nghak ang).
hidePrevented.bs.modal He event hi modal a lan hian a fire a, a backdrop a nih staticchuan modal pawn lama click emaw escape key press emaw hmangin keyboard option emaw data-bs-keyboardset falseto
var myModalEl = document.getElementById('myModal')
myModalEl.addEventListener('hidden.bs.modal', function (event) {
  // do something...
})