ダイアログにCSSスタイリングを追加する
このステップでは、ダイアログボックスにいくつかのCSSスタイリングを追加して、もっと見栄えが良くなるようにします。
<!doctype html>
<html>
<head>
<title>Modal Window using HTML dialog tag</title>
<!-- Adding styles for the dialog box -->
<style>
dialog {
width: 300px;
height: 150px;
background-color: #f2f2f2;
padding: 20px;
border: 2px solid #000;
border-radius: 10px;
box-shadow: 0px 0px 10px #000;
font-family: Arial, san-serif;
font-size: 18px;
color: #000;
text-align: center;
}
</style>
</head>
<body>
<h1>Modal Window using HTML dialog tag</h1>
<!-- Adding the dialog tag -->
<dialog id="dialog">
<p>This is a sample dialog box!</p>
</dialog>
<!-- Adding a button to open the dialog -->
<button onclick="document.getElementById('dialog').showModal()">
Open Dialog
</button>
<!-- Adding a button to close the dialog, which is hidden by default -->
<button onclick="document.getElementById('dialog').close()">
Close Dialog
</button>
</body>
</html>
これで、HTMLダイアログタグを使ってモーダルウィンドウを作成するためのHTMLコードは完成です。