To create a modal, we use the <dialog> element, which is natively supported in most modern browsers. For more information, visit MDN.
Dialog modal
Add a unique id to the <dialog id="myModal"> HTML element, then use that id in JavaScript to call myModal.showModal() to open the modal and myModal.close() to close the modal.
The .modal-backdrop provides a clickable background that closes the modal when clicked outside the modal content.
html
<dialog class="modal" id="backdropModal">
<div class="modal-body">
<div class="flex justify-between items-center gap-3">
<div class="font-medium heading-lg">Dialog modal</div>
</div>
<div class="divider my-3"></div>
<p class="para">Press Esc key or click outside to close the modal</p>
</div>
<button type="button" class="modal-backdrop" onclick="backdropModal.close()">
Close Modal via Backdrop
</button>
</dialog>
<button type="button" class="btn btn-xs" onclick="backdropModal.showModal()">Show Modal</button>
Modal Slide Animations
Smoothly slide in and out from the top, bottom, start (left in LTR, right in RTL), or end (right in LTR, left in RTL) with the following directional utility classes: modal-slide-top, modal-slide-bottom, modal-slide-start, and modal-slide-end in the modal component. These animations improve the user experience through the use of dynamic transitions based on modal position.
Modal slide from start (Horizontal)
Add the modal-slide-start utility to slide the modal horizontally from the start side; in left-to-right (LTR) layouts, it appears from the left, and in right-to-left (RTL) layouts, from the right.
Add the modal-slide-end utility in modal component to slide the modal horizontally from the end side; in left-to-right (LTR) layouts, it appears from the right, and in right-to-left (RTL) layouts, from the left.
html
<dialog class="modal modal-x-center lg:modal-y-center modal-slide-end" id="slideEndModal">
<div class="modal-body">
<div class="flex justify-between items-center gap-3">
<div class="font-medium heading-lg">Slide end modal</div>
<button type="button" class="btn btn-xs btn-square btn-rounded btn-ghost" onclick="slideEndModal.close()">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewbox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M18 6 6 18"></path>
<path d="m6 6 12 12"></path>
</svg>
</button>
</div>
<div class="divider my-3"></div>
<p class="para">Horizontally slides the modal in and out from the end side.</p>
</div>
</dialog>
<button type="button" class="btn btn-xs" onclick="slideEndModal.showModal()">Show Modal</button>
The modal component is by default positioned in the center of the screen, but it can be placed depending upon the use case using utility classes such as modal-x-{position} and modal-y-{position}, where {position} is start, center, or end. Placement is designed with RTL and LTR compatibility in mind, making the modal adaptable to different language directions and layouts.
Use modal-x-{position} for horizontal placement and modal-y-{position} for vertical placement, where {position} can be start, center, or end. These classes are applied to the modal component.
Top left modal placement
html
<dialog class="modal modal-x-start modal-y-start" id="topLeftModal">
<div class="modal-body">
<div class="flex justify-between items-center gap-3">
<div class="font-medium heading-lg">Modal placement top left</div>
<button type="button" class="btn btn-xs btn-square btn-rounded btn-ghost" onclick="topLeftModal.close()">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewbox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M18 6 6 18"></path>
<path d="m6 6 12 12"></path>
</svg>
</button>
</div>
<div class="divider my-3"></div>
<p class="para">Modal placed at the top left corner of the screen</p>
</div>
<button type="button" class="modal-backdrop" onclick="topLeftModal.close()">
Close Modal via Backdrop
</button>
</dialog>
<button type="button" class="btn btn-xs" onclick="topLeftModal.showModal()">Show Modal</button>
Top center modal placement
html
<dialog class="modal modal-x-center modal-y-start" id="topCenterModal">
<div class="modal-body">
<div class="flex justify-between items-center gap-3">
<div class="font-medium heading-lg">Modal placement top center</div>
<button type="button" class="btn btn-xs btn-square btn-rounded btn-ghost" onclick="topCenterModal.close()">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewbox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M18 6 6 18"></path>
<path d="m6 6 12 12"></path>
</svg>
</button>
</div>
<div class="divider my-3"></div>
<p class="para">Modal placed at the top center corner of the screen</p>
</div>
<button type="button" class="modal-backdrop" onclick="topCenterModal.close()">
Close Modal via Backdrop
</button>
</dialog>
<button type="button" class="btn btn-xs" onclick="topCenterModal.showModal()">Show Modal</button>
Top right modal placement
html
<dialog class="modal modal-x-end modal-y-start" id="topEndModal">
<div class="modal-body">
<div class="flex justify-between items-center gap-3">
<div class="font-medium heading-lg">Modal placement top end</div>
<button type="button" class="btn btn-xs btn-square btn-rounded btn-ghost" onclick="topEndModal.close()">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewbox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M18 6 6 18"></path>
<path d="m6 6 12 12"></path>
</svg>
</button>
</div>
<div class="divider my-3"></div>
<p class="para">Modal placed at the top end corner of the screen</p>
</div>
<button type="button" class="modal-backdrop" onclick="topEndModal.close()">
Close Modal via Backdrop
</button>
</dialog>
<button type="button" class="btn btn-xs" onclick="topEndModal.showModal()">Show Modal</button>
Responsive positioning of modal for desktop and mobile screens
Depending on your use case, you can position the modal anywhere on desktop or mobile—for example, desktop center and mobile top center.
html
<dialog class="modal modal-x-center modal-y-start lg:modal-x-center lg:modal-y-center" id="responsiveModal">
<div class="modal-body">
<div class="flex justify-between items-center gap-3">
<div class="font-medium heading-lg">Responsive modal</div>
</div>
<div class="divider my-3"></div>
<p class="para">Resize the modal to a small screen to see the change in modal position.</p>
</div>
<button type="button" class="modal-backdrop" onclick="responsiveModal.close()">
Close Modal via Backdrop
</button>
</dialog>
<button type="button" class="btn btn-xs" onclick="responsiveModal.showModal()">Show Modal</button>
Modal positioning based on language direction
Place the modal wherever on the screen depending on the language direction (LTR or RTL).
Right-aligned and vertically centered in LTR and RTL
html
<dialog class="modal ltr:modal-x-end ltr:modal-y-center rtl:modal-x-start lg:modal-y-center" id="ltrModal">
<div class="modal-body">
<div class="flex justify-between items-center gap-3">
<div class="font-medium heading-lg">Responsive modal</div>
</div>
<div class="divider my-3"></div>
<p class="para">
Changing
<code>dir="rtl"</code>
in the HTML will have no effect on the modal's the position because the layout is controlled by utility classes:
<code>modal-x-end</code>
for the LTR variant and
<code>modal-x-start</code>
for the RTL variant.
</p>
</div>
<button type="button" class="modal-backdrop" onclick="ltrModal.close()">
Close Modal via Backdrop
</button>
</dialog>
<button type="button" class="btn btn-xs" onclick="ltrModal.showModal()">Show Modal</button>
Place the modal based on screen size and language direction.
html
<dialog class="modal ltr:modal-x-center lg:ltr:modal-x-start ltr:modal-y-start lg:ltr:modal-y-center rtl:modal-x-center lg:rtl:modal-x-start rtl:modal-y-end lg:rtl:modal-y-center" id="mixedModal">
<div class="modal-body">
<div class="flex justify-between items-center gap-3">
<div class="font-medium heading-lg">Responsive modal</div>
</div>
<div class="divider my-3"></div>
<p class="para">Change the screen and screen direction to see the change in modal position.</p>
</div>
<button type="button" class="modal-backdrop" onclick="mixedModal.close()">
Close Modal via Backdrop
</button>
</dialog>
<button type="button" class="btn btn-xs" onclick="mixedModal.showModal()">Show Modal</button>