Playground

Image

An image with required alt text, a ratio, and a fallback when it fails to load.

  • Built on native elements
  • alt is required; use alt="" only for purely decorative images.
  • The fallback shows the alt text so the content is not lost when loading fails.
  • Styles in one file: image.css

Import

import '@monoline/css';
import {Image} from '@monoline/react';

Not installed yet? Pick a route.

Copies the source and its styles into your project.

npx monoline add image

Usage

import {Image} from '@monoline/react';

const svg = (c: string) => 'data:image/svg+xml;utf8,' + encodeURIComponent(`<svg xmlns="http://www.w3.org/2000/svg" width="320" height="180"><rect width="320" height="180" fill="${c}"/></svg>`);

<div style={{maxWidth: '320px'}}>
  <Image src={svg('#999')} alt="A grey placeholder" ratio={16 / 9} />
</div>

Examples

Ratio

Fallback

A broken source shows the alt text.

Props

ImageProps

PropTypeDefaultDescription
alt requiredstring–Required. Describe the image, or pass "" if it is decorative.
rationumber–Width / height, for example `16 / 9`. Reserves space so the page does not jump.
fit"cover" | "contain"'cover'How the image fills its box when `ratio` crops it: `cover` (default) or `contain`.

Data Attributes

State is exposed through attributes. Style it with these selectors, not with class toggles.

This component sets no state attributes.

CSS classes

ClassStylesheet
.imageimage.css
.image-containimage.css
.image-coverimage.css
.image-fallbackimage.css

Customization

Style it with the kit's tokens in your own CSS, loaded after @monoline/css. Prefer the attributes above over extra classes. See Theming for every token.

app.css
.image {
  border-color: var(--accent);
}

Accessibility

  • alt is required; use alt="" only for purely decorative images.
  • The fallback shows the alt text so the content is not lost when loading fails.