|
| 1 | +--- |
| 2 | +title: Aspect ratio |
| 3 | +description: Make elements maintain specific aspect ratios. Perfect for handling videos, slideshow embeds, and more based on the width of the parent. |
| 4 | +toc: true |
| 5 | +--- |
| 6 | + |
| 7 | +Use the ratio utility to manage the aspect ratios of content like `<iframe>`s, `<embed>`s, `<video>`s, and `<object>`s. These helpers also can be used on any standard HTML child element (e.g., a `<div>` or `<img>`). Customize the available aspect ratios with the Sass variable or the utility API. |
| 8 | + |
| 9 | +<Callout> |
| 10 | +**Pro-Tip!** You don't need `frameborder="0"` on your `<iframe>`s as we override that for you in [Reboot]([[docsref:/content/reboot]]). |
| 11 | +</Callout> |
| 12 | + |
| 13 | +## Example |
| 14 | + |
| 15 | +Add your ratio utility to the element you want to modify, like an `<iframe>`, `<video>`, or less semantic elements like `<div>`. Ratio utilities also pair well with any width utilities, as shown below. Customize the available aspect ratios with the Sass variable or the utility API. |
| 16 | + |
| 17 | +Heads up—you can omit `frameborder="0"` on your `<iframe>`s as that is overridden for you in [Reboot]([[docsref:/content/reboot]]). |
| 18 | + |
| 19 | +<Example code={`<iframe class="w-100 ratio-16x9" src="https://www.youtube.com/embed/zpOULjyy-n8?rel=0" title="YouTube video" allowfullscreen></iframe>`} /> |
| 20 | + |
| 21 | +Swap the `.ratio-*` class for a different aspect ratio. |
| 22 | + |
| 23 | +<Example class="bd-example-ratios" code={`<div class="ratio-auto"> |
| 24 | + <div>Auto</div> |
| 25 | + </div> |
| 26 | + <div class="w-25 ratio-1x1"> |
| 27 | + <div>1×1</div> |
| 28 | + </div> |
| 29 | + <div class="w-50 ratio-4x3"> |
| 30 | + <div>4×3</div> |
| 31 | + </div> |
| 32 | + <div class="w-75 ratio-16x9"> |
| 33 | + <div>16×9</div> |
| 34 | + </div> |
| 35 | + <div class="w-100 ratio-21x9"> |
| 36 | + <div>21×9</div> |
| 37 | + </div>`} /> |
| 38 | + |
| 39 | + |
| 40 | +{/* |
| 41 | +## Custom ratios |
| 42 | +
|
| 43 | +mdo-do: do we bring these back? |
| 44 | +
|
| 45 | +Each `.ratio-*` class includes a CSS custom property (or CSS variable) in the selector. You can override this CSS variable to create custom aspect ratios on the fly with some quick math on your part. |
| 46 | +
|
| 47 | +For example, to create a 2x1 aspect ratio, set `--bs-aspect-ratio: 50%` on the `.ratio`. |
| 48 | +
|
| 49 | +<Example class="bd-example-ratios" code={`<div class="ratio" style="--bs-aspect-ratio: 50%;"> |
| 50 | + <div>2x1</div> |
| 51 | + </div>`} /> |
| 52 | +
|
| 53 | +This CSS variable makes it easy to modify the aspect ratio across breakpoints. The following is 4x3 to start, but changes to a custom 2x1 at the medium breakpoint. |
| 54 | +
|
| 55 | +```scss |
| 56 | +.ratio-4x3 { |
| 57 | + @include media-breakpoint-up(md) { |
| 58 | + --bs-aspect-ratio: 50%; // 2x1 |
| 59 | + } |
| 60 | +} |
| 61 | +``` |
| 62 | +
|
| 63 | +<Example class="bd-example-ratios bd-example-ratios-breakpoint" code={`<div class="ratio ratio-4x3"> |
| 64 | + <div>4x3, then 2x1</div> |
| 65 | + </div>`} /> |
| 66 | +*/} |
| 67 | + |
| 68 | +## Sass map |
| 69 | + |
| 70 | +Within `_variables.scss`, you can change the aspect ratios you want to use. Here's our default `$aspect-ratios` map. Modify the map as you like and recompile your Sass to put them to use. |
| 71 | + |
| 72 | +<ScssDocs name="aspect-ratios" file="scss/_variables.scss" /> |
0 commit comments