Data displayMedia
Searchastronomic.dev# Media
For a photo or picture in a fixed shape, like a project image, with an optional caption. Round, or with one corner cut.
Static: no JavaScript.
## Usage
```astro
import Media from "@astronomic-dev/ui/components/Media.astro";
```
## Examples
### Ratios and a caption
16/9 with a caption, 3/4, and a decorative 1/1 with alt="".
```astro
---
import Media from "@astronomic-dev/ui/components/Media.astro";
// Your own image files. An imported image gives Media its width and height, so the page does not jump.
import shelves from "../../photos/studio-shelves.webp";
import stairway from "../../photos/white-stairway.webp";
import plants from "../../photos/plants-shelf.webp";
---
<div class="grid gap-5 sm:grid-cols-[minmax(0,2fr)_minmax(0,1fr)_minmax(0,1fr)] sm:items-start">
<Media src={shelves} alt="Stacks of plates and bowls on two white shelves." caption="The studio's sample shelf, 16/9." />
<Media src={stairway} alt="A white stairwell seen from above." ratio="3/4" />
<Media src={plants} alt="" ratio="1/1" />
</div>
```
### Your own images
Astro's <Image> draws the photo (imageComponent={Image}), with no wrapper; the kit draws the frame and passes the sizes you set. Astro writes a srcset when your astro.config sets image.layout. For EmDash or another CMS, see "Your own images" in the kit README.
```astro
---
import Media from "@astronomic-dev/ui/components/Media.astro";
// Astro's <Image> draws the photo inside Media's frame, with no wrapper: it takes the props Media passes.
// For a srcset, set image: { layout: "constrained" } in your astro.config (or pass widths through a small wrapper).
import { Image } from "astro:assets";
import shelves from "../../photos/studio-shelves.webp";
---
<div class="mx-auto max-w-xl">
<Media src={shelves} alt="Stacks of plates and bowls on two white shelves." ratio="4/3" sizes="(min-width: 40rem) 36rem, 100vw" imageComponent={Image} caption="Drawn by Astro's <Image>, in the kit's frame." />
</div>
```
### Round portraits
shape="circle" makes the frame round and square at any width.
```astro
---
import Media from "@astronomic-dev/ui/components/Media.astro";
// shape="circle" makes the frame round and square at any width; the portraits here are 4:5, so focalPoint keeps each face in view.
import marcus from "../../photos/portrait-smiling.webp";
import dana from "../../photos/portrait-glasses.webp";
import walter from "../../photos/portrait-grey-hair.webp";
---
<div class="flex flex-wrap items-start gap-6">
<Media src={marcus} alt="A smiling young man with crossed arms in a sunlit field." shape="circle" focalPoint="50% 30%" class="w-24" />
<Media src={dana} alt="Dana Brooks, a woman with short fair hair and glasses, in a leather jacket." shape="circle" focalPoint="50% 30%" caption="Dana Brooks, studio lead" class="w-32" />
<Media src={walter} alt="A smiling gray-haired man in glasses and a green polo shirt." shape="circle" focalPoint="50% 30%" class="w-40" />
</div>
```
### Cut corners
shape="cut" slices one corner off, with soft joins (45° unless the frame is under twice the cut size). cutCorner picks it in the writing direction, so right to left mirrors it.
```astro
---
import Media from "@astronomic-dev/ui/components/Media.astro";
import desk from "../../photos/studio-desk.webp";
import plants from "../../photos/plants-shelf.webp";
import stairway from "../../photos/white-stairway.webp";
import concrete from "../../photos/concrete-slab.webp";
// shape="cut" slices one corner off. Each photo cuts the corner that faces the middle of the grid, so the four cuts face each other.
---
<div class="mx-auto grid max-w-xl grid-cols-2 items-start gap-cards">
<Media src={desk} alt="A laptop, a framed print and a cactus on a light wood desk." ratio="4/3" shape="cut" cutCorner="bottom-end" />
<Media src={plants} alt="Two potted plants on a wooden shelf." ratio="4/3" shape="cut" cutCorner="bottom-start" />
<Media src={stairway} alt="A white stairwell seen from above." ratio="4/3" shape="cut" cutCorner="top-end" />
<Media src={concrete} alt="A concrete building against the sky." ratio="4/3" shape="cut" cutCorner="top-start" caption="A concrete building, seen from the street." />
</div>
```
## Props
### Media
| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `src` | `ImageSource` | None | Required. A URL, or an imported image (Astro's image metadata): its width and height are used too. |
| `alt` | `string` | None | Required. What the image shows. Use `""` when it is only decoration. |
| `ratio` | `"16/9" \| "4/3" \| "1/1" \| "3/4"` | `"16/9"` | The frame's shape as width/height; any other pair works too. The image fills it and is cropped. |
| `shape` | `"rect" \| "circle" \| "cut"` | `"rect"` | The frame's shape: rect (default), circle or cut. A circle is always square: it sets the ratio to 1/1, and `ratio` and `ratioClass` are not used. A cut slices one corner off (see `cutCorner`), with softly rounded joins; any ratio works. The slice is 45° when both sides are at least twice the cut size; on a smaller frame the cut stops at half of each side, so its slope is shallower. Its size and rounding are the tokens `--ui-cut-size` and `--ui-cut-r`. A cut frame has no edge and no shadow (the clip would cut them); the dim stays. |
| `cutCorner` | `"top-start" \| "top-end" \| "bottom-start" \| "bottom-end"` | None | With `shape="cut"`: the corner to cut, in the writing direction (`start` is the left in left to right and the right in right to left). Default: top-end. Ignored without `shape="cut"`, with a warning in dev. |
| `ratioClass` | `string` | None | Classes for the frame's shape in place of `ratio`, for a shape that changes with the width (aspect-[4/3] md:aspect-video). When set, the image has no inline ratio and `ratio` is not used. |
| `focalPoint` | `string` | None | The point to keep in view when the frame crops the image, as `object-position`: one or two parts, each a keyword (`left`, `center`, `right`, `top`, `bottom`) or a percentage, x then y ("50% 20%", "top", "left 30%" is left and 30% down). Anything else throws. Default: the center. |
| `caption` | `string` | None | A short line under the image, in muted text. The whole becomes a `<figure>`. |
| `loading` | `"lazy" \| "eager"` | `"lazy"` | Set eager for an image near the top of the page. |
| `fetchpriority` | `"high" \| "low" \| "auto"` | None | Set high for the page's largest image (a post's header photo), so the browser fetches it first. Passed to your image component too. |
| `width` | `number` | None | The file's width, so the browser keeps the space. Default: the imported image's own. |
| `height` | `number` | None | The file's height. Default: the imported image's own. |
| `imageComponent` | `ImageComponent` | None | Your own image component, so the photo gets your srcset: Astro's <Image> as it is, or a small wrapper for your CMS (type its Props as ImageComponentProps in lib/image.ts). The frame (ratio, corners, edge, dim, fill) then sits on a box around it; the component gets src as you gave it, alt, width, height, loading, decoding, sizes, a class that fills the box, and style with only the focal point. See "Your own images" in the kit README. |
| `sizes` | `string` | None | How wide the image shows, as the sizes attribute, for imageComponent's srcset or for srcset. The plain <img> ignores it without srcset. |
| `srcset` | `string` | None | Ready image candidates for the plain <img> ("/a-400.webp 400w, /a-800.webp 800w"), such as your CMS's or getImage()'s URLs, when you use no image component. It then also writes sizes. Not used with imageComponent. |
| `class` | `string` | None | More classes, merged with cn(), so yours win on conflicts. |
| Slot | Description |
| --- | --- |
| `caption` | Your own caption content, in place of caption (for a link or a credit). |
Any other attribute goes on the outer <figure> or <div>.
## Accessibility
- alt is required. Say what the image shows; use alt="" for decoration, and the image is skipped by screen readers.
- With a caption it is a <figure> with a <figcaption>, so the caption is tied to the image.
- A circle crops more of the photo: set focalPoint so a face stays in view.
- The cut is decoration: alt stays what the photo shows. Set focalPoint so a face or a key detail stays clear of the cut corner.
- Right to left needs the dir attribute on the page or a parent: the cut mirrors by :dir(rtl).
- Images load lazily and keep their space (width, height and the ratio), so the page does not jump as they load.
Media
FreeFor a photo or picture in a fixed shape, like a project image, with an optional caption. Round, or with one corner cut.
- File
- Media.astro
- Group
- Data display
- Kind
- Static: no JavaScript
- Examples
- 4
Examples
Drag the orange handle to set the width. Frames get real breakpoints.
- Static
- Interactive
- Framed
Ratios and a caption
16/9 with a caption, 3/4, and a decorative 1/1 with alt="".
This preview did not load.
examples/media/main.astro
---
import Media from "@astronomic-dev/ui/components/Media.astro";
// Your own image files. An imported image gives Media its width and height, so the page does not jump.
import shelves from "../../photos/studio-shelves.webp";
import stairway from "../../photos/white-stairway.webp";
import plants from "../../photos/plants-shelf.webp";
---
<div class="grid gap-5 sm:grid-cols-[minmax(0,2fr)_minmax(0,1fr)_minmax(0,1fr)] sm:items-start">
<Media src={shelves} alt="Stacks of plates and bowls on two white shelves." caption="The studio's sample shelf, 16/9." />
<Media src={stairway} alt="A white stairwell seen from above." ratio="3/4" />
<Media src={plants} alt="" ratio="1/1" />
</div>Your own images
Astro's <Image> draws the photo (imageComponent={Image}), with no wrapper; the kit draws the frame and passes the sizes you set. Astro writes a srcset when your astro.config sets image.layout. For EmDash or another CMS, see "Your own images" in the kit README.
This preview did not load.
examples/media/own-image.astro
---
import Media from "@astronomic-dev/ui/components/Media.astro";
// Astro's <Image> draws the photo inside Media's frame, with no wrapper: it takes the props Media passes.
// For a srcset, set image: { layout: "constrained" } in your astro.config (or pass widths through a small wrapper).
import { Image } from "astro:assets";
import shelves from "../../photos/studio-shelves.webp";
---
<div class="mx-auto max-w-xl">
<Media src={shelves} alt="Stacks of plates and bowls on two white shelves." ratio="4/3" sizes="(min-width: 40rem) 36rem, 100vw" imageComponent={Image} caption="Drawn by Astro's <Image>, in the kit's frame." />
</div>Round portraits
shape="circle" makes the frame round and square at any width.
This preview did not load.
examples/media/circle.astro
---
import Media from "@astronomic-dev/ui/components/Media.astro";
// shape="circle" makes the frame round and square at any width; the portraits here are 4:5, so focalPoint keeps each face in view.
import marcus from "../../photos/portrait-smiling.webp";
import dana from "../../photos/portrait-glasses.webp";
import walter from "../../photos/portrait-grey-hair.webp";
---
<div class="flex flex-wrap items-start gap-6">
<Media src={marcus} alt="A smiling young man with crossed arms in a sunlit field." shape="circle" focalPoint="50% 30%" class="w-24" />
<Media src={dana} alt="Dana Brooks, a woman with short fair hair and glasses, in a leather jacket." shape="circle" focalPoint="50% 30%" caption="Dana Brooks, studio lead" class="w-32" />
<Media src={walter} alt="A smiling gray-haired man in glasses and a green polo shirt." shape="circle" focalPoint="50% 30%" class="w-40" />
</div>Cut corners
shape="cut" slices one corner off, with soft joins (45° unless the frame is under twice the cut size). cutCorner picks it in the writing direction, so right to left mirrors it.
This preview did not load.
examples/media/cut.astro
---
import Media from "@astronomic-dev/ui/components/Media.astro";
import desk from "../../photos/studio-desk.webp";
import plants from "../../photos/plants-shelf.webp";
import stairway from "../../photos/white-stairway.webp";
import concrete from "../../photos/concrete-slab.webp";
// shape="cut" slices one corner off. Each photo cuts the corner that faces the middle of the grid, so the four cuts face each other.
---
<div class="mx-auto grid max-w-xl grid-cols-2 items-start gap-cards">
<Media src={desk} alt="A laptop, a framed print and a cactus on a light wood desk." ratio="4/3" shape="cut" cutCorner="bottom-end" />
<Media src={plants} alt="Two potted plants on a wooden shelf." ratio="4/3" shape="cut" cutCorner="bottom-start" />
<Media src={stairway} alt="A white stairwell seen from above." ratio="4/3" shape="cut" cutCorner="top-end" />
<Media src={concrete} alt="A concrete building against the sky." ratio="4/3" shape="cut" cutCorner="top-start" caption="A concrete building, seen from the street." />
</div>