Aspect Ratio
A box that holds its content to a fixed width-to-height ratio.
<arc-aspect-ratio>Overview
AspectRatio keeps its content at a fixed width-to-height ratio with the CSS aspect-ratio property. Pass a W/H string like "16/9", "4/3" or "1/1". The container is as wide as its parent and takes its height from the ratio, so it reserves the space for an image or video before the media loads and the page doesn't shift (CLS).
Every slotted child is sized to fill the box. Images and videos get object-fit: cover, so they're cropped, never letterboxed or stretched. Text that doesn't fit is clipped, with no scrolling.
Guidelines
Do
- Wrap images and videos in it to stop the page shifting as they load
- Match the ratio to the media: `16/9` for video, `4/3` for photos, `1/1` for avatars or thumbnails
- Put it in a grid or flex container that sets its width
- Use it with lazy-loaded images, since the space is reserved before the image arrives
Don't
- Don't use it when the content has its own fixed dimensions and layout shift isn't a problem
- Don't put a lot of text in it
- Don't set a fixed height on the same element. The two conflict
Features
- Decimal ratios work, such as `2.35/1` for cinematic widescreen
- An unparseable ratio, or one with a zero on either side, falls back to `16/9`
- Corners are rounded with `--radius-md`
Preview
Usage
<arc-aspect-ratio ratio="16/9">
<img src="/hero.jpg" alt="Hero banner" />
</arc-aspect-ratio>
<!-- Square thumbnail -->
<arc-aspect-ratio ratio="1/1">
<img src="/avatar.jpg" alt="User avatar" />
</arc-aspect-ratio>import { AspectRatio } from '@arclux/arc-ui-react';
export default function Example() {
return (
<>
<AspectRatio ratio="16/9">
<img src="/hero.jpg" alt="Hero banner" />
</AspectRatio>
{/* Square thumbnail */}
<AspectRatio ratio="1/1">
<img src="/avatar.jpg" alt="User avatar" />
</AspectRatio>
</>
);
}<script setup>
import { AspectRatio } from '@arclux/arc-ui-vue';
</script>
<template>
<AspectRatio ratio="16/9">
<img src="/hero.jpg" alt="Hero banner" />
</AspectRatio>
<!-- Square thumbnail -->
<AspectRatio ratio="1/1">
<img src="/avatar.jpg" alt="User avatar" />
</AspectRatio>
</template><script>
import { AspectRatio } from '@arclux/arc-ui-svelte';
</script>
<AspectRatio ratio="16/9">
<img src="/hero.jpg" alt="Hero banner" />
</AspectRatio>
<!-- Square thumbnail -->
<AspectRatio ratio="1/1">
<img src="/avatar.jpg" alt="User avatar" />
</AspectRatio>import { Component } from '@angular/core';
import { AspectRatio } from '@arclux/arc-ui-angular';
@Component({
imports: [AspectRatio],
template: `
<arc-aspect-ratio ratio="16/9">
<img src="/hero.jpg" alt="Hero banner" />
</arc-aspect-ratio>
<!-- Square thumbnail -->
<arc-aspect-ratio ratio="1/1">
<img src="/avatar.jpg" alt="User avatar" />
</arc-aspect-ratio>
`,
})
export class MyComponent {}import { AspectRatio } from '@arclux/arc-ui-solid';
export default function Example() {
return (
<>
<AspectRatio ratio="16/9">
<img src="/hero.jpg" alt="Hero banner" />
</AspectRatio>
{/* Square thumbnail */}
<AspectRatio ratio="1/1">
<img src="/avatar.jpg" alt="User avatar" />
</AspectRatio>
</>
);
}import { AspectRatio } from '@arclux/arc-ui-preact';
export default function Example() {
return (
<>
<AspectRatio ratio="16/9">
<img src="/hero.jpg" alt="Hero banner" />
</AspectRatio>
{/* Square thumbnail */}
<AspectRatio ratio="1/1">
<img src="/avatar.jpg" alt="User avatar" />
</AspectRatio>
</>
);
}<!-- Auto-generated by @arclux/prism — do not edit manually -->
<!-- arc-aspect-ratio — requires aspect-ratio.css + tokens.css (or arc-ui.css) -->
<div class="arc-aspect-ratio">
<div
class="aspect-ratio"
style="aspect-ratio: _aspect Ratio;"
>
<div class="aspect-ratio__inner">
AspectRatio
</div>
</div>
</div><!-- Auto-generated by @arclux/prism — do not edit manually -->
<!-- arc-aspect-ratio — self-contained, no external CSS needed -->
<div class="arc-aspect-ratio" style="display: block">
<div
style="position: relative; width: 100%; overflow: hidden; border-radius: 10px"
style="aspect-ratio: _aspect Ratio;"
>
<div style="width: 100%; height: 100%">
AspectRatio
</div>
</div>
</div>API
DEFAULT_RATIOstring'16/9'- The documented default, and what anything unusable normalises to.
ratiostring'16/9'- Aspect ratio as a
W/Hstring. Supports integers and decimals. An unparseable value, or one with a zero on either side, is normalised **on the property** to16/9, so readingratioback always gives the ratio the component is actually using.