Aspect Ratio

Keeps its content to a ratio of width to height — a photo, a video, a map — whatever its width.

LayoutExperimental

Usage

import { NgOptimizedImage } from '@angular/common';import { Component, ViewEncapsulation } from '@angular/core';import { ScAspectRatio, ScFrame } from '@semantic-components/ui';
@Component({  selector: 'app-aspect-ratio-usage-demo',  imports: [NgOptimizedImage, ScAspectRatio, ScFrame],  template: `    <div scAspectRatio scFrame [ratio]="16 / 9" class="w-full max-w-sm">      <img        ngSrc="/images/gallery/forest.jpg"        fill        priority        alt="A forest path"        class="object-cover"      />    </div>  `,  host: { class: 'flex w-full justify-center' },  encapsulation: ViewEncapsulation.None,})export class AspectRatioUsageDemo {}

Anatomy

One directive, on your element; its content fills it (an image with fill, a video with size-full).

import { ScAspectRatio, ScFrame } from '@semantic-components/ui';
<div scAspectRatio scFrame [ratio]="16 / 9" class="w-full max-w-sm">  <img ngSrc="…" fill alt="…" /></div>

Notes

  • Its width is yours — a class (w-full max-w-sm) or its parent's; the height follows.
  • The ratio goes through a CSS variable, --ratio, not an inline style: a class can still change it, by breakpoint (max-sm:aspect-square).
  • It is relative: what fills it can be placed inside it. It clips what overflows its box, so a framed photo takes the frame's corners.
  • Framed: add scFrame — the style's card radius, ring and shadow (a fixed rounded-lg suits few styles).

Examples

Square

A rocky coast
import { NgOptimizedImage } from '@angular/common';import { Component, ViewEncapsulation } from '@angular/core';import { ScAspectRatio, ScFrame } from '@semantic-components/ui';
/** A square: ratio 1. */@Component({  selector: 'app-square-aspect-ratio-demo',  imports: [NgOptimizedImage, ScAspectRatio, ScFrame],  template: `    <div scAspectRatio scFrame [ratio]="1" class="w-full max-w-48">

Portrait

A narrow gorge
import { NgOptimizedImage } from '@angular/common';import { Component, ViewEncapsulation } from '@angular/core';import { ScAspectRatio, ScFrame } from '@semantic-components/ui';
/** Taller than wide: 9 / 16. */@Component({  selector: 'app-portrait-aspect-ratio-demo',  imports: [NgOptimizedImage, ScAspectRatio, ScFrame],  template: `    <div scAspectRatio scFrame [ratio]="9 / 16" class="w-full max-w-40">

Video

import { Component, ViewEncapsulation } from '@angular/core';import { ScAspectRatio, ScFrame } from '@semantic-components/ui';
/** A video in a 16 / 9 frame, filling it. */@Component({  selector: 'app-video-aspect-ratio-demo',  imports: [ScAspectRatio, ScFrame],  template: `    <div      scAspectRatio

Responsive

Mountains
import { NgOptimizedImage } from '@angular/common';import { Component, ViewEncapsulation } from '@angular/core';import { ScAspectRatio, ScFrame } from '@semantic-components/ui';
/** A class over the ratio: square on a phone, 16 / 9 beyond (the ratio is a CSS variable, so a class wins). */@Component({  selector: 'app-responsive-aspect-ratio-demo',  imports: [NgOptimizedImage, ScAspectRatio, ScFrame],  template: `    <div

RTL

مقهى
مقهى صغير في المدينة القديمة.
import { Dir } from '@angular/cdk/bidi';import { NgOptimizedImage } from '@angular/common';import { Component, LOCALE_ID, ViewEncapsulation } from '@angular/core';import { ScAspectRatio, ScFrame } from '@semantic-components/ui';
/** Right to left: the ratio doesn't change; the caption reads from the right. */@Component({  selector: 'app-rtl-aspect-ratio-demo',  imports: [Dir, NgOptimizedImage, ScAspectRatio, ScFrame],  template: `

API reference

ScAspectRatio — [scAspectRatio]

InputTypeDefaultDescription
rationumber1 Width over height: 16 / 9, 1, 9 / 16. Set as --ratio.
classstring'' Classes merged over its own (cn: a later class wins) — its width, or another ratio by breakpoint.