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.
<div class="vin-aspect" style="--vin-ratio: 16 / 9; max-width: 480px">
<img src="/ui/media/mountains.svg" alt="Purple mountains at sunset">
</div>import { AspectRatio } from "@/components/vin/aspect-ratio";
export function AspectRatioWide() {
return (
<AspectRatio ratio="16 / 9" style={{ maxWidth: 480 }}>
<img src="/ui/media/mountains.svg" alt="Purple mountains at sunset" />
</AspectRatio>
);
}The Vue version renders the same markup as the HTML tab, so it looks and works the same.
<script setup lang="ts">
import AspectRatio from "@/components/vin/aspect-ratio.vue";
</script>
<template>
<AspectRatio ratio="16 / 9" style="max-width: 480px">
<img src="/ui/media/mountains.svg" alt="Purple mountains at sunset" />
</AspectRatio>
</template>The Svelte version renders the same markup as the HTML tab, so it looks and works the same.
<script lang="ts">
import AspectRatio from "$lib/components/vin/aspect-ratio.svelte";
</script>
<AspectRatio ratio="16 / 9" style="max-width: 480px">
<img src="/ui/media/mountains.svg" alt="Purple mountains at sunset" />
</AspectRatio>Square
1 by 1, for thumbnails and avatars.
<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>import { AspectRatio } from "@/components/vin/aspect-ratio";
export function AspectRatioSquare() {
return (
<>
<AspectRatio ratio={1} style={{ width: 160 }}>
<img src="/ui/media/lake.svg" alt="A lake below green hills" />
</AspectRatio>
<AspectRatio ratio={1} style={{ width: 160 }}>
<img src="/ui/media/mountains.svg" alt="Purple mountains at sunset" />
</AspectRatio>
</>
);
}The Vue version renders the same markup as the HTML tab, so it looks and works the same.
<script setup lang="ts">
import AspectRatio from "@/components/vin/aspect-ratio.vue";
</script>
<template>
<AspectRatio :ratio="1" style="width: 160px">
<img src="/ui/media/lake.svg" alt="A lake below green hills" />
</AspectRatio>
<AspectRatio :ratio="1" style="width: 160px">
<img src="/ui/media/mountains.svg" alt="Purple mountains at sunset" />
</AspectRatio>
</template>The Svelte version renders the same markup as the HTML tab, so it looks and works the same.
<script lang="ts">
import AspectRatio from "$lib/components/vin/aspect-ratio.svelte";
</script>
<AspectRatio ratio={1} style="width: 160px">
<img src="/ui/media/lake.svg" alt="A lake below green hills" />
</AspectRatio>
<AspectRatio ratio={1} style="width: 160px">
<img src="/ui/media/mountains.svg" alt="Purple mountains at sunset" />
</AspectRatio>Portrait
3 by 4, for posters and phone screenshots.
<div class="vin-aspect" style="--vin-ratio: 3 / 4; width: 200px">
<img src="/ui/media/lake.svg" alt="A lake below green hills">
</div>import { AspectRatio } from "@/components/vin/aspect-ratio";
export function AspectRatioPortrait() {
return (
<AspectRatio ratio="3 / 4" style={{ width: 200 }}>
<img src="/ui/media/lake.svg" alt="A lake below green hills" />
</AspectRatio>
);
}The Vue version renders the same markup as the HTML tab, so it looks and works the same.
<script setup lang="ts">
import AspectRatio from "@/components/vin/aspect-ratio.vue";
</script>
<template>
<AspectRatio ratio="3 / 4" style="width: 200px">
<img src="/ui/media/lake.svg" alt="A lake below green hills" />
</AspectRatio>
</template>The Svelte version renders the same markup as the HTML tab, so it looks and works the same.
<script lang="ts">
import AspectRatio from "$lib/components/vin/aspect-ratio.svelte";
</script>
<AspectRatio ratio="3 / 4" style="width: 200px">
<img src="/ui/media/lake.svg" alt="A lake below green hills" />
</AspectRatio>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:
npx @vinish/ui add aspect-ratioimport { AspectRatio } from "@/components/vin/aspect-ratio";import AspectRatio from "@/components/vin/aspect-ratio.vue";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.
| Prop | Values | Default | In HTML |
|---|---|---|---|
ratioWidth divided by height, like 16 / 9, 4 / 3 or 1. | number | string | 16 / 9 | style="--vin-ratio: 16 / 9" |
Parts
| React, Vue, Svelte | HTML 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.
Customize
Saved in this browser. Style and accent are in the header.