🚀 Ship faster with premium components 🚀
Search documentation v1.4.0

Image

Render optimized images

Use the Image component to render images with consistent visuals. It supports lazy loading, fluid dimensions, aspect ratios, center alignment and rounded corners. You can use the ImageLoader wrapper to also handle loading animation and broken images.

Preview
Astro islands architecture illustration
How to use Images in Astro
---
import { Image } from 'webcoreui/astro'
---
<Image
src="/img/astro.png"
alt="Alternate text"
width={200}
height={200}
lazy={true}
/>
How to use Images in Svelte
<script>
import { Image } from 'webcoreui/svelte'
</script>
<Image
src="/img/astro.png"
alt="Alternate text"
width={200}
height={200}
lazy={true}
/>
How to use Images in React
import React from 'react'
import { Image } from 'webcoreui/react'
const Component = () => (
<Image
src="/img/astro.png"
alt="Alternate text"
width={200}
height={200}
lazy={true}
/>
)
export default Component

To use the Image component, you need to provide the following props:

Optionally, you can set the lazy property to true to lazy-load the image. If omitted, the image will be loaded eagerly.

Center Image

To center align an image, simply set the center prop to true:

Astro islands architecture illustration
How to center align images
<Image
src="/img/astro.png"
alt="Alternate text"
width={200}
height={200}
lazy={true}
center={true}
/>

Fill Available Space

To make an image fill its container, set the full prop to width, height or both:

Astro islands architecture illustration
How to make images fill their container
<Image
src="/img/astro.png"
alt="Alternate text"
width={200}
height={200}
lazy={true}
full="width"
/>

Aspect Ratio

You can also specify the aspect ratio of the image using the ratio prop that supports both width/height and width:height formats.

Images with 4:3, 16:9 and 21:9 aspect ratios
Astro islands architecture illustration
Astro islands architecture illustration
Astro islands architecture illustration
How to set aspect ratio
<Image
src="/img/astro.png"
alt="Alternate text"
width={200}
height={200}
lazy={true}
ratio="16:9"
/>

Rounded Corners

By default, images are rendered with rounded corners. You can set the rounded prop to one of the following values:

Top
Astro islands architecture illustration
Bottom
Astro islands architecture illustration
Left
Astro islands architecture illustration
Right
Astro islands architecture illustration
Diagonal
Astro islands architecture illustration
Reverse diagonal
Astro islands architecture illustration

Or disable rounded corners completely by setting the rounded prop to none.

How to customize rounded corners
<Image {...imageProps} rounded="top" />
<Image {...imageProps} rounded="bottom" />
<Image {...imageProps} rounded="left" />
<Image {...imageProps} rounded="right" />
<Image {...imageProps} rounded="diagonal" />
<Image {...imageProps} rounded="reverse-diagonal" />

Handle Loading and Broken Images

To handle loading animation and broken images, you can use the ImageLoader component, which is a wrapper around the Image component.

How to use the ImageLoader component
---
import { ImageLoader } from 'webcoreui/astro'
---
<ImageLoader
src="/img/astro.png"
fallback="/img/placeholder.png"
alt="Alternate text"
width={200}
height={200}
lazy={true}
/>

Simply replace Image with ImageLoader in your code to add a loading animation. You can also add a fallback image by specifying the fallback prop. In case the image is not found, the fallback placeholder will be displayed. You can also pass SkeletonProps to the component to customize the loading animation.

API

type ImageProps = {
src: string
alt: string
width: number | string
height: number | string
lazy?: boolean
ratio?: string
center?: boolean
full?: 'width' | 'height' | 'both'
rounded?:
| 'top'
| 'bottom'
| 'left'
| 'right'
| 'diagonal'
| 'reverse-diagonal'
| 'none'
className?: string
}
PropPurpose
src Sets the image source.
alt Sets an alternate text for the image.
width Sets the width of the image.
height Sets the height of the image.
lazy Set to true to enable lazy loading for the image.
ratio Sets the aspect ratio of the image.
center Set to true to center the image.
full Set to width, height, or both to make the image fill available space.
rounded Sets the border radius of the image.
className Pass additional CSS classes for the image.

Changelog


Request improvement for this page