Skip to content

Instantly share code, notes, and snippets.

@timhudson
Created December 13, 2012 05:50
Show Gist options
  • Save timhudson/4274361 to your computer and use it in GitHub Desktop.
Save timhudson/4274361 to your computer and use it in GitHub Desktop.
Getting consistent aspect ratios in a responsive grid

Padding-bottom: who knew you had it in you?

I've built many responsive layouts in the last couple years and my achilles' heel has always been a fluid grid of (n) squares, or anything with a fixed aspect ratio.

Say you want a grid of movie posters to all line up evenly. You could start with setting up the x-axis:

.grid-item {
  float: left;
  width: 25%;
}

This works great if you can trust the height of the content to remain consitent. Lately, I've been working with images from many sources so consistency with aspect ratio is not an option. My first experiments revolved around the height attribute:

.grid-item {
  float: left;
  width: 25%;
  height: 25%;
}

Unfortunately, height has no relation to width but is instead based on the height of it's container. At this point I resorted to JS which felt really dirty:

$(window).resize(function() {
  $('.grid-item').each(function() {
    var width = $(this).width();
    $(this).height(width);
    // I hate that I just did this
  });
});

Ok, so what other options do we have?

Padding!

Padding is based on the width of it's container. So if we set padding-bottom equal to our width we would get a flexible square.

.grid-item {
  float: left;
  width: 25%;
  padding-bottom: 25%;
}

This technique has been staring me in the face for a while now and I'm stoked to start using it. Try out the example below or let me know why you think this is a horrible idea.

<div class="grid">
<div class="grid-item" style="background-image: url(http://distilleryimage2.s3.amazonaws.com/1a88bb42423e11e28b4622000a9e2975_7.jpg)"></div>
<div class="grid-item" style="background-image: url(http://distilleryimage2.s3.amazonaws.com/1a88bb42423e11e28b4622000a9e2975_7.jpg)"></div>
<div class="grid-item" style="background-image: url(http://distilleryimage2.s3.amazonaws.com/1a88bb42423e11e28b4622000a9e2975_7.jpg)"></div>
<div class="grid-item" style="background-image: url(http://distilleryimage2.s3.amazonaws.com/1a88bb42423e11e28b4622000a9e2975_7.jpg)"></div>
<div class="grid-item" style="background-image: url(http://distilleryimage2.s3.amazonaws.com/1a88bb42423e11e28b4622000a9e2975_7.jpg)"></div>
<div class="grid-item" style="background-image: url(http://distilleryimage2.s3.amazonaws.com/1a88bb42423e11e28b4622000a9e2975_7.jpg)"></div>
<div class="grid-item" style="background-image: url(http://distilleryimage2.s3.amazonaws.com/1a88bb42423e11e28b4622000a9e2975_7.jpg)"></div>
<div class="grid-item" style="background-image: url(http://distilleryimage2.s3.amazonaws.com/1a88bb42423e11e28b4622000a9e2975_7.jpg)"></div>
<div class="grid-item" style="background-image: url(http://distilleryimage2.s3.amazonaws.com/1a88bb42423e11e28b4622000a9e2975_7.jpg)"></div>
<div class="grid-item" style="background-image: url(http://distilleryimage2.s3.amazonaws.com/1a88bb42423e11e28b4622000a9e2975_7.jpg)"></div>
<div class="grid-item" style="background-image: url(http://distilleryimage2.s3.amazonaws.com/1a88bb42423e11e28b4622000a9e2975_7.jpg)"></div>
</div>
<style>
.grid {
padding: 60px;
margin-right: -3%; // used to offset the extra margin on the last grid item in each row
}
.grid-item {
float: left;
width: 22%;
padding-bottom: 22%;
margin-right: 3%;
margin-bottom: 3%;
background-size: cover;
}
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment