Skip to content

Instantly share code, notes, and snippets.

@stevetrask
Created July 20, 2015 18:13
Show Gist options
  • Save stevetrask/0b70b78f1d576c2e29bf to your computer and use it in GitHub Desktop.
Save stevetrask/0b70b78f1d576c2e29bf to your computer and use it in GitHub Desktop.
Fixed aspect ratio for columns
$(function(){
$('.row[data-column-aspect-ratio]').each(function(){
var row = $(this);
var aspectRatio = row.attr('data-column-aspect-ratio');
function resizeColumns(){
row.find('> .column').each(function(){
var column = $(this);
var inner = column.find('> .inner');
var columnWidth = column.width();
var columnHeight = columnWidth / aspectRatio;
var innerHeight = inner.height();
var innerMarginTop = (columnHeight / 2) - (innerHeight / 2);
column.css('height', columnHeight + 'px');
inner.css('margin-top', innerMarginTop + 'px');
});
}
$(window).on('resize', resizeColumns);
resizeColumns();
});
});
<div class="row" data-column-aspect-ratio="1">
<div class="column column-1">
<div class="inner">
<h4>Asdasdkjasdjkaskdj</h4>
<p>asdasdasdasdsa</p>
</div>
</div>
<div class="column column-2">
<div class="inner">
<h4>Asdasdkjasdjkaskdj</h4>
<p>asdasdasdasdsa</p>
</div>
</div>
<div class="column column-3">
<div class="inner">
<h4>Asdasdkjasdjkaskdj</h4>
<p>asdasdasdasdsa</p>
</div>
</div>
<div class="column column-4">
<div class="inner">
<h4>Asdasdkjasdjkaskdj</h4>
<p>asdasdasdasdsa</p>
</div>
</div>
</div>
row{
width: 100%;
}
.column{
width: 25%;
float: left;
margin: 0;
font-size: 1em;
}
.column-1, .column-3{
background: #000;
color: #fff;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment