Gerillass

v2.1.0

Aspect Ratio

Type: Mixin
@include aspect-ratio();

* You can call mixins with or without the gls- namespace (e.g. @include gls-aspect-ratio();).

The Aspect Ratio Sass mixin holds an element to the ratio you give it. CSS has an aspect-ratio property of its own, and this mixin adds the three things that property leaves out.

Arguments

NameTypeDescription
$ratio (16/9)string, number

The ratio to hold. Accepts a slash separated string such as 16/9, a colon separated string such as 16:9, or a bare number, where 1.5 means 1.5 to 1. The default is 16/9. Anything else is refused with an error.

$fit (cover)string

Sets the object-fit property. Accepts fill, contain, cover, none, scale-down, or null to leave object-fit alone. The default is cover.

Pass null as the second argument to leave object-fit out of the output entirely.

Examples

No arguments

Call it without arguments and the ratio is 16/9.

Sass
.element {
  @include aspect-ratio;
}
CSS
.element {
  display: block;
  width: 100%;
  aspect-ratio: 16 / 9;
  border: 0;
  object-fit: cover;
}
On an image

Because object-fit defaults to cover, the image is cropped to fill the box rather than stretched.

Sass
.element {
  @include aspect-ratio("16:9");
}
CSS
.element {
  display: block;
  width: 100%;
  aspect-ratio: 16 / 9;
  border: 0;
  object-fit: cover;
}
Result
A different ratio

A colon and a slash mean the same thing, so 16:9 and 16/9 give identical output.

Sass
.element {
  @include aspect-ratio("4/3");
}
CSS
.element {
  display: block;
  width: 100%;
  aspect-ratio: 4 / 3;
  border: 0;
  object-fit: cover;
}
Result
Containing instead of cropping

Pass a second value to change how the content fills the box. Here the box is square and the whole image stays visible.

Sass
.element {
  @include aspect-ratio("1:1", contain);
  background: #f0f0ee;
}
CSS
.element {
  display: block;
  width: 100%;
  aspect-ratio: 1 / 1;
  border: 0;
  object-fit: contain;
  background: #f0f0ee;
}
Result
A bare number, and no object-fit

1.5 means 1.5 to 1. Passing null as the second argument leaves object-fit out, which is what you want when the element has no content of its own to fit.

Sass
.element {
  @include aspect-ratio(1.5, null);
}
CSS
.element {
  display: block;
  width: 100%;
  aspect-ratio: 1.5;
  border: 0;
}
An embedded video

Apply the mixin to the iframe itself. There is no wrapper element and no padding hack.

Sass
.element {
  @include aspect-ratio("16/9");
}
CSS
.element {
  display: block;
  width: 100%;
  aspect-ratio: 16 / 9;
  border: 0;
  object-fit: cover;
}
Result
The ratio on its own

If you want the ratio without the rest of the mixin, validateRatio accepts the same values and returns just the ratio.

Sass
.element {
  aspect-ratio: validateRatio("16:9");
}
CSS
.element {
  aspect-ratio: 16 / 9;
}

Coming from Ratio Box or Responsive Video

Both of those mixins held a ratio with a padding-top hack, a pseudo-element and an absolutely positioned child. The CSS aspect-ratio property made all of that unnecessary, and it left the two mixins identical to each other, so they were replaced by this single one.

The rename itself is mechanical. What is not mechanical is where the mixin goes: the old mixins were applied to a wrapping element, and this one goes on the element itself.

Sass
// Before, on a wrapper
.hero  { @include ratio-box("16/9"); }
.video { @include responsive-video("16/9"); }

// After, on the element
.hero  { @include aspect-ratio("16/9"); }
.video iframe { @include aspect-ratio("16/9"); }

If you have a wrapper <div> around a video embed only to hold its ratio, remove the wrapper along with the old mixin.