Skip to content

Instantly share code, notes, and snippets.

@bepatrickdavid
Last active August 29, 2015 14:25
Show Gist options
  • Save bepatrickdavid/926362d1894fc9aac4a9 to your computer and use it in GitHub Desktop.
Save bepatrickdavid/926362d1894fc9aac4a9 to your computer and use it in GitHub Desktop.
jQUERY: div same height as width 100% parent change dinamically
//html
<div class="home-item" style="background-image:url(http://placehold.it/500x500)"></div>
//css
.home-item {
width:20%;
background-position: center center;
background-repeat: no-repeat;
background-size: cover;
float: left;
}
<script>
$(window).load(function () {
var divWidth = $('.home-item').width();
$('.home-item').css({
'height': divWidth + 'px'
});
});
$(window).resize(function () {
var divWidth = $('.home-item').width();
$('.home-item').css({
'height': divWidth + 'px'
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment