HTML element dialog

by mdvanes

HTML

<h1>The Dialog Element</h1>

<button id="show">Open the Dialog</button>

<p>Experimental element, only (partially) supported by Chrome 37+. Demo based on <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dialog">this MDN example</a>.</p>

<p><a href="http://blog.teamtreehouse.com/a-preview-of-the-new-dialog-element">This article</a> was also used for inspiration, as it explains that the ::backdrop pseudo-element only works on dialogs launched using showModal(). The alternative, to open the dialog from the HTML, can be achieved by adding the "open" attribute to the dialog element.</p>

<dialog id="myDialog">
    <h2>Wow Much Dialog!</h2>
</dialog>

CSS

dialog {
    background: url('http://i3.kym-cdn.com/photos/images/newsfeed/000/581/296/c09.jpg');
}

dialog::backdrop {
    position: fixed;
    background-color: rgba(23,23,23,0.5);
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}

dialog h2 {
    color: fuchsia;
    font-family: 'Comic Sans MS';
    margin-top: 4em;
}

body {
    font: 200% sans-serif;
}

button {
    background: fuchsia; /*#232323;*/
    border-radius: 0.2em;
    border: none;
    cursor: pointer;
    color: white;
    font-size: 110%;
    padding: 0.3em;
}

JavaScript

var dialog = document.getElementById('myDialog');
var showBtn = document.getElementById('show');

// Setup an event listener for the show button.
showBtn.addEventListener('click', function(e) {
  e.preventDefault();

  // Show the modal.
  dialog.showModal();
});