Image
Image shows a picture with a loading placeholder, an error fallback, and lazy loading, and keeps its alt right in every state.
<tk-aspect-ratio ratio="4/3"> <tk-image src="/photos/market.jpg" alt="A market stall of fresh fruit"></tk-image> </tk-aspect-ratio>
Anatomy
| Part | What it is |
|---|---|
tk-image |
the box; the image fills it |
| image | the native <img>, fit to the box by fit |
| placeholder | shown while the image loads; a |
| fallback | shown when the image fails or src is empty; a sunken box with an image glyph |
States
<tk-aspect-ratio ratio="4/3" style="max-width: 240px"> <tk-image alt="Product photo"></tk-image> </tk-aspect-ratio>
| State | When | What shows |
|---|---|---|
| loading | src is set and not loaded yet |
the placeholder |
| loaded | the image has loaded | the image |
| error | the image failed, or src is empty |
the fallback |
- An image already in the cache never gets stuck loading.
- An empty
srcshows the fallback at once. - Changing
srcloads again, also after a failure.
Reserving space
An image without a reserved height makes the page jump when it loads. Reserve space for any image above the fold or in a list:
<tk-aspect-ratio ratio="16/9"> <tk-image src="/photos/hero.jpg" alt="The team at the spring offsite" loading="eager"></tk-image> </tk-aspect-ratio> <tk-image src="/photos/chart.png" alt="Sales by month, rising since March" width="800" height="450"></tk-image>
- Wrap it in an
Aspect ratio , or - set
widthandheightto the source's pixel size. They reserve the box before the image loads. They are the source size, not a ratio.
Without either, the image takes its natural ratio when it loads, and the page shifts.
Fit
cover(default): fills the box and crops what overflows. Use it in an Aspect ratio.contain: shows the whole image and leaves empty bands.
--tk-image-position sets the focal point (default center). --tk-image-radius rounds
the corners (default square).
<tk-aspect-ratio ratio="1/1" style="max-width: 160px"> <tk-image alt="Store logo" fit="contain" style="--tk-image-radius: var(--radius-12)"></tk-image> </tk-aspect-ratio>
Lazy loading
loading="lazy" (default) fetches the image when it nears the viewport. Use eager for
the main image at the top of a page.
Lazy loading only works with reserved space. Images without a height collapse to the top of the page, and the browser fetches them all at once.
When to use
- Use it for content images: photos, thumbnails, media cards, illustrations.
- Use an
Avatar for a person or an entity. - Resize images before you pass them. Image doesn't resize, re-encode, or build a
srcset; your framework or image CDN does that.
Content
- Meaningful image: write
altthat says what the image shows in this context. Skip "image of". - Decorative image, one that repeats nearby text or is pure decoration: set
alt="". - Always set one or the other. Without
alt, the image is treated as decorative and a warning is logged.
Do / Don't
| ✅ Do | ❌ Don't |
|---|---|
Set alt, or alt="" for decoration |
Leave alt out |
Reserve space with an Aspect ratio or width and height |
Lazy-load images with no reserved height |
Use eager for the main image at the top |
Lazy-load the largest image on the page |
| Pass an image already sized for its box | Expect Image to resize or compress |
| Use an Avatar for people | Build an avatar from an Image |
Related
Aspect ratio : reserves the box for an image.Skeleton : the loading placeholder.Avatar : a person or an entity.
API
Attributes
| Attribute | Type | Default | What it does |
|---|---|---|---|
src |
string | the image URL; empty shows the fallback | |
alt |
string | the accessible name; "" means decorative |
|
fit |
cover | contain |
cover |
crop to fill, or fit inside |
loading |
lazy | eager |
lazy |
fetch near the viewport, or at once |
width |
number | the source's pixel width, to reserve space | |
height |
number | the source's pixel height; set it with width |
Slots
| Slot | Content |
|---|---|
placeholder |
replaces the loading placeholder |
fallback |
replaces the fallback's glyph |
Events
None.
CSS
| Custom property | Default | What it does |
|---|---|---|
--tk-image-radius |
0 |
rounds the corners |
--tk-image-position |
center |
the focal point (object-position) |
Tokens
| Element | Tokens |
|---|---|
| fallback box | surface/sunken |
| fallback glyph | icon/secondary |
| placeholder | the |
Accessibility
Built in:
altstays the image's name while it loads, after it loads, and after it fails.- The placeholder is hidden from screen readers.
- A failed image is never announced as "broken image": the fallback is an image named by
your
alt, or hidden whenalt="".
Your part:
- Set
alton every image, oralt=""on a decorative one. - A custom
fallbacksits inside the named fallback box; keep it decorative.