Fluid Aspect Ratio

Move from a minimum viewport width of 320px and an aspect ratio of 16:9, to a maximum viewport of 1200px and an aspect ratio of 5:1.

Targets: At 320px wide, with an aspect of 16:9 the container should be 320 x 180px.
At 1200px wide, with an aspect of 4:1 the container should be 1200 x 300px.

The problem:

A small viewport demands an image that is more rectangular — and we typically use the 16:9 aspect ratio for 320px wide screens. Wider viewports, however, need an image size that is less tall in order that content can remain visible. Since most laptops have a 16:9 widescreen aspect ratio, we need something even more dramatic. Here, I target a 4:1 ratio for 1200px wide screens.

The goal:

Move fluidly from the 16:9 aspect ratio up to a 4:1 aspect ratio without using multiple media queries, resulting in a “stepped” effect when the media query abruptly changes the height of the containing element.

The solution:

CSS’s new calc() function can use dynamic units like viewport units to create a measurement that constantly changes. Thanks you, thank you, thank you to Tim Brown for his in-depth article and crazy math around fluid line-heights .

How does this work?

min-height: 180px;
  height: calc(180px + (300 - 180) * ((100vw - 320px)/(1200 - 320)));
  max-height: 300px;

Think of it as portions to the left and right of the multiplication sign — left is heights, right is widths:

If I were to break it down into SASS variables, it might look something like this:

height: calc($min-height + ($max-height - $min-height) * ((100vw - $min-width)/($max-width - $min-width)))

And further, if I were to break this down into reusable SASS logic:

$min-width: 320px;
  $min-aspect: 16 9;
  $min-height: $min-width * (nth($min-aspect, 2) / nth($min-aspect, 1));
  $max-width: 1200px;
  $max-aspect: 4 1;
  $max-height: $max-width * (nth($max-aspect, 2) / nth($max-aspect, 1));
  .element {
  min-height: $min-height;
  height: calc(#{$min-height} + (#{$max-height} - #{$min-height}) * ((100vw - #{$min-width})/(#{$max-width} - #{$min-width})));
  max-height: $max-height;
  }

Again, big thanks to Tim Brown for the start on this crazy calculus.
Authored by J. Hogue at Oomph, Inc.