Skip to content
Go back

A Modal Cure in Pure CSS – No Wrappers, no JavaScript, no BS

Published:  at  05:43 PM

Modals. They’ve been the subject of countless hacks over the years (I did a cross-browser one a while ago here), but due to cross-browser considerations, they usually are less than elegant in their implementation. Well, if you don’t care about IE much, much easier. This little trick is so simple it’s going to make you cry, in fact it’s so easy, it may cause you to audibly curse IE louder and more passionately than you ever have:

<div id="modal"></div>
#modal {
   display: block;
   position: fixed;
   top: 50%;
   left: 50%;
   box-sizing: border-box;
   transform: translate(-50%, -50%);
}

Here’s a fiddle so you can see it in action:

Yeah I know, all those hours wasted on wrapper divs, tables, crazy CSS, and performance-robbing JS, all to get hoodwinked by some top/left positioning and a transform property, go figure.


Suggest Changes

Previous Post
CSS Selector Listeners
Next Post
I Want a DAMNodeInserted Event!