Aspect Ratio

GitHubnpm
Displays content within a desired ratio.
Landscape photograph by Tobias Tullius
<script setup lang="ts">
import { AspectRatio } from 'akar';
import './styles.css';
</script>

<template>
  <div class="akar-demo akar-demo-AspectRatio">
    <div class="Container">
      <AspectRatio :ratio="16 / 9">
        <img
          class="Image"
          src="https://images.unsplash.com/photo-1498855926480-d98e83099315?w=300&dpr=2&q=80"
          alt="Landscape photograph by Tobias Tullius"
        >
      </AspectRatio>
    </div>
  </div>
</template>

Features

  • Accepts any custom ratio.

Installation

Install the component from your command line.

pnpm add akar

Anatomy

Import the component.

<script setup>
import { AspectRatio } from 'akar';
</script>

<template>
  <AspectRatio />
</template>

API Reference

Root

Contains the content you want to constrain to a given ratio.

PropDefaultType
as
"div"
AsTag | Component

The element or component this component should render as. Can be overwritten by asChild.

asChild
boolean

Change the default rendered element for the one passed as a child, merging their props and behavior.

Read our Composition guide for more details.

ratio
1
number

The desired ratio. Eg: 16/9

Slot (default)Payload
aspect
number

Current aspect ratio (in %)

AspectRatio.vue source