aspect ratio

Displays content within a desired ratio.

PreviousNext
Photo
import { NgOptimizedImage } from '@angular/common';
import { ChangeDetectionStrategy, Component } from '@angular/core';

import { ZardAspectRatioComponent } from '../aspect-ratio.component';

@Component({
  selector: 'z-demo-aspect-ratio-preview',
  imports: [ZardAspectRatioComponent, NgOptimizedImage],
  template: `
    <z-aspect-ratio [zRatio]="16 / 9" class="bg-muted w-full max-w-sm rounded-lg">
      <img
        ngSrc="https://avatar.vercel.sh/shadcn1"
        alt="Photo"
        fill
        priority
        class="rounded-lg object-cover grayscale dark:brightness-20"
      />
    </z-aspect-ratio>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
  host: { class: 'contents' },
})
export class ZardDemoAspectRatioPreviewComponent {}

Installation

Copy
npx zard-cli@latest add aspect-ratio

Usage

import { ZardAspectRatioComponent } from '@/shared/components/aspect-ratio/aspect-ratio.component';
Copy
<z-aspect-ratio [zRatio]="16 / 9"></z-aspect-ratio>
Copy

Examples

square

A square aspect ratio component using [zRatio]="1 / 1". This is useful for displaying images in a square format.
Photo
import { NgOptimizedImage } from '@angular/common';
import { ChangeDetectionStrategy, Component } from '@angular/core';

import { ZardAspectRatioComponent } from '../aspect-ratio.component';

@Component({
  selector: 'z-demo-aspect-ratio-square',
  imports: [ZardAspectRatioComponent, NgOptimizedImage],
  template: `
    <z-aspect-ratio [zRatio]="1 / 1" class="bg-muted w-full max-w-48 rounded-lg">
      <img
        ngSrc="https://avatar.vercel.sh/shadcn1"
        alt="Photo"
        fill
        class="rounded-lg object-cover grayscale dark:brightness-20"
      />
    </z-aspect-ratio>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
  host: { class: 'contents' },
})
export class ZardDemoAspectRatioSquareComponent {}

portrait

A portrait aspect ratio component using [zRatio]="9 / 16". This is useful for displaying images in a portrait format.
Photo
import { NgOptimizedImage } from '@angular/common';
import { ChangeDetectionStrategy, Component } from '@angular/core';

import { ZardAspectRatioComponent } from '../aspect-ratio.component';

@Component({
  selector: 'z-demo-aspect-ratio-portrait',
  imports: [ZardAspectRatioComponent, NgOptimizedImage],
  template: `
    <z-aspect-ratio [zRatio]="9 / 16" class="bg-muted w-full max-w-40 rounded-lg">
      <img
        ngSrc="https://avatar.vercel.sh/shadcn1"
        alt="Photo"
        fill
        class="rounded-lg object-cover grayscale dark:brightness-20"
      />
    </z-aspect-ratio>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
  host: { class: 'contents' },
})
export class ZardDemoAspectRatioPortraitComponent {}

rtl

To enable RTL support in zard/ui, see the RTL configuration guide.
Photo
منظر طبيعي جميل
// You will notice this example is rendered inside a wrapper that sets the dir and data-lang attributes, and
// reads its text from a translations helper. This is because this site is not RTL by default.
// In your application you won't need either: set dir="rtl" on <html> (or use the CDK Dir directive on a subtree)
// and write the text of your own language. See the RTL guide: https://zardui.com/docs/rtl

import { NgOptimizedImage } from '@angular/common';
import { ChangeDetectionStrategy, Component } from '@angular/core';

import { ZardAspectRatioComponent } from '@/shared/components/aspect-ratio/aspect-ratio.component';
import { injectRtlTranslation, type ZardRtlTranslations } from '@/shared/demo-utils/rtl-translation';

const translations: ZardRtlTranslations<{ caption: string }> = {
  en: {
    dir: 'ltr',
    values: {
      caption: 'Beautiful landscape',
    },
  },
  ar: {
    dir: 'rtl',
    values: {
      caption: 'منظر طبيعي جميل',
    },
  },
  he: {
    dir: 'rtl',
    values: {
      caption: 'נוף יפהפה',
    },
  },
};

@Component({
  selector: 'z-demo-aspect-ratio-rtl',
  imports: [ZardAspectRatioComponent, NgOptimizedImage],
  template: `
    <figure class="w-full max-w-sm">
      <z-aspect-ratio [zRatio]="16 / 9" class="bg-muted rounded-lg">
        <img
          ngSrc="https://avatar.vercel.sh/shadcn1"
          alt="Photo"
          fill
          class="rounded-lg object-cover grayscale dark:brightness-20"
        />
      </z-aspect-ratio>
      <figcaption class="text-muted-foreground mt-2 text-center text-sm">{{ i18n.t().caption }}</figcaption>
    </figure>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
  host: { class: 'contents' },
})
export class ZardDemoAspectRatioRtlComponent {
  protected readonly i18n = injectRtlTranslation(translations);
}

API Reference

z-aspect-ratio

Displays content within a desired ratio.

Prop
zRatio

Width-to-height ratio, e.g. 16 / 9

Type
number | string
Default
1
class

Custom CSS classes

Type
ClassValue
Default
''
github iconwhatsapp icondiscord iconX icon

Made with ❤ in Brazil. Open source and available on GitHub .