Primitives
Aspect Ratio
A container that keeps its content at a fixed width-to-height ratio.
Resize the preview and watch the frame hold its 16 / 9 ratio. The primitive writes the ratio and nothing else, so every border, colour and inset below is your own CSS.
It is a pure visual utility that locks an element's box via the native CSS aspect-ratio property, with no ARIA semantics. Reach for it to reserve space for media before it loads (preventing layout shift), keep cards on a grid uniform, or wrap responsive iframes.
Why this exists
You don't need this primitive for a fixed, never-changing ratio, which is one line of CSS:
.box {
aspect-ratio: 16 / 9;
}
[forAspectRatio] earns its place when the ratio is dynamic or must be validated. It is more than the static declaration:
- Reactive
ratioinput. Bind[ratio]="ratio()"and the host style recomputes as the value changes, with no manual style writes. - Invalid-value guarding.
0, negative, and non-finite ratios fall back to1, so a bad computed value never emits invalid CSS. - SSR-safe. The
aspect-ratiostyle is bound declaratively (never touched imperatively), so it renders identically on the server and hydrates cleanly. - Consistent headless API. Same shape as the other primitives, so it composes the same way.
If your ratio is a literal constant, prefer the CSS property directly and keep the bundle leaner. Import [forAspectRatio] when reactivity or validation buys you something.
Anatomy
<div forAspectRatio [ratio]="16 / 9">
<!-- your content fills the box -->
</div>
Examples
Square (1 / 1)
Set ratio to 1 to keep a box perfectly square at any width. That is handy for avatars, thumbnails, or uniform grid cards.
API
ForAspectRatio
| Property | Type | Description |
|---|---|---|
ratio | input | Width / height ratio (e.g. 16 / 9, 4 / 3, 1). Accepts both numeric expressions and string attributes. Non-positive or non-finite values fall back to 1.Default: 1 |
| Data attribute | Values | |
|---|---|---|
[forAspectRatio] | present |
Styling
forty-cdk ships no styles: put your own class on the host rather than styling the for* selector (Styling forty-cdk explains why). This primitive is purely structural: its only host effect is the native aspect-ratio style, so it reflects no data-* attributes and writes no CSS custom properties.
Behavior notes
- Browser support. Native
aspect-ratiois in Baseline 2021 (Chrome 88+, Firefox 89+, Safari 15+), so no polyfill is needed on any browser Angular itself supports. - Width still on you. The directive only sets
aspect-ratio; you decide width / max-width / display. The height is computed from the ratio. - Children fill the box. Use
width: 100%; height: 100%; object-fit: coveron inner media to fill without distortion. The directive imposes no styles on children. - No role, no a11y. This is a layout utility. The element it sits on keeps whatever semantics you give it (
<div>,<figure>,<a>, …).