Modal
✅ 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.
<dialog class="modal modal-x-center lg:modal-y-center modal-slide-right lg:modal-slide-down" id="toggleModal">
<div class="modal-body">
<div class="flex justify-between items-center gap-3">
<div class="font-medium heading-lg">Dialog modal</div>
<button type="button" class="btn btn-xs btn-square btn-rounded btn-ghost" onclick="toggleModal.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">Press Esc key or close button to close the modal</p>
<form method="dialog"> <input type="text"> <button type="button">submit</button> </form>
</div>
<button type="button" class="modal-backdrop" onclick="toggleModal.close()">Close Modal via Backdrop</button>
</dialog>
<button type="button" class="btn btn-xs" onclick="toggleModal.showModal()">Show Modal</button>
Close modal when click outside
The .modal-backdrop provides a clickable background that closes the modal when clicked outside the modal content.
<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.
Slide from Start (Horizontal): modal-slide-start
➡️ Direction: Slides in horizontally from the start side
- LTR: from the left
- RTL: from the right
<dialog class="modal modal-x-center lg:modal-y-center modal-slide-start" id="slideStartModal">
<div class="modal-body">
<div class="flex justify-between items-center gap-3">
<div class="font-medium heading-lg">Slide start modal</div>
<button type="button" class="btn btn-xs btn-square btn-rounded btn-ghost" onclick="slideStartModal.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 start side.</p>
</div>
</dialog>
<button type="button" class="btn btn-xs" onclick="slideStartModal.showModal()">Show Modal</button>
Slide from End (Horizontal): modal-slide-end
➡️ Direction: Slides in horizontally from the end side
- LTR: from the right
- RTL: from the left
<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>
Slide from Bottom (Vertical): modal-slide-up
➡️ Direction: Slides in vertically from the bottom
<dialog class="modal modal-x-center lg:modal-y-center modal-slide-up" id="slideUpModal">
<div class="modal-body">
<div class="flex justify-between items-center gap-3">
<div class="font-medium heading-lg">Slide up modal</div>
<button type="button" class="btn btn-xs btn-square btn-rounded btn-ghost" onclick="slideUpModal.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">Vertically slides the modal in and out from the bottom.</p>
</div>
</dialog>
<button type="button" class="btn btn-xs" onclick="slideUpModal.showModal()">Show Modal</button>
Slide from Top (Vertical): modal-slide-down
➡️ Direction: Slides in vertically from the top
<dialog class="modal modal-x-center lg:modal-y-center modal-slide-down" id="slideDownModal">
<div class="modal-body">
<div class="flex justify-between items-center gap-3">
<div class="font-medium heading-lg">Slide down modal</div>
<button type="button" class="btn btn-xs btn-square btn-rounded btn-ghost" onclick="slideDownModal.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">Vertically slides the modal in and out from the top.</p>
</div>
</dialog>
<button type="button" class="btn btn-xs" onclick="slideDownModal.showModal()">Show Modal</button>
Example usage of sliding animation modal for notification
<dialog class="modal modal-x-end modal-y-start modal-slide-end" id="notificationSlideModal">
<div class="modal-body">
<div class="flex justify-between items-center gap-3">
<div class="font-medium heading-lg">Notification modal</div>
<button type="button" class="btn btn-xs btn-square btn-rounded btn-ghost" onclick="notificationSlideModal.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>
</dialog>
<button type="button" class="btn btn-xs" onclick="notificationSlideModal.showModal()">Show Modal</button>
Modal placements
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
<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
<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
<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>
Center left modal placement
<dialog class="modal modal-x-start modal-y-center" id="startCenterModal">
<div class="modal-body">
<div class="flex justify-between items-center gap-3">
<div class="font-medium heading-lg">Modal placement left center</div>
<button type="button" class="btn btn-xs btn-square btn-rounded btn-ghost" onclick="startCenterModal.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 start center corner of the screen</p>
</div>
<button type="button" class="modal-backdrop" onclick="startCenterModal.close()">
Close Modal via Backdrop
</button>
</dialog>
<button type="button" class="btn btn-xs" onclick="startCenterModal.showModal()">Show Modal</button>
Center right modal placement
<dialog class="modal modal-x-end modal-y-center" id="endCenterModal">
<div class="modal-body">
<div class="flex justify-between items-center gap-3">
<div class="font-medium heading-lg">Modal placement right center</div>
<button type="button" class="btn btn-xs btn-square btn-rounded btn-ghost" onclick="endCenterModal.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 end center corner of the screen</p>
</div>
<button type="button" class="modal-backdrop" onclick="endCenterModal.close()">
Close Modal via Backdrop
</button>
</dialog>
<button type="button" class="btn btn-xs" onclick="endCenterModal.showModal()">Show Modal</button>
Bottom left modal placement
<dialog class="modal modal-x-start modal-y-end" id="bottomStartModal">
<div class="modal-body">
<div class="flex justify-between items-center gap-3">
<div class="font-medium heading-lg">Modal placement bottom start</div>
<button type="button" class="btn btn-xs btn-square btn-rounded btn-ghost" onclick="bottomStartModal.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 bottom start corner of the screen</p>
</div>
<button type="button" class="modal-backdrop" onclick="bottomStartModal.close()">
Close Modal via Backdrop
</button>
</dialog>
<button type="button" class="btn btn-xs" onclick="bottomStartModal.showModal()">Show Modal</button>
Bottom center modal placement
<dialog class="modal modal-x-center modal-y-end" id="bottomCenterModal">
<div class="modal-body">
<div class="flex justify-between items-center gap-3">
<div class="font-medium heading-lg">Modal placement bottom center</div>
<button type="button" class="btn btn-xs btn-square btn-rounded btn-ghost" onclick="bottomCenterModal.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 bottom center corner of the screen</p>
</div>
<button type="button" class="modal-backdrop" onclick="bottomCenterModal.close()">
Close Modal via Backdrop
</button>
</dialog>
<button type="button" class="btn btn-xs" onclick="bottomCenterModal.showModal()">Show Modal</button>
Bottom right modal placement
<dialog class="modal modal-x-end modal-y-end" id="bottomEndModal">
<div class="modal-body">
<div class="flex justify-between items-center gap-3">
<div class="font-medium heading-lg">Modal placement bottom center</div>
<button type="button" class="btn btn-xs btn-square btn-rounded btn-ghost" onclick="bottomEndModal.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 bottom center corner of the screen</p>
</div>
<button type="button" class="modal-backdrop" onclick="bottomEndModal.close()">
Close Modal via Backdrop
</button>
</dialog>
<button type="button" class="btn btn-xs" onclick="bottomEndModal.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.
<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 center on LTR and left center on RTL
<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">Change the dir="rtl" in html to see the change in modal position.</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.
<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-end 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>