<button id="button-open">モーダルを開く</button>
<dialog id="my-modal">
<p>モーダルの中身です。</p>
<button id="button-close">モーダルを閉じる</button>
</dialog>
#my-modal:modal {
padding: 2.5rem;
border: 0;
border-radius: .5rem;
background-color: #fff;
box-shadow: 0 0 2rem .5rem rgba(0,0,0,.1);
}
#my-modal:modal p {
margin: .5rem 0 0;
color: #333;
font-size: 1.2rem;
line-height: 1.2;
font-weight: bold;
letter-spacing: .025em;
text-align: center;
}
#my-modal:modal button {
display: block;
width: 100%;
margin-top: 1rem;
border: 0;
padding: .9rem;
border-radius: .5rem;
background-color: dodgerblue;
color: white;
font-size: 1rem;
line-height: 1.2;
font-weight: bold;
letter-spacing: .025em;
}
#my-modal::backdrop {
background-color: rgba(255,255,255,.8);
}
const modal = document.querySelector('#my-modal')
const buttonOpen = document.querySelector('#button-open')
const buttonClose = document.querySelector('#button-close')
buttonOpen.addEventListener('click', () => {
modal.showModal()
})
buttonClose.addEventListener('click', () => {
modal.close()
})
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.