Skip to content
VinUIComponentsAspect Ratio

Aspect Ratio

A box that keeps a fixed shape, so images and videos don't jump as they load. Same markup and props in every style: switch the style in the header to see it change.

16 by 9

For video and wide pictures. The picture is cropped to fill the box.

Purple mountains at sunset
aspect-ratio-wide.html
<div class="vin-aspect" style="--vin-ratio: 16 / 9; max-width: 480px">
  <img src="/ui/media/mountains.svg" alt="Purple mountains at sunset">
</div>

Square

1 by 1, for thumbnails and avatars.

A lake below green hills
Purple mountains at sunset
aspect-ratio-square.html
<div class="vin-aspect" style="--vin-ratio: 1; width: 160px">
  <img src="/ui/media/lake.svg" alt="A lake below green hills">
</div>
<div class="vin-aspect" style="--vin-ratio: 1; width: 160px">
  <img src="/ui/media/mountains.svg" alt="Purple mountains at sunset">
</div>

Portrait

3 by 4, for posters and phone screenshots.

A lake below green hills
aspect-ratio-portrait.html
<div class="vin-aspect" style="--vin-ratio: 3 / 4; width: 200px">
  <img src="/ui/media/lake.svg" alt="A lake below green hills">
</div>

Installation

React, Vue and Svelte: add the component to your project with the CLI. It reads your framework from vin.json and also copies any component this one needs. Then import it:

Terminal
npx @vinish/ui add aspect-ratio
React
import { AspectRatio } from "@/components/vin/aspect-ratio";
Vue
import AspectRatio from "@/components/vin/aspect-ratio.vue";
Svelte
import AspectRatio from "$lib/components/vin/aspect-ratio.svelte";

The import paths assume the default folders: @/components/vin (React and Vue, with the usual @ → src alias) and $lib/components/vin (SvelteKit). Use your own if you chose another folder in init. How the Vue and Svelte versions pass values and content is in Frameworks.

Plain HTML: the classes come with the style's stylesheet. It needs no JavaScript.

API

From spec/aspect-ratio.json, which every framework version is tested against.

PropValuesDefaultIn HTML
ratio
Width divided by height, like 16 / 9, 4 / 3 or 1.
number | string16 / 9style="--vin-ratio: 16 / 9"

Parts

React, Vue, SvelteHTML classes
<AspectRatio>.vin-aspect

Accessibility

  • The box adds nothing for screen readers: give the picture inside real alt text, or alt="" if it's decoration.
  • An iframe inside needs a title ("Product demo video").
  • Reserving the space stops the page from shifting as the media loads.