网页
综合
更新时间 2026-11-16 04:29:44
<NuxtImg> · @nuxt/image · Nuxt Modules
自动蜘蛛
nuxt.com
正文约 4738 字
<NuxtImg> is a drop-in replacement for the native <img> tag.
<NuxtImg> outputs a native img tag directly (without any wrapper around it). Use it like you would use the <img> tag:
The custom prop determines whether <NuxtImg> should render as a simple <img> element or only serve as a provider for custom rendering. When set to true, it disables the default rendering behavior, allowing full control over how the image is displayed. This is useful for implementing custom functionalities, such as placeholders.
When using the custom prop, <NuxtImg> passes necessary data and attributes to its default slot. You can access the following values via the v-slot directive:
This approach ensures flexibility for custom rendering scenarios, while <NuxtImg> continues to handle image optimization and data provisioning behind the scenes.
src should be in the form of an absolute path for static images in public/ directory. Otherwise path that is expected by provider that starts with / or a URL.
For image optimization when using external URLs in src, we need to whitelist them using domains option.
Specify width/height of the image.
Although Nuxt Image does not apply any special handling, it's worth mentioning the alt attribute. It is a native global attribute that specifies an alternate text for an image, if the image cannot be displayed.
It should always be provided.
Specify responsive sizes.
This is a space-separated list of screen size/width pairs. You can see a list of the defined screen sizes here.
By default Nuxt generates responsive-first sizing.
The densities prop serves high-resolution images for Retina/HiDPI screens.
Nuxt Image generates multiple versions at different pixel densities and creates the appropriate srcset.
When you specify densities, Nuxt Image multiplies your base dimensions by each density value to generate the corresponding image sizes.
Display a placeholder image before the actual image is fully loaded.
You can also use the custom prop to make any placeholder you want.
The placeholder prop can be either a string, a boolean, a number, or an array. The usage is shown below for each case.
You can also leverage useImage() to generate a placeholder image based on the original image, can be useful if the source is an SVG or you want better control on the modifiers:
When using a placeholder, you can use placeholder-class to apply a class to the original underlying <img> element (while the placeholder is being rendered).
If you need to apply some CSS to only the loaded image you can do so with something like:
Use other provider instead of default provider option specified in nuxt.config
Presets are predefined sets of image modifiers that can be used create unified form of images in your projects.
In case you want to serve images in a specific format, use this prop.
Available formats are webp, avif, jpeg, jpg, png, gif and svg. If the format is not specified, it will respect the default image format.
The quality for the generated image(s).
The fit property specifies the size of the images. There are five standard values you can use with this property.
In addition to the standard modifiers, each provider might have its own additional modifiers. Because these modifiers depend on the provider, refer to its documentation to know what can be used.
Using the modifiers prop lets you use any of these transformations.
In case you want to preload the image, use this prop. This will place a corresponding link tag in the page's head.
Passing an object to the preload prop lets you define the fetchPriority property. Note that the JavaScript property fetchPriority maps to the fetchpriority HTML attribute in the preload link tag. Accepted values are auto, high, or low.
This is a native attribute that provides a hint to the browser on how to handle the loading of an image which is outside the viewport. It is supported by the latest version of all major browsers since March 2022.
Set loading="lazy" to defer loading of an image until it appears in the viewport.
This is a native global attribute that defines a cryptographic nonce (number used once) that can be used by Content Security Policy to determine whether or not a given fetch will be allowed to proceed for a given element. Providing a nonce allows you to avoid using the CSP unsafe-inline directive, which would allowlist all inline script or styles.
Native events emitted by the <img> element contained by <NuxtImg> and <NuxtPicture> components are re-emitted and can be listened to.
Example: Listen to the native onLoad event from <NuxtImg>
Nuxt Image is configured with sensible defaults.
Discover how to use and configure the Nuxt Picture component.
Stay updated on new releases and features, guides, and community updates.
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。