Skip to content

Aspect Ratio

Displays content within a desired ratio.

Photo by Drew Beamer
## Installation
bash
npx @galaxy-stack/nebula-cli@latest add aspect-ratio
bash
pnpm dlx @galaxy-stack/nebula-cli@latest add aspect-ratio
bash
yarn dlx @galaxy-stack/nebula-cli@latest add aspect-ratio
bash
bunx @galaxy-stack/nebula-cli@latest add aspect-ratio
bash
# If you have installed galaxy-design globally
galaxy-design add aspect-ratio

Usage

vue
<script setup lang="ts">
import { AspectRatio } from '@/components/ui/aspect-ratio'
</script>

<template>
  <AspectRatio :ratio="16 / 9" class="bg-muted">
    <img
      src="https://images.unsplash.com/photo-1588345921523-c2dcdb7f1dcd"
      alt="Photo by Drew Beamer"
      class="rounded-md object-cover"
    />
  </AspectRatio>
</template>
tsx
import { AspectRatio } from "@/components/ui/aspect-ratio"

export default function App() {
  return (
    <AspectRatio ratio={16 / 9} className="bg-muted">
      <img
        src="https://images.unsplash.com/photo-1588345921523-c2dcdb7f1dcd"
        alt="Photo by Drew Beamer"
        className="rounded-md object-cover"
      />
    </AspectRatio>
  )
}
typescript
import { Component } from '@angular/core';
import { AspectRatioComponent } from '@/components/ui/aspect-ratio';

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [AspectRatioComponent],
  template: `
    <ui-aspect-ratio [ratio]="16 / 9" class="bg-muted">
      <img
        src="https://images.unsplash.com/photo-1588345921523-c2dcdb7f1dcd"
        alt="Photo by Drew Beamer"
        class="rounded-md object-cover"
      />
    </ui-aspect-ratio>
  `
})
export class AppComponent {}

API Reference

Props

PropTypeDefaultDescriptionFrameworks
rationumber1Desired aspect ratio (e.g., 16/9)All
asChildbooleanfalseRender as child elementReact, Vue, Angular
childrenReactNode-Content constrained by the ratioReact, Vue, Angular

Accessibility

Adheres to the img role requirements.

Author

Bùi Trọng Hiếu (kevinbui)

License

MIT © 2025 Bùi Trọng Hiếu (kevinbui)

Released under the MIT License.