Image / aspect ratio
An image with a fixed aspect ratio and a fallback when it fails to load.
Example
<focsd-image src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 160 90'%3E%3Crect width='160' height='90' fill='%230A5E73'/%3E%3Ccircle cx='120' cy='26' r='12' fill='%23F2B705'/%3E%3Cpath d='M0 90 50 40l30 30 20-20 60 40z' fill='%23141A18'/%3E%3C/svg%3E" alt="Illustration of hills under a sun" ratio="16/9" style="inline-size: 240px"></focsd-image>Usage
Import the module once, then use the tag like any HTML element. Every framework below renders the same element; see Frameworks for setup.
<script type="module"> import '@focsd-dev/components/focsd-image.js';</script>
<focsd-image src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 160 90'%3E%3Crect width='160' height='90' fill='%230A5E73'/%3E%3Ccircle cx='120' cy='26' r='12' fill='%23F2B705'/%3E%3Cpath d='M0 90 50 40l30 30 20-20 60 40z' fill='%23141A18'/%3E%3C/svg%3E" alt="Illustration of hills under a sun" ratio="16/9"></focsd-image>import { Image } from '@focsd-dev/react';
export function Example() { return <Image src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 160 90'%3E%3Crect width='160' height='90' fill='%230A5E73'/%3E%3Ccircle cx='120' cy='26' r='12' fill='%23F2B705'/%3E%3Cpath d='M0 90 50 40l30 30 20-20 60 40z' fill='%23141A18'/%3E%3C/svg%3E" alt="Illustration of hills under a sun" ratio="16/9"></Image>;}<script setup>import '@focsd-dev/components/focsd-image.js';</script>
<template> <focsd-image src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 160 90'%3E%3Crect width='160' height='90' fill='%230A5E73'/%3E%3Ccircle cx='120' cy='26' r='12' fill='%23F2B705'/%3E%3Cpath d='M0 90 50 40l30 30 20-20 60 40z' fill='%23141A18'/%3E%3C/svg%3E" alt="Illustration of hills under a sun" ratio="16/9"></focsd-image></template>// Add CUSTOM_ELEMENTS_SCHEMA to the component's schemas, then:import '@focsd-dev/components/focsd-image.js';
// template<focsd-image src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 160 90'%3E%3Crect width='160' height='90' fill='%230A5E73'/%3E%3Ccircle cx='120' cy='26' r='12' fill='%23F2B705'/%3E%3Cpath d='M0 90 50 40l30 30 20-20 60 40z' fill='%23141A18'/%3E%3C/svg%3E" alt="Illustration of hills under a sun" ratio="16/9"></focsd-image><script> import '@focsd-dev/components/focsd-image.js';</script>
<focsd-image src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 160 90'%3E%3Crect width='160' height='90' fill='%230A5E73'/%3E%3Ccircle cx='120' cy='26' r='12' fill='%23F2B705'/%3E%3Cpath d='M0 90 50 40l30 30 20-20 60 40z' fill='%23141A18'/%3E%3C/svg%3E" alt="Illustration of hills under a sun" ratio="16/9"></focsd-image>API
Attributes
| Attribute | Property | Type | Default |
|---|---|---|---|
src | src | string | — |
alt | alt | string | null | null |
ratio | ratio | string | — |
fit | fit | 'cover' | 'contain' | cover |
srcset | srcset | string | — |
sizes | sizes | string | — |
width | width | number | null | undefined | — |
height | height | number | null | undefined | — |
loading | loading | 'lazy' | 'eager' | lazy |
decoding | decoding | 'async' | 'sync' | 'auto' | async |
fetchpriority | fetchPriority | string | — |
crossorigin | crossOrigin | string | null | null |
referrerpolicy | referrerPolicy | string | — |
Methods
| Method | Description |
|---|---|
decode() | Resolves when the image is decoded and ready to paint; rejects when it cannot be. |
CSS parts
Style the inside of the component with ::part(), for example focsd-image::part(fallback).
| Part | Description |
|---|---|
fallback | Shown in place of the image when it fails to load. |
frame | The box that holds the image and keeps its aspect ratio. |
image | The image element. |
Accessibility
srcset,sizes,widthandheight(which also reserve the aspect ratio),loading(defaultlazy),decoding,fetchpriority,crossoriginandreferrerpolicyare forwarded;complete,naturalWidth,naturalHeightanddecode()work as on a native image.altis required: describe the image, or usealt=""for decorative images.- If loading fails, the fallback panel keeps the description (
role="img"witharia-label). - Loads lazily and decodes asynchronously by default.
Ask the docs
Hi, what are you building?
Ask about any component, theming, forms or frameworks. I answer with the right component, a snippet and a link to its docs.