Skip to content

Instantly share code, notes, and snippets.

@johndhancock
Created March 24, 2015 21:29
Show Gist options
  • Save johndhancock/ad0abb47c2e839aaca2c to your computer and use it in GitHub Desktop.
Save johndhancock/ad0abb47c2e839aaca2c to your computer and use it in GitHub Desktop.
This resizes a div's height proportionally to the width of it
#myDiv {
width: 100%;
height: 0px; /*arbitrary number, 0 may be best to use until jquery calculation runs */
}
<div id="myDiv">
<p>This is a div with some content in it</p>
</div>
$(document).ready(function() {
var windowWidth = $(window).width(); //grabbing window width
$('#myDiv').css('height', windowWidth * .667); //running the initial sizing function on page load
//rerunning the function on window resizing
$(window).resize(function() {
windowWidth = $(window).width();
$('#myDiv').css('height', windowWidth * .667);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment