Skip to content
Focsd homeFocsd homeFocsd
Forms

Image / aspect ratio

An image with a fixed aspect ratio and a fallback when it fails to load.

  • Tag: focsd-image
  • Pattern: Img with ratio + fallback
  • Category: Display

Example

HTML
<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>

API

Attributes

AttributePropertyTypeDefault
srcsrcstring—
altaltstring | nullnull
ratioratiostring—
fitfit'cover' | 'contain'cover
srcsetsrcsetstring—
sizessizesstring—
widthwidthnumber | null | undefined—
heightheightnumber | null | undefined—
loadingloading'lazy' | 'eager'lazy
decodingdecoding'async' | 'sync' | 'auto'async
fetchpriorityfetchPrioritystring—
crossorigincrossOriginstring | nullnull
referrerpolicyreferrerPolicystring—

Methods

MethodDescription
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).

PartDescription
fallbackShown in place of the image when it fails to load.
frameThe box that holds the image and keeps its aspect ratio.
imageThe image element.

Accessibility

  • srcset, sizes, width and height (which also reserve the aspect ratio), loading (default lazy), decoding, fetchpriority, crossorigin and referrerpolicy are forwarded; complete, naturalWidth, naturalHeight and decode() work as on a native image.
  • alt is required: describe the image, or use alt="" for decorative images.
  • If loading fails, the fallback panel keeps the description (role="img" with aria-label).
  • Loads lazily and decodes asynchronously by default.