Skip to main content

Helpers

Ratio

Keep embeds and media at a fixed aspect ratio, with four presets and a custom property.

How it works

Wrap the embed in .ratio and add a .ratio-* modifier. The wrapper gets a ::before with padding-top: var(--bs-aspect-ratio), and the one direct child is stretched to fill it. Nothing about it depends on script or on the child's own size.

  • .ratio — position: relative; width: 100%.
  • .ratio > * — absolutely positioned, 100% × 100%.
  • --bs-aspect-ratio — a percentage: height ÷ width × 100.

Use it for <iframe>, <video>, <embed> and <object>, or any single block.

16×9 — a highlight reel would go here
HTML
<div class="ratio ratio-16x9">
  <div class="text-bg-navy d-flex align-items-center justify-content-center">16×9 — a highlight reel would go here</div>
</div>
<div class="ratio ratio-16x9">
  <iframe src="https://www.youtube.com/embed/…" title="Highlight reel" allowfullscreen></iframe>
</div>

Aspect ratios

Four presets are compiled. Each one only sets the custom property.

Class--bs-aspect-ratioTypical use
.ratio-1x1100%Square thumbnails, roster headshots
.ratio-4x375%Era-accurate broadcast footage
.ratio-16x956.25%Modern video embeds
.ratio-21x942.8571428571%Banner art
1×1
4×3
16×9
21×9
HTML
<div class="row g-4">
  <div class="col-6 col-md-3">
    <div class="ratio ratio-1x1"><div class="text-bg-cream border d-flex align-items-center justify-content-center">1×1</div></div>
  </div>
  <div class="col-6 col-md-3">
    <div class="ratio ratio-4x3"><div class="text-bg-cream border d-flex align-items-center justify-content-center">4×3</div></div>
  </div>
  <div class="col-6 col-md-3">
    <div class="ratio ratio-16x9"><div class="text-bg-cream border d-flex align-items-center justify-content-center">16×9</div></div>
  </div>
  <div class="col-6 col-md-3">
    <div class="ratio ratio-21x9"><div class="text-bg-cream border d-flex align-items-center justify-content-center">21×9</div></div>
  </div>
</div>

Custom ratios

Set --bs-aspect-ratio inline for a one-off. The value is a percentage, so a 2:1 banner is 50% and a portrait poster at 2:3 is 150%.

2×1
Poster 2×3
HTML
<div class="row g-4">
  <div class="col-md-8">
    <div class="ratio" style="--bs-aspect-ratio: 50%"><div class="text-bg-gold d-flex align-items-center justify-content-center">2×1</div></div>
  </div>
  <div class="col-md-4">
    <div class="ratio" style="--bs-aspect-ratio: 150%"><div class="text-bg-gold d-flex align-items-center justify-content-center">Poster 2×3</div></div>
  </div>
</div>

The custom property can also change per breakpoint in your own CSS — a square on phones, widescreen on desktop:

.ratio-poster {
  --bs-aspect-ratio: 100%;
}
@media (min-width: 768px) {
  .ratio-poster {
    --bs-aspect-ratio: 56.25%;
  }
}

Sass

Presets come from $aspect-ratios. Add a key and the class appears; Bootstrap's defaults:

$aspect-ratios: (
  "1x1": 100%,
  "4x3": calc(3 / 4 * 100%),
  "16x9": calc(9 / 16 * 100%),
  "21x9": calc(9 / 21 * 100%)
);

Related: the sizing utilities for plain width and height, and Images for .img-fluid and object-fit.