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 and Aspect Ratios
Vanilla Breeze provides variant classes for common image treatments: .rounded, .circle, .thumbnail, .full, and aspect ratio classes like .ratio-square, .ratio-video, .ratio-portrait, and .ratio-landscape.
Variant Classes
<img src="photo.jpg" alt="A scenic landscape photograph" />
<img src="photo.jpg" alt="..." class="rounded" />
<img src="avatar.jpg" alt="User name" class="circle" style="max-inline-size: 96px;" />
<img src="thumb.jpg" alt="..." class="thumbnail" />
<img src="banner.jpg" alt="..." class="full" />
Aspect Ratio Classes
| 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.
<!-- Contain: letterbox to fit --><img src="tall.jpg" alt="..." class="contain" style="max-block-size: 200px;" /> <!-- Cover: crop to fill --><img src="photo.jpg" alt="..." class="cover" style="block-size: 200px; inline-size: 100%;" />
Lazy Loading
Native lazy loading defers loading images until they approach the viewport, improving initial page load performance. Lazy-loaded images display a subtle background color while loading.
<!-- Lazy load images below the fold --><img src="photo.jpg" alt="..." loading="lazy" /> <!-- Eager load critical above-the-fold images --><img src="hero.jpg" alt="..." loading="eager" />
Image Links
When an <img> is the direct child of an <a>, Vanilla Breeze automatically removes the underline decoration via a:has(> img) { text-decoration: none }. This prevents an unsightly underline bar beneath linked images.
<a href="/profile"> <img src="avatar.jpg" alt="View your profile" /></a>
See the <a> element docs for more on link styling and auto-detected link types.
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
<!-- Informative image --><img src="team.jpg" alt="The engineering team celebrating the product launch" /> <!-- Decorative image --><img src="divider.png" alt="" role="presentation" /> <!-- Functional image (link) --><a href="/profile"> <img src="avatar.jpg" alt="View your profile" /></a>
Related
<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<a>- Image links with automatic underline removal<layout-card>- Cards that commonly contain images<compare-surface>- Before/after image comparison slider
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); |