Skip to content

Image - Vant 4

Image

🖼️ Transform your images into stunning visual experiences! The Image component is your gateway to professional-grade image handling in mobile applications.

🌟 Intro

Meet the supercharged img tag that goes beyond basic image display! With intelligent fill modes, seamless lazy loading, elegant loading states, and graceful error handling, this component ensures your images always look perfect and load efficiently. Whether you're building a photo gallery, product showcase, or content-rich interface, the Image component delivers exceptional performance and user experience.

📦 Install

Get started with professional image handling in seconds! Register the component globally via app.use for instant access throughout your application. Check out Component Registration for additional setup options.

js
import { createApp } from'vue'; import { ImageasVanImage } from'vant'; const app = createApp(); app.use(VanImage);

🎨 Usage

🌟 Basic Usage

Creating beautiful images is as simple as adding a src attribute! The Image component automatically handles loading states and provides a smooth user experience out of the box.

html

🎯 Fit Mode

Master the art of image presentation! Control exactly how your images fill their containers with powerful fit modes, leveraging the same flexibility as CSS object-fit. From perfect covers to contained displays, achieve the exact visual impact you're aiming for.

html

📍 Position

Precision positioning at your fingertips! Fine-tune where your images are positioned within their containers using the same control as CSS object-position. Perfect for highlighting specific areas or creating artistic compositions.

html

⭕ Round

Create stunning circular images with a single property! Perfect for avatars, profile pictures, and modern UI designs. Note: Works best with fill and cover fit modes for optimal visual results.

html

🚀 Lazy Load

Boost your app's performance with intelligent lazy loading! Images load only when they're about to enter the viewport, dramatically improving initial page load times and reducing bandwidth usage. Essential for image-heavy applications and mobile optimization.

html
js
import { createApp } from'vue'; import { Lazyload } from'vant'; const app = createApp(); app.use(Lazyload);

🔧 API

🎛️ Props

Customize every aspect of your image display with these comprehensive configuration options:

AttributeDescriptionTypeDefault
srcSrcstring-
fitFit mode, same as object-fitstringfill
positionPosition, same as object-position, can be set to top``right``bottom``left or stringstringcenter
altAltstring-
widthWidth*numberstring*
heightHeight*numberstring*
radiusBorder Radius*numberstring*
roundWhether to be roundbooleanfalse
block 3.6.3Whether the root node is a block elementbooleanfalse
lazy-loadWhether to enable lazy load, should register Lazyload componentbooleanfalse

| show-error | Whether to show error placeholder | boolean | true | | show-loading | Whether to show loading placeholder | boolean | true | | error-icon | Error icon | string | photo-fail | | loading-icon | Loading icon | string | photo | | icon-size | Icon size | number | string | 32px | | icon-prefix | Icon className prefix | string | van-icon | | crossorigin | same as crossorigin | string | - | | referrerpolicy | same as referrerpolicy | string | - | | decoding v4.9.20 | same as decoding | string | - |

🎨 Fit Optional Values

Choose the perfect display strategy for your images:

namedescription
containKeep aspect ratio, fully display the long side of the image
coverKeep aspect ratio, fully display the short side of the image, cutting the long side
fillStretch and resize image to fill the content box
noneNot resize image
scale-downTake the smaller of none or contain

📡 Events

Stay connected with image lifecycle events:

EventDescriptionArguments
clickEmitted when image is clickedevent: MouseEvent
loadEmitted when image loadedevent: Event
errorEmitted when image load failed-

🎰 Slots

Customize loading and error states with powerful slot options:

NameDescription
defaultCustom the content below the image
loadingCustom loading placeholder
errorCustom error placeholder

📝 Types

The component exports comprehensive type definitions for enhanced TypeScript development:

ts
importtype { ImageFit, ImagePosition, ImageProps } from'vant';

🎨 Theming

🎯 CSS Variables

Transform your image component's appearance with these comprehensive styling variables. Refer to ConfigProvider component for global theming options.

NameDefault ValueDescription
--van-image-placeholder-text-colorvar(--van-text-color-2)-
--van-image-placeholder-font-sizevar(--van-font-size-md)-
--van-image-placeholder-backgroundvar(--van-background)-
--van-image-loading-icon-size32px-
--van-image-loading-icon-colorvar(--van-gray-4)-
--van-image-error-icon-size32px-
--van-image-error-icon-colorvar(--van-gray-4)-
  • Lazyload - Image lazy loading directive
  • Icon - Icon component for loading and error states
  • Grid - Grid layout for image galleries
  • List - List component for image feeds
  • Swipe - Touch slider for image carousels
  • Loading - Loading indicator component

Enterprise-level mobile solution based on Vant