The showModal() method of the HTMLDialogElement interface displays the dialog as a modal, over the top of any other dialogs that might be present. It displays into the top layer, along with a ::backdrop pseudo-element. Interaction outside the dialog is blocked and the content outside it is rendered inert. MDN documentation
As you can see, outside of the dialog all other html nodes will be blocked. But, we can add a child node as a wrapper for our content, thus we separate our content from the outside area. Let's remove the extra padding from dialog with css and add it to the modal class.
const dialog = document.getElementById('myDialog');
const button = document.querySelector('button');
function openDialog() {
dialog.showModal();
}
function closeDialog(event) {
// If the target dialog is
if (!event.target.contains(dialog)) return;
dialog.close();
}
button.addEventListener('click', openDialog);
document.addEventListener('click', closeDialog);
dialog {
padding: 0;
}
.modal {
display: flex;
padding: 1rem;
}
dialog::backdrop {
background-color: rgba(255, 0, 0, 0.25);
}
<p>Click the button to show the dialog.</p>
<button>Show dialog</button>
<p><b>Note:</b> Use the "Esc" button to close the modal.</p>
<p><b>Note:</b> The dialog element is only supported in Chrome 37+, Safari 6+ and Opera 24+.</p>
<dialog id="myDialog">
<div class="modal">
<p>This is a dialog window</p>
</div>
</dialog>