Skip to content

Commit a991868

Browse files
committed
Move ratio from helpers to utilties (#41684)
* Convert .ratio helper to new .ratio utility * Fix up * Fix links for now, even though they'll be deleted
1 parent 1ae8576 commit a991868

File tree

9 files changed

+93
-122
lines changed

9 files changed

+93
-122
lines changed

scss/_helpers.scss

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
@import "helpers/colored-links";
44
@import "helpers/focus-ring";
55
@import "helpers/icon-link";
6-
@import "helpers/ratio";
76
@import "helpers/position";
87
@import "helpers/stacks";
98
@import "helpers/visually-hidden";

scss/_utilities.scss

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ $utilities: map-merge(
1111
values: baseline top middle bottom text-bottom text-top
1212
),
1313
// scss-docs-end utils-vertical-align
14+
// scss-docs-start utils-aspect-ratio
15+
"aspect-ratio": (
16+
property: aspect-ratio,
17+
class: ratio,
18+
values: $aspect-ratios
19+
),
20+
// scss-docs-end utils-aspect-ratio
1421
// scss-docs-start utils-float
1522
"float": (
1623
responsive: true,

scss/_variables.scss

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -588,10 +588,11 @@ $transition-collapse-width: width .35s ease !default;
588588

589589
// scss-docs-start aspect-ratios
590590
$aspect-ratios: (
591-
"1x1": 100%,
592-
"4x3": calc(3 / 4 * 100%),
593-
"16x9": calc(9 / 16 * 100%),
594-
"21x9": calc(9 / 21 * 100%)
591+
"auto": auto,
592+
"1x1": #{"1 / 1"},
593+
"4x3": #{"4 / 3"},
594+
"16x9": #{"16 / 9"},
595+
"21x9": #{"21 / 9"}
595596
) !default;
596597
// scss-docs-end aspect-ratios
597598

scss/helpers/_ratio.scss

Lines changed: 0 additions & 26 deletions
This file was deleted.

site/data/sidebar.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@
107107
- title: Focus ring
108108
- title: Icon link
109109
- title: Position
110-
- title: Ratio
111110
- title: Stacks
112111
- title: Stretched link
113112
- title: Text truncation
@@ -119,6 +118,7 @@
119118
icon_color: red
120119
pages:
121120
- title: API
121+
- title: Aspect ratio
122122
- title: Background
123123
- title: Borders
124124
- title: Colors

site/src/content/docs/helpers/ratio.mdx

Lines changed: 0 additions & 71 deletions
This file was deleted.

site/src/content/docs/migration.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -709,11 +709,11 @@ Want more information? [Read the v5.1.0 blog post.](https://blog.getbootstrap.co
709709

710710
### Helpers
711711

712-
- <span class="badge text-bg-danger">Breaking</span> **Responsive embed helpers have been renamed to [ratio helpers]([[docsref:/helpers/ratio]])** with new class names and improved behaviors, as well as a helpful CSS variable.
712+
- <span class="badge text-bg-danger">Breaking</span> **Responsive embed helpers have been renamed to [ratio helpers]([[docsref:/utilities/aspect-ratio]])** with new class names and improved behaviors, as well as a helpful CSS variable.
713713
- Classes have been renamed to change `by` to `x` in the aspect ratio. For example, `.ratio-16by9` is now `.ratio-16x9`.
714714
- We’ve dropped the `.embed-responsive-item` and element group selector in favor of a simpler `.ratio > *` selector. No more class is needed, and the ratio helper now works with any HTML element.
715715
- The `$embed-responsive-aspect-ratios` Sass map has been renamed to `$aspect-ratios` and its values have been simplified to include the class name and the percentage as the `key: value` pair.
716-
- CSS variables are now generated and included for each value in the Sass map. Modify the `--bs-aspect-ratio` variable on the `.ratio` to create any [custom aspect ratio]([[docsref:/helpers/ratio#custom-ratios]]).
716+
- CSS variables are now generated and included for each value in the Sass map. Modify the `--bs-aspect-ratio` variable on the `.ratio` to create any [custom aspect ratio]([[docsref:/utilities/aspect-ratio#custom-ratios]]).
717717

718718
- <span class="badge text-bg-danger">Breaking</span> **"Screen reader" classes are now ["visually hidden" classes]([[docsref:/helpers/visually-hidden]]).**
719719
- Changed the Sass file from `scss/helpers/_screenreaders.scss` to `scss/helpers/_visually-hidden.scss`
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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" />

site/src/scss/_component-examples.scss

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -173,29 +173,18 @@
173173

174174
// Ratio helpers
175175
.bd-example-ratios {
176-
.ratio {
177-
display: inline-block;
178-
width: 10rem;
176+
[class*="ratio"] {
177+
display: flex;
178+
align-items: center;
179+
justify-content: center;
180+
margin-bottom: 1rem;
179181
color: var(--bs-secondary-color);
180182
background-color: var(--bs-tertiary-bg);
181183
border: var(--bs-border-width) solid var(--bs-border-color);
182-
183-
> div {
184-
display: flex;
185-
align-items: center;
186-
justify-content: center;
187-
}
184+
@include border-radius(var(--bs-border-radius));
188185
}
189186
}
190-
.bd-example-ratios-breakpoint {
191-
.ratio-4x3 {
192-
width: 16rem;
193187

194-
@include media-breakpoint-up(md) {
195-
--bs-aspect-ratio: 50%; // 2x1
196-
}
197-
}
198-
}
199188

200189
.bd-example-offcanvas {
201190
.offcanvas {

0 commit comments

Comments
 (0)