Aspect Ratio

View as Markdown

A box that keeps its shape before its picture loads, such as a 16:9 video frame.

  • Linen apron, oat
    Linen apron, oat$48
  • Stoneware mug
    Stoneware mug$22
  • Walnut serving board$64
  • Cast iron grill pan
    Cast iron grill pan$85

When to use

A box that keeps its shape before its picture loads, such as a 16:9 video frame. Use Masonry instead when the pictures keep their own heights in a wall.

Usage

import { AspectImage } from "./ui/uicontext/components/AspectRatio.ts"
export const Cover = <M>(h: Html.H<M>, url: string) => AspectImage(h, { ratio: "16 / 9", src: url, alt: "Team offsite, Lisbon", shape: "rounded" })

Examples

A video frame and a round avatar

AspectRatio takes anything; give it a poster now and the player later, and the page doesn't move.

Lesson 4 · Priya Raman

API reference

AspectRatioView function

Options

AspectRatio options
NameTypeDescription
ratiorequiredstringThe shape, as CSS writes it: "16 / 9", "4 / 3", "1".
childrenrequiredView.ChildrenWhat fills the box; give an image or iframe aspectFill so it fills it.
shape"rounded" | "circle"Rounded corners, or a circle for a square box.
styleStyle—

AspectImageView function

Options

AspectImage options
NameTypeDefaultDescription
ratiorequiredstring——
srcstring—Left out, or empty, while there's no picture: a placeholder says what it was.
altrequiredstring—What the picture shows; empty for one that only decorates.
fit"cover" | "contain"——
shape"rounded" | "circle"——
loading"lazy" | "eager""lazy""lazy" by default: the box keeps the room until it scrolls near.
styleStyle——

Installation

pnpm dlx shadcn@latest add checkfu/uicontext/aspect-ratio

Source