Image

Image shows a picture that loads lazily, fills its box and takes the theme's corner.

Image renders one img element with no wrapper. It loads lazily, fills the box you give it and rests on a quiet neutral fill until the picture arrives. A person's photo or initials is an Avatar.

Playground

A bright room with tall windows
Riverside loft2 beds, from $180 a night

Usage

import { Card, Flex, Image, Text } from '@kushagradhawan/kookie-ui';
 
export function ListingCard() {
  return (
    <Card size="2">
      <Flex direction="column" gap="3">
        <Image
          src="/listings/loft.jpg"
          alt="A bright loft with a sofa under tall windows"
          width="100%"
          height="200px"
        />
        <Flex direction="column" gap="1">
          <Text size="2" weight="medium">Riverside loft</Text>
          <Text size="2" emphasis="medium">2 beds, from $180 a night</Text>
        </Flex>
      </Flex>
    </Card>
  );
}

Best practices

Write alt for what the picture shows. Describe what someone would miss without it, such as "A bright loft with a sofa under tall windows". The types require alt.

Pass an empty alt for decoration. When a picture adds nothing the text doesn't already say, set alt="". A screen reader then skips it.

Set a width and a height. With both set, the box holds its place while the picture loads, so the content below doesn't jump. For a picture that shrinks with its container, pass the pixel size as numbers, such as width={1200} height={800}. These become the native attributes, and the box keeps that shape at any width. The neutral fill shows in that box until the picture arrives.

Load the first picture on the screen eagerly. Image sets loading="lazy" by default. Set loading="eager" on a hero picture that is visible when the page opens, so it doesn't wait.

Don't use it for icons. An icon belongs in a HugeIcons component, which follows the text colour and size of the control around it.

Fit

fit sets how the picture fills its box, as CSS object-fit. It matters only when the box has a different shape from the picture.

FitBehaviourUse it for
coverFills the box and crops the edgesPhotos in cards and grids. This is the default
containShows the whole picture, with space around itLogos, screenshots and product shots
fillStretches to the boxPictures already cut to the box's shape
scale-downLike contain, but never larger than the pictureSmall pictures in a large box
noneKeeps the picture's own size and crops itPixel art and fixed-size assets

Size and corner

width and height as strings set the CSS size of the img itself, as any CSS length, and accept responsive objects. As numbers they are the picture's pixel size, written as the native width and height attributes. minWidth, maxWidth, minHeight and maxHeight work the same way. Without a width, the picture shows at its own size and never grows wider than its container.

radius sets the corner. With no radius, Image takes the theme's corner. radius="none" squares it.

Examples

Grid of photos

Give every Image the same height and leave fit at cover, so photos of different shapes line up.

<Grid columns="3" gap="2">
  {photos.map((photo) => (
    <Image
      key={photo.id}
      src={photo.url}
      alt={photo.description}
      width="100%"
      height="160px"
    />
  ))}
</Grid>

Logo on a card

fit="contain" shows the whole logo without cropping it.

<Image
  src="/logos/northwind.svg"
  alt="Northwind"
  width="120px"
  height="40px"
  fit="contain"
/>

Picture that fails to load

If the address is broken, the neutral fill stays in the box, so the layout doesn't change. Pass onError to replace the picture or report the failure. Image calls your onLoad and onError as usual.

Hero picture

The picture at the top of a page is visible straight away, so skip lazy loading.

<Image
  src="/hero.jpg"
  alt="The dashboard on a laptop"
  width="100%"
  height={{ initial: '240px', md: '420px' }}
  loading="eager"
/>

Props

PropTypeDefaultDescription
srcstringThe picture's address. Required
altstringThe text alternative. Required. Pass "" for decoration
fit'cover' | 'contain' | 'fill' | 'scale-down' | 'none''cover'How the picture fills its box
radius'none' | 'small' | 'medium' | 'large' | 'full'Theme radiusThe corner
widthstring, responsive, or numberA string sets the CSS width of the img. A number is the native width attribute
heightstring, responsive, or numberA string sets the CSS height of the img. A number is the native height attribute
minWidth, maxWidthstring, responsiveThe CSS minimum and maximum width
minHeight, maxHeightstring, responsiveThe CSS minimum and maximum height
loading'lazy' | 'eager''lazy'When the browser loads the picture
decoding'async' | 'sync' | 'auto''async'How the browser decodes the picture

Image also accepts every other attribute of the HTML img element. className, style and the ref all go to the img.

Accessibility

  • Name: A screen reader announces the alt text as an image. The types require alt, so a picture can't be left without one by mistake.
  • Decoration: With alt="", the picture has the presentation role and a screen reader skips it.
  • Text in pictures: Don't put words that people need inside a picture. If you must, repeat them in alt or in the text nearby.