Skip to content

Instantly share code, notes, and snippets.

@TylerFisher TylerFisher/LICENSE
Last active Aug 29, 2015

Embed
What would you like to do?
Our elections app relied on a 16x9 slide layout across all devices, regardless of the size of the actual screen. We wrote some tricky CSS and JavaScript to achieve this.
html {
font-size:1vw;
}
body {
margin:0;
}
#stack {
box-sizing:border-box;
-moz-box-sizing:border-box;
-webkit-box-sizing:border-box;
border:4px dashed black;
/*display: inline-block;*/
margin: 0 auto;
width: 100rem;
height: 56.3rem;
}
.big {
font-size: 10rem;
}
.little {
font-size: 2rem;
}
.em {
background-color: blue;
width: 1rem;
height: .1rem;
}
<html>
<head>
<link rel="stylesheet" href="app.css">
</head>
<body>
<div id="baseline"></div>
<div id="stack">
<div class="big">
BIG
<div class="em"></div>
</div>
<div class="little">little <div class="em"></div>
</div>
</div>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="app.js"></script>
</body>
The MIT License (MIT)
Copyright (c) 2014 NPR
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
var onWindowResize = function(){
var stack = document.getElementById("stack");
var aspect = window.innerWidth / window.innerHeight;
if ( aspect > 16/9) {
document.documentElement.style.fontSize = ((16/9) / aspect) + 'vw';
} else {
document.documentElement.style.fontSize = '1vw';
}
}
$(window).on('resize', onWindowResize);
onWindowResize();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.