Aspect Ratio
shadcn-htmx/aspect-ratioFreeComponent
A ratio-box wrapper that locks a child (image, video, iframe, embed, or chart slot) to a fixed width-to-height ratio while it resizes fluidly, eliminating layout shift. Built on the native CSS aspect-ratio property plus object-fit — one CSS declaration, zero JavaScript.
Live preview
live · rendered by the registry
Rendered by shadcn-htmx on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://shadcn-htmx.productdevbook.com/r/aspect-ratio.jsonSource
1/** @jsxImportSource hono/jsx */2import type { Child } from "hono/jsx"3import { cloneElement, isValidElement } from "hono/jsx"4import { cn, type ClassValue } from "@/registry/lib/cn"56// Aspect Ratio — shadcn-htmx, htmx v4 + Tailwind v4.7//8// A ratio-box wrapper that locks a child (image / video / iframe / embed /9// chart slot) to a fixed width-to-height ratio while it resizes fluidly,10// eliminating layout shift. One CSS declaration does all the work — there11// is no JavaScript here.12//13// shadcn/ui's upstream AspectRatio wraps Radix's primitive, which predates14// native browser support and emulates the ratio with a padding-bottom hack15// + absolute positioning. We do NOT copy that: the platform now ships the16// real thing, so we use the native CSS `aspect-ratio` property instead. No17// hacks (see AGENTS.md rule 4).18// Upstream (anatomy only):19// repos/shadcn-ui/apps/v4/registry/new-york-v4/ui/aspect-ratio.tsx20//21// Built on:22// - CSS `aspect-ratio` — defines the desired width-to-height ratio of the23// box; the browser keeps it as the box resizes. At least one of the24// box's sizes must be automatic for it to take effect (we leave height25// auto, width fluid).26// repos/mdn/files/en-us/web/css/reference/properties/aspect-ratio/index.md27// - CSS `object-fit` — how a replaced element (img/video) fills the box:28// `cover` crops to fill, `contain` letterboxes to fit. Note object-fit29// has no effect on <iframe>/<embed>, which already stretch to the box.30// repos/mdn/files/en-us/web/css/reference/properties/object-fit/index.md31// - web.dev "Aspect ratio image card" pattern (`aspect-ratio: 16 / 9`,32// no padding-top hack):33// repos/web.dev/src/site/content/en/patterns/layout/aspect-ratio-image-card/index.md34//35// Tailwind v4: `aspect-video` = 16/9, `aspect-square` = 1/1; any other36// ratio is the arbitrary `aspect-[w/h]` utility. `object-cover` /37// `object-contain` map to the object-fit keywords.3839export type AspectRatioFit = "cover" | "contain"4041// Named ratios mapped to Tailwind's stock aspect utilities; everything else42// falls through to the arbitrary `aspect-[w/h]` form below.43const NAMED_RATIO: Record<string, string> = {44 "1/1": "aspect-square",45 "16/9": "aspect-video",46}4748const fitClasses: Record<AspectRatioFit, string> = {49 cover: "object-cover",50 contain: "object-contain",51}5253// Turn a ratio prop into a Tailwind class. Accepts:54// - a number → 1.78 → aspect-[1.78]55// … truncated
More from shadcn-htmx
All 83 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Accordionaccordion | shadcn-htmx | Components | Free | no deps | |
| Active Searchactive-search | shadcn-htmx | Components | Free | no deps | |
| Alertalert | shadcn-htmx | Components | Free | no deps | |
| Alert Dialogalert-dialog | shadcn-htmx | Components | Free | no deps | |
| Auto Gridauto-grid | shadcn-htmx | Components | Free | no deps | |
| Autocompleteautocomplete | shadcn-htmx | Components | Free | no deps | |
| Autosize Textareaautosize-textarea | shadcn-htmx | Components | Free | no deps | |
| Avataravatar | shadcn-htmx | Components | Free | no deps |
