/* container defines margins and width */
.container {
margin: 60px 120px 0;
}
/* outer container will define aspect ratio */
.outer {
position: relative;
width: 100%;
}
.outer.r4x3 {
padding-top: 75%; /* "height" will be 3/4 of width */
}
.outer.r2x1 {
padding-top: 50%; /* "height" will be 1/2 of width */
}
/* inner container positioned absolutely and holds content */
.outer .inner {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
outline: 1px solid grey;
}
/* example content */
.little-box {
float: left;
outline: 1px solid steelblue;
width: 25%;
height: 33.333%;
}
/* just for display purposes... */
.inner::after {
content: attr(data-size);
position: absolute;
left:0;
top: -1em;
}
JavaScript
// this javascript is simply to display the size of the box
// you'll see some rounding errors from the percentage calculation in CSS
$(window).on('resize', function(){
$('.inner').each(function(){
var box = $(this);
var width = box.width();
var height = box.height();
box.attr('data-size', width+'x'+height+' (r: '+(width/height).toFixed(2)+')');
});
}).trigger('resize');
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.