Skip to content
On this page Ratio

Display

Image

A responsive image that reserves its space (no layout shift), loads lazily, and preloads the one above-the-fold image.

<script>
  import { Image } from "@codeatlasdev/ui";
</script>

<Image src="/hero.avif" alt="Dashboard overview" width={1200} height={630} priority />

width and height are the intrinsic pixel size of the file. The browser derives the aspect ratio from them before the image arrives, so nothing jumps when it loads, and max-width: 100% with height: auto keeps that ratio on small screens. Images are loading="lazy" and decoding="async" by default.

Use priority on the one image that is the largest thing visible on first paint (the LCP element). It loads eagerly, sets fetchpriority="high" and adds a <link rel="preload" as="image"> to <head>. Never put it on more than one image per page.

#127

Fixed ratio

The 800 by 600 file renders at 360px wide and keeps its 4:3 box.

Gradient placeholder, 4 by 3

Reference #127 · Pick in the gallery

Code
svelte
<script lang="ts">
  import { Image } from '@codeatlasdev/ui';

  // A 4:3 placeholder, inlined so the demo needs no asset.
  const src =
    "data:image/svg+xml;utf8," +
    encodeURIComponent(
      `<svg xmlns='http://www.w3.org/2000/svg' width='800' height='600'><defs><linearGradient id='g' x1='0' y1='0' x2='1' y2='1'><stop offset='0' stop-color='#c47a15'/><stop offset='1' stop-color='#2b5fd9'/></linearGradient></defs><rect width='800' height='600' fill='url(#g)'/></svg>`
    );
</script>

<div style="max-width: 360px">
  <Image {src} alt="Gradient placeholder, 4 by 3" width={800} height={600} />
</div>
#128

srcset and sizes

The browser picks the smallest rendition that covers the rendered width.

Two renditions of the same 16 by 9 picture

Reference #128 · Pick in the gallery

Code
svelte
<script lang="ts">
  import { Image } from '@codeatlasdev/ui';

  const svg = (w: number, h: number, c: string) =>
    "data:image/svg+xml;utf8," +
    encodeURIComponent(`<svg xmlns='http://www.w3.org/2000/svg' width='${w}' height='${h}'><rect width='${w}' height='${h}' fill='${c}'/></svg>`);
  const small = svg(400, 225, '#2b5fd9');
  const large = svg(800, 450, '#1a7a50');
</script>

<div style="max-width: 420px">
  <Image
    src={large}
    srcset={`${small} 400w, ${large} 800w`}
    sizes="(min-width: 768px) 420px, 100vw"
    alt="Two renditions of the same 16 by 9 picture"
    width={800}
    height={450}
  />
</div>
#960

ratio and fit

One 4:3 file in a 16:9 box, a square box and a contained square.

Wide cover
16 / 9
Square cover
1
Contained
1, contain

Reference #960 · Pick in the gallery

Code
svelte
<script lang="ts">
  import { Image } from '@codeatlasdev/ui';

  // A 4:3 file cropped into three boxes; inlined so the demo needs no asset.
  const src =
    "data:image/svg+xml;utf8," +
    encodeURIComponent(
      `<svg xmlns='http://www.w3.org/2000/svg' width='800' height='600'><defs><linearGradient id='g' x1='0' y1='0' x2='1' y2='1'><stop offset='0' stop-color='#c47a15'/><stop offset='1' stop-color='#2b5fd9'/></linearGradient></defs><rect width='800' height='600' fill='url(#g)'/><circle cx='400' cy='300' r='140' fill='#fff' fill-opacity='.35'/></svg>`
    );
</script>

<div class="row">
  <figure><Image {src} alt="Wide cover" width={800} height={600} ratio={16 / 9} /><figcaption>16 / 9</figcaption></figure>
  <figure><Image {src} alt="Square cover" width={800} height={600} ratio={1} /><figcaption>1</figcaption></figure>
  <figure><Image {src} alt="Contained" width={800} height={600} ratio={1} fit="contain" /><figcaption>1, contain</figcaption></figure>
</div>

<style>
  .row { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(8rem, 100%), 1fr)); gap: var(--ca-space-3); width: 100%; max-width: 32rem; }
  figure { margin: 0; }
  figcaption { margin-top: var(--ca-space-1); color: var(--ca-text-secondary); font-size: var(--ca-font-size-caption); }
</style>

Ratio

ratio crops the image to a box of that width to height (16 / 9, 1) and fills the width of its container, so a grid of covers or thumbnails shares one shape whatever the files measure. fit="cover" (default) crops, fit="contain" letterboxes. Without ratio the width and height attributes keep the picture’s own shape. For a video, a map or an embed use AspectRatio.

For AVIF and WebP generation at build time see Performance.

Alt text

alt is required. An image that carries no meaning passes decorative instead of alt=""; it renders alt="" with aria-hidden="true". In development the component warns when alt="" is passed without decorative, when width or height is missing, and when decorative is combined with a non-empty alt.

Props

PropTypeDefaultDescription
srcstring–Image URL.
altstring–Text alternative. Required; use `decorative` for decoration.
widthnumber–Intrinsic width in pixels. Required, with `height` it reserves the space.
heightnumber–Intrinsic height in pixels. Required.
decorativebooleanfalseRenders `alt=""` and `aria-hidden="true"`.
prioritybooleanfalseEager load, `fetchpriority="high"` and a preload link in `<head>`. One per page, for the LCP image.
rationumberundefinedCrops the image to a box of this width / height and fills the container width.
fit'cover' | 'contain''cover'With `ratio`: crop (`cover`) or letterbox (`contain`).
loading'lazy' | 'eager''lazy' ('eager' with priority)Overrides the default.
decoding'async' | 'sync' | 'auto''async'Decode hint.
classstring''Extra class names.

Other <img> attributes are forwarded as they are. srcset and sizes are the ones you will use most: srcset lists the candidate renditions and sizes tells the browser the rendered width. With priority, both are copied onto the preload link in <head>. fetchpriority is controlled by priority.