Skip to content

Commit

Permalink
New glossary: aspect ratio (#33289)
Browse files Browse the repository at this point in the history
* New glossary: aspect ratio

* New glossary: aspect ratio

* New glossary: aspect ratio

* New glossary: aspect ratio
  • Loading branch information
estelle authored Apr 29, 2024
1 parent 5a2b25f commit df03295
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions files/en-us/glossary/aspect_ratio/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
title: Aspect ratio
slug: Glossary/Aspect_ratio
page-type: glossary-definition
---

{{GlossarySidebar}}

An **aspect ratio** is the proportional relationship between an element or {{glossary("viewport")}}'s width and height, and is represented as a ratio or two numbers.

Having an aspect ratio, whether it's an inherent aspect ratio like with images and videos or if it's extrinsically set, maintains the intended proportions of an element. You can also query an element or viewport's aspect, which is useful in developing flexible components and layouts.

In CSS, the {{cssxref("ratio")}} data type is written as `width / height` (e.g., `1 / 1` for a square, `16 / 9` for widescreen) or a single number, in which case the number represents the width and the height is `1`.

```css
.wideBox {
aspect-ratio: 5 / 2;
}
.tallBox {
aspect-ratio: 0.25;
}
```

In SVG, the aspect ratio is defined by the a four-value [`viewBox`](/en-US/docs/Web/SVG/Attribute/viewBox) attribute. The first two values the smallest X and Y origin coordinates the SVG can have, and the second two values are the width and height which set the aspect ratio of the SVG.

```svg
<svg viewBox="0 0 300 100" xmlns="http://www.w3.org/2000/svg"></svg>
```

In JavaScript APIs, querying an aspect ratio returns a double-precision floating-point number representing the width divided by height. You can also use JavaScript to set an element's aspect ratio. For example, setting an aspect ratio constraint for a 1920x1080 video using the {{domxref("MediaStreamTrack")}} or {{domxref("MediaTrackSettings")}} dictionary's [`aspectRatio`](/en-US/docs/Web/API/MediaTrackSettings/aspectRatio) property would be computed as 16/9, or 1920/1080, which is `1.7777777778`:

```javascript
const constraints = {
width: 1920,
height: 1080,
aspectRatio: 1.777777778,
};

myTrack.applyConstraints(constraints);
```

## See also

- CSS {{cssxref("aspect-ratio")}} property
- [CSS box sizing](/en-US/docs/Web/CSS/CSS_box_sizing) module
- {{glossary("intrinsic size")}} glossary term
- CSS {{cssxref("min-content")}}, {{cssxref("max-content")}}, and {{cssxref("fit-content")}} property values.

0 comments on commit df03295

Please sign in to comment.