Getting Started
UI
Components
- Accordion
- Alert
- Alert Dialog
- Aspect Ratio
- Attachment
- Autocomplete
- Avatar
- Badge
- Breadcrumb
- Bubble
- Button
- Button Group
- Calendar
- Card
- Carousel
- Chart
- Checkbox
- Code Block
- Collapsible
- Combobox
- Command
- Context Menu
- Data Table
- Date Picker
- Dialog
- Drawer
- Dropdown Menu
- Empty
- Field
- Hover Card
- Input Group
- Input OTP
- Input
- Item
- Kbd
- Label
- Marker
- Menubar
- Message
- Message Scroller
- Native Select
- Navigation Menu
- Pagination
- Popover
- Progress
- Questionnaire
- Radio Group
- Resizable
- Scroll Area
- Select
- Separator
- Sheet
- Sidebar
- Skeleton
- Slider
- Sonner (Toast)
- Spinner
- Switch
- Table
- Tabs
- Textarea
- Toggle
- Toggle Group
- Tooltip
- Trace
Utilities
Forms
Stack
Aspect Ratio
Displays content within a desired ratio.

import { NgOptimizedImage } from '@angular/common';
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { HlmAspectRatioImports } from '@spartan-ng/helm/aspect-ratio';
@Component({
selector: 'spartan-aspect-ratio-preview',
imports: [HlmAspectRatioImports, NgOptimizedImage],
changeDetection: ChangeDetectionStrategy.OnPush,
host: {
class: 'flex items-center justify-center h-72 w-full',
},
template: `
<div [hlmAspectRatio]="16 / 9" class="w-full max-w-sm overflow-hidden">
<img ngSrc="/assets/mountains.jpg" fill alt="Mountain views" class="rounded-lg object-cover" />
</div>
`,
})
export class AspectRatioPreview {}Installation
ng g @spartan-ng/cli:ui aspect-rationx g @spartan-ng/cli:ui aspect-ratioimport { OVERLAY_DEFAULT_CONFIG } from '@angular/cdk/overlay';
import { brnClasses, type BrnClassesOptions } from '@spartan-ng/brain/core';
import { cn, type ClassValue } from 'cn';
import { createCn } from 'cn/config';
import { makeEnvironmentProviders, type EnvironmentProviders } from '@angular/core';
import { provideSpartanHlm } from '@spartan-ng/helm/utils';
/**
* Merges class values and resolves Tailwind CSS conflicts.
*
* To customize the merge (e.g. register custom theme values), create your own instance:
*
* @example
* ```ts
*
*
* export const hlm = createCn({
* extend: { classGroups: { 'font-size': [{ text: ['hero'] }] } },
* });
* ```
*/
export const hlm = cn;
/**
* This function dynamically adds and removes classes for a given element without requiring
* the a class binding (e.g. `[class]="..."`) which may interfere with other class bindings.
* The classes are merged with `hlm`.
*/
export function classes(computed: () => ClassValue[] | string, options: Omit<BrnClassesOptions, 'merge'> = {}) {
brnClasses(computed, { ...options, merge: hlm });
}
/**
* Provides default configuration for Spartan Helm components.
*
* This utility configures the Angular CDK overlay to disable the `usePopover`
* behavior introduced in Angular 21, which causes CDK overlay-based components
* (sheets, dialogs, tooltips, etc.) to render above `position: fixed` elements
* like `<hlm-toaster>`.
*
* @returns {EnvironmentProviders} Environment providers to be added to the application config.
*
* @example
* ```ts
* // app.config.ts
*
*
* export const appConfig: ApplicationConfig = {
* providers: [
* provideSpartanHlm(),
* // ... other providers
* ],
* };
* ```
*/
export function provideSpartanHlm(): EnvironmentProviders {
return makeEnvironmentProviders([
{
provide: OVERLAY_DEFAULT_CONFIG,
useValue: { usePopover: false },
},
]);
}import { Directive, input } from '@angular/core';
import { classes } from '@spartan-ng/helm/utils';
import { coerceNumberProperty, type NumberInput } from '@angular/cdk/coercion';
const parseDividedString = (value: NumberInput): NumberInput => {
if (typeof value !== 'string' || !value.includes('/')) return value;
return value
.split('/')
.map((v) => Number.parseInt(v, 10))
.reduce((a, b) => a / b);
};
@Directive({
selector: '[hlmAspectRatio]',
exportAs: 'hlmAspectRatio',
host: {
'data-slot': 'aspect-ratio',
'[style.--ratio]': 'ratio()',
},
})
export class HlmAspectRatio {
/**
* Aspect ratio of the element, defined as width / height.
*/
public readonly ratio = input<number, NumberInput>(1, {
alias: 'hlmAspectRatio',
transform: (value: NumberInput) => {
const coerced = coerceNumberProperty(parseDividedString(value));
return coerced <= 0 ? 1 : coerced;
},
});
constructor() {
classes(() => 'relative aspect-(--ratio)');
}
}
export const HlmAspectRatioImports = [HlmAspectRatio] as const;import { Directive, input } from '@angular/core';
import { classes } from '@spartan-ng/helm/utils';
import { coerceNumberProperty, type NumberInput } from '@angular/cdk/coercion';
const parseDividedString = (value: NumberInput): NumberInput => {
if (typeof value !== 'string' || !value.includes('/')) return value;
return value
.split('/')
.map((v) => Number.parseInt(v, 10))
.reduce((a, b) => a / b);
};
@Directive({
selector: '[hlmAspectRatio]',
exportAs: 'hlmAspectRatio',
host: {
'data-slot': 'aspect-ratio',
'[style.--ratio]': 'ratio()',
},
})
export class HlmAspectRatio {
/**
* Aspect ratio of the element, defined as width / height.
*/
public readonly ratio = input<number, NumberInput>(1, {
alias: 'hlmAspectRatio',
transform: (value: NumberInput) => {
const coerced = coerceNumberProperty(parseDividedString(value));
return coerced <= 0 ? 1 : coerced;
},
});
constructor() {
classes(() => 'relative aspect-(--ratio)');
}
}
export const HlmAspectRatioImports = [HlmAspectRatio] as const;import { Directive, input } from '@angular/core';
import { classes } from '@spartan-ng/helm/utils';
import { coerceNumberProperty, type NumberInput } from '@angular/cdk/coercion';
const parseDividedString = (value: NumberInput): NumberInput => {
if (typeof value !== 'string' || !value.includes('/')) return value;
return value
.split('/')
.map((v) => Number.parseInt(v, 10))
.reduce((a, b) => a / b);
};
@Directive({
selector: '[hlmAspectRatio]',
exportAs: 'hlmAspectRatio',
host: {
'data-slot': 'aspect-ratio',
'[style.--ratio]': 'ratio()',
},
})
export class HlmAspectRatio {
/**
* Aspect ratio of the element, defined as width / height.
*/
public readonly ratio = input<number, NumberInput>(1, {
alias: 'hlmAspectRatio',
transform: (value: NumberInput) => {
const coerced = coerceNumberProperty(parseDividedString(value));
return coerced <= 0 ? 1 : coerced;
},
});
constructor() {
classes(() => 'relative aspect-(--ratio)');
}
}
export const HlmAspectRatioImports = [HlmAspectRatio] as const;import { Directive, input } from '@angular/core';
import { classes } from '@spartan-ng/helm/utils';
import { coerceNumberProperty, type NumberInput } from '@angular/cdk/coercion';
const parseDividedString = (value: NumberInput): NumberInput => {
if (typeof value !== 'string' || !value.includes('/')) return value;
return value
.split('/')
.map((v) => Number.parseInt(v, 10))
.reduce((a, b) => a / b);
};
@Directive({
selector: '[hlmAspectRatio]',
exportAs: 'hlmAspectRatio',
host: {
'data-slot': 'aspect-ratio',
'[style.--ratio]': 'ratio()',
},
})
export class HlmAspectRatio {
/**
* Aspect ratio of the element, defined as width / height.
*/
public readonly ratio = input<number, NumberInput>(1, {
alias: 'hlmAspectRatio',
transform: (value: NumberInput) => {
const coerced = coerceNumberProperty(parseDividedString(value));
return coerced <= 0 ? 1 : coerced;
},
});
constructor() {
classes(() => 'relative aspect-(--ratio)');
}
}
export const HlmAspectRatioImports = [HlmAspectRatio] as const;import { Directive, input } from '@angular/core';
import { classes } from '@spartan-ng/helm/utils';
import { coerceNumberProperty, type NumberInput } from '@angular/cdk/coercion';
const parseDividedString = (value: NumberInput): NumberInput => {
if (typeof value !== 'string' || !value.includes('/')) return value;
return value
.split('/')
.map((v) => Number.parseInt(v, 10))
.reduce((a, b) => a / b);
};
@Directive({
selector: '[hlmAspectRatio]',
exportAs: 'hlmAspectRatio',
host: {
'data-slot': 'aspect-ratio',
'[style.--ratio]': 'ratio()',
},
})
export class HlmAspectRatio {
/**
* Aspect ratio of the element, defined as width / height.
*/
public readonly ratio = input<number, NumberInput>(1, {
alias: 'hlmAspectRatio',
transform: (value: NumberInput) => {
const coerced = coerceNumberProperty(parseDividedString(value));
return coerced <= 0 ? 1 : coerced;
},
});
constructor() {
classes(() => 'relative aspect-(--ratio)');
}
}
export const HlmAspectRatioImports = [HlmAspectRatio] as const;import { Directive, input } from '@angular/core';
import { classes } from '@spartan-ng/helm/utils';
import { coerceNumberProperty, type NumberInput } from '@angular/cdk/coercion';
const parseDividedString = (value: NumberInput): NumberInput => {
if (typeof value !== 'string' || !value.includes('/')) return value;
return value
.split('/')
.map((v) => Number.parseInt(v, 10))
.reduce((a, b) => a / b);
};
@Directive({
selector: '[hlmAspectRatio]',
exportAs: 'hlmAspectRatio',
host: {
'data-slot': 'aspect-ratio',
'[style.--ratio]': 'ratio()',
},
})
export class HlmAspectRatio {
/**
* Aspect ratio of the element, defined as width / height.
*/
public readonly ratio = input<number, NumberInput>(1, {
alias: 'hlmAspectRatio',
transform: (value: NumberInput) => {
const coerced = coerceNumberProperty(parseDividedString(value));
return coerced <= 0 ? 1 : coerced;
},
});
constructor() {
classes(() => 'relative aspect-(--ratio)');
}
}
export const HlmAspectRatioImports = [HlmAspectRatio] as const;Usage
import { HlmAspectRatioImports } from '@spartan-ng/helm/aspect-ratio';<div [hlmAspectRatio]="16 / 9">
<img src="..." alt="Image" class="rounded-md object-cover" />
</div>Examples
Square
A square aspect ratio component using the [hlmAspectRatio]="1 / 1" prop. This is useful for displaying images in a square format.

import { NgOptimizedImage } from '@angular/common';
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { HlmAspectRatioImports } from '@spartan-ng/helm/aspect-ratio';
@Component({
selector: 'spartan-aspect-ratio-square',
imports: [HlmAspectRatioImports, NgOptimizedImage],
changeDetection: ChangeDetectionStrategy.OnPush,
host: {
class: 'flex items-center justify-center h-72 w-full',
},
template: `
<div [hlmAspectRatio]="1 / 1" class="w-full max-w-48 overflow-hidden">
<img ngSrc="/assets/mountains.jpg" fill alt="Mountain views" class="rounded-lg object-cover" />
</div>
`,
})
export class AspectRatioSquare {}Portrait
A portrait aspect ratio component using the [hlmAspectRatio]="9 / 16" prop. This is useful for displaying images in a portrait format.

import { NgOptimizedImage } from '@angular/common';
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { HlmAspectRatioImports } from '@spartan-ng/helm/aspect-ratio';
@Component({
selector: 'spartan-aspect-ratio-portrait',
imports: [HlmAspectRatioImports, NgOptimizedImage],
changeDetection: ChangeDetectionStrategy.OnPush,
host: {
class: 'flex items-center justify-center h-72 w-full',
},
template: `
<div [hlmAspectRatio]="9 / 16" class="w-full max-w-40 overflow-hidden">
<img ngSrc="/assets/mountains.jpg" fill alt="Mountain views" class="rounded-lg object-cover" />
</div>
`,
})
export class AspectRatioPortrait {}RTL
To enable RTL support in spartan-ng, see the RTL configuration guide.

import { NgOptimizedImage } from '@angular/common';
import { ChangeDetectionStrategy, Component, computed, inject } from '@angular/core';
import { TranslateService, Translations } from '@spartan-ng/app/app/shared/translate.service';
import { HlmAspectRatioImports } from '@spartan-ng/helm/aspect-ratio';
@Component({
selector: 'spartan-aspect-ratio-rtl',
imports: [HlmAspectRatioImports, NgOptimizedImage],
changeDetection: ChangeDetectionStrategy.OnPush,
host: {
class: 'flex items-center justify-center h-72 w-full',
'[dir]': '_dir()',
},
template: `
<figure class="w-full max-w-sm">
<div [hlmAspectRatio]="16 / 9" class="overflow-hidden">
<img ngSrc="/assets/mountains.jpg" fill alt="Mountain views" class="rounded-lg object-cover" />
</div>
<figcaption class="text-muted-foreground mt-2 text-start text-sm">
{{ _t()['caption'] }}
</figcaption>
</figure>
`,
})
export class AspectRatioRtl {
private readonly _language = inject(TranslateService).language;
private readonly _translations: Translations = {
en: {
dir: 'ltr',
values: {
caption: 'Beautiful landscape',
},
},
ar: {
dir: 'rtl',
values: {
caption: 'منظر طبيعي جميل',
},
},
he: {
dir: 'rtl',
values: {
caption: 'נוף יפה',
},
},
};
private readonly _translation = computed(() => this._translations[this._language()]);
protected readonly _t = computed(() => this._translation().values);
protected readonly _dir = computed(() => this._translation().dir);
}Helm API
HlmAspectRatio
Selector: [hlmAspectRatio]
ExportAs: hlmAspectRatio
Inputs
| Prop | Type | Default | Description |
|---|---|---|---|
| hlmAspectRatio | number | 1 | Aspect ratio of the element, defined as width / height. |
On This Page
Stop configuring. Start shipping.
Zerops powers spartan.ng and Angular teams worldwide.
One-command deployment. Zero infrastructure headaches.
Deploy with Zerops