…in less than half a kilobyte
All you need is the file modal.min.css
and the following code:
<input id="helloworld" type="checkbox" hidden><div class="modal">
<label for="helloworld"></label><!-- close modal on click -->
<div><!-- modal contents -->
<h1>Look at this awesomeness!</h1>
Hello, I'm a modal window!
</div>
</div>
<label for="helloworld">Open the modal!</label>
window.modal={};
modal.toggle = function toggle(id){document.getElementById(id).click()}
modal.open = function open(id){(function(e){!e.checked&&e.click()})(document.getElementById(id))}
modal.close = function close(id){(function(e){e.checked&&e.click()})(document.getElementById(id))}
modal.isOpen = function isOpen(id){return document.getElementById(id).checked}
modal.listen = function change(id,fn){document.getElementById(id).addEventListener("change",function(){fn(event.target.checked)}.bind(fn))}
modal.toggle(id)
modal.open(id)
modal.close(id)
modal.isOpen(id)
modal.listen(id, function(isOpen) {
console.log(isOpen ? "Opened" : "Closed");
}
<input id="lightbox" type="checkbox" hidden><div class="modal">
<label for="lightbox"><div style="padding: 0; font-size: 0; width: 100%; background: transparent">
<img width="100%" onload="this.parentElement.style.width = 'calc((100vh - 30px) * ' + (Math.floor(this.width * 1000 / this.height) / 1000) + ')'">
</div></label>
</div>
<script>
function lightbox(url) {
var modal = document.getElementById("lightbox");
modal.nextElementSibling.getElementsByTagName("img")[0].src = url || event.target.src;
modal.checked = true;
}
</script>
<img src="my-image.jpg" onclick="lightbox()">
<input id="lightbox" type="checkbox" hidden><div class="modal"><label for="lightbox"><div style="padding:0;font-size:0;width:100%;background:transparent"><img width="100%" onload="this.parentElement.style.width='calc((100vh-30px)*'+(Math.floor(this.width*1000/this.height)/1000)+')'"></div></label></div><script>function lightbox(u){var m=document.getElementById("lightbox");m.nextElementSibling.getElementsByTagName("img")[0].src=url||event.target.src;m.checked=true}</script>
2017-05-08: Release
2017-05-08: Content div can now be nested inside the closing label, which means that clicking the content can now also close the modal. Added lightbox example and JS wrapper.
public domain / cc0