Skip to content

Aspect Ratio

@astryxdesign/core v0.7.1 · AspectRatio

Usage ​

AspectRatio keeps a fixed width-to-height ratio as its container changes size. Use it for images, video, thumbnails, and other media that should stay in proportion. The width comes from the parent, and the height is derived from ratio, so an ancestor needs a definite width.

vue
<script setup lang="ts">
import { AspectRatio } from '@astryx-vue/core/AspectRatio'
</script>

<template>
  <AspectRatio :ratio="16 / 9" fit="cover">
    <img alt="Landscape" src="/photo.jpg">
  </AspectRatio>
</template>

ratio is width divided by height. Write 16 / 9 rather than a rounded decimal. shape is rectangle or ellipse; an ellipse with ratio 1 is a circle. fit sizes the child: cover fills and crops, contain letterboxes, and center keeps the natural size. Leave fit unset when the child sizes itself.

The ratio is written to the CSS variable --astryx-aspect-ratio, not as an inline aspect-ratio declaration, so a stylesheet can override it.

Anatomy

ElementDescription
Ratio boxThe outer element that holds the aspect ratio and clips overflow. Carries the astryx-aspect-ratio theme target, and the elliptical clip when shape is ellipse.
Content slotA wrapper that fills the ratio box and positions the child. With fit set it also sizes the child; without it the child styles itself.

Best practices ​

GuidancePractices
DoPass one child when fit is set. Every direct child is stretched, so put an overlay inside that child.
DoGive images an alt, or alt="" when the image is decorative. The ratio box adds no accessible name of its own.
Don'tUse it as a general layout container. Use Section, Card, or Layout for page regions.
Don'tSet only a height. The width comes from the container, so a lone height clamps the box off ratio.

Examples ​

Circle

Circular image

shape ellipse with ratio 1 clips the image to a circle.

Image gallery

Mountain landscape
Ocean sunset
Forest trail
City skyline
Desert dunes
Snowy peaks

A grid of images that share a 4:3 frame.

Square

1:1 square

A 1:1 frame for an avatar or a square image.

Widescreen

16:9 widescreen

A 16:9 frame around an image.

Loading skeleton

The frame holds a skeleton while the image is still loading.