img
The image element embeds responsive images with automatic sizing, lazy loading support, and visual variants for different use cases.
Description
The <img> element is the primary way to embed images in HTML. Vanilla Breeze styles images to be responsive by default, constraining them to their container width while maintaining aspect ratio. Additional variant classes provide shapes, borders, and aspect ratio control.
Images are block-level elements with max-inline-size: 100% and block-size: auto, ensuring they never overflow their container while preserving their natural proportions.
When to Use
- Content images - Photos, illustrations, and graphics that are part of the content
- Avatars - User profile pictures using the
.circlevariant - Thumbnails - Small preview images with borders
- Hero images - Large banner images using
.fullvariant - Product images - E-commerce product photos with consistent aspect ratios
When to Use Other Elements
Variants
Default
Images are block-level and constrained to their container width.
.rounded
Applies medium border radius for softer edges.
.circle
Creates a circular crop with 1:1 aspect ratio. Ideal for avatars and profile pictures.
.thumbnail
Adds padding and a subtle border for gallery-style thumbnails.
.full
Forces the image to fill its container width completely.
Aspect Ratios
Force consistent aspect ratios with automatic cropping via object-fit: cover.
| Class | Aspect Ratio | Use Case |
|---|---|---|
.ratio-square |
1:1 | Profile photos, app icons |
.ratio-video |
16:9 | Video thumbnails, hero images |
.ratio-portrait |
3:4 | Book covers, portraits |
.ratio-landscape |
4:3 | Classic photo format |
Object Fit
Control how images fit within constrained dimensions.
Scales to fit within bounds, preserving aspect ratio. May show letterboxing.
Fills container completely, cropping as needed.
Lazy Loading
Native lazy loading defers loading images until they approach the viewport, improving initial page load performance.
Loading States
Lazy-loaded images display a subtle background color while loading. Broken or missing images show a placeholder.
Accessibility
Alt Text Guidelines
Every image must have an alt attribute. The content depends on the image's purpose:
| Image Type | Alt Text Approach | Example |
|---|---|---|
| Informative | Describe the content and meaning | alt="Golden retriever playing fetch in a park" |
| Functional | Describe the action or destination | alt="Submit form" |
| Decorative | Empty alt attribute | alt="" |
| Complex | Brief summary + detailed description nearby | alt="Q3 sales chart. Details in table below." |
Best Practices
- Keep alt text concise (under 125 characters when possible)
- Don't start with "Image of" or "Picture of" - screen readers announce this
- Include relevant text that appears in the image
- For decorative images, use
alt=""(not omitting the attribute) - Use
role="presentation"for purely decorative images
Related Elements
<picture>- Art direction and format switching with multiple sources<source>- Define image sources within picture element<figure>- Semantic wrapper for images with captions<figcaption>- Caption text for figures<svg>- Vector graphics and icons
CSS Reference
Styles defined in /src/native-elements/image/styles.css
| Selector | Properties |
|---|---|
img |
max-inline-size: 100%; block-size: auto; display: block; |
img.rounded |
border-radius: var(--radius-m); |
img.circle |
border-radius: var(--radius-full); aspect-ratio: 1; object-fit: cover; |
img.thumbnail |
padding; background; border; border-radius; |
img.contain |
object-fit: contain; |
img.cover |
object-fit: cover; |
img[loading="lazy"] |
background: var(--color-surface-raised); |