Aspect Ratio

Keeps its content at a fixed width-to-height ratio.

AspectRatio is new in v0.4. It takes the width of its parent and sets its height from Ratio, then stretches its child to fill it. Use it for images, video embeds, maps and placeholders that must not shift the layout while loading.

Installation

bash
shellui add aspect-ratio

Usage

Basic

Ratio is width divided by height. It is a double, so write 16d / 9d: 16 / 9 is integer division in C# and gives 1.

16 : 9
<div class="w-full max-w-md">
    <AspectRatio Ratio="16d / 9d" Class="rounded-md bg-muted">
        <div class="flex items-center justify-center text-sm text-muted-foreground">16 : 9</div>
    </AspectRatio>
</div>

Common ratios

1 : 1
4 : 3
21 : 9
<div class="grid w-full gap-4 sm:grid-cols-3">
    <AspectRatio Ratio="1" Class="rounded-md bg-muted">
        <div class="flex items-center justify-center text-sm text-muted-foreground">1 : 1</div>
    </AspectRatio>
    <AspectRatio Ratio="4d / 3d" Class="rounded-md bg-muted">
        <div class="flex items-center justify-center text-sm text-muted-foreground">4 : 3</div>
    </AspectRatio>
    <AspectRatio Ratio="21d / 9d" Class="rounded-md bg-muted">
        <div class="flex items-center justify-center text-sm text-muted-foreground">21 : 9</div>
    </AspectRatio>
</div>

Images

The child is positioned to fill the box, so an image only needs object-cover.

Landscape illustration
<div class="w-full max-w-md">
    <AspectRatio Ratio="16d / 9d" Class="overflow-hidden rounded-md border border-border">
        <img src="@Image" alt="Landscape illustration" class="object-cover" />
    </AspectRatio>
</div>

@code {
    private const string Image = "data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='800' height='500'><defs><linearGradient id='s' x1='0' y1='0' x2='0' y2='1'><stop offset='0' stop-color='%2393c5fd'/><stop offset='1' stop-color='%23fde68a'/></linearGradient></defs><rect width='800' height='500' fill='url(%23s)'/><circle cx='620' cy='140' r='60' fill='%23fbbf24'/><path d='M0 380 L200 220 L360 360 L520 240 L800 400 L800 500 L0 500 Z' fill='%2334d399'/><path d='M0 430 L260 300 L460 440 L800 330 L800 500 L0 500 Z' fill='%23059669'/></svg>";
}

API Reference

Parameter Type Default Description
Ratio double 16d / 9d Width divided by height
ChildContent RenderFragment? null Content that fills the box
Class string? null Additional CSS classes

Accessibility

  • A plain container with no role of its own; give images inside it alt text

Rejoining the server...

Rejoin failed... trying again in seconds.

Failed to rejoin.
Please retry or reload the page.

The session has been paused by the server.

Failed to resume the session.
Please retry or reload the page.