Skip to content
Elements Image

Image

An img with the width, height, srcset, sizes and loading a provider fills in.

On this page

Examples

Size, format and fit

Served by the local optimizer (Klyroo.UI.Images.Local): open the network tab to see the resized WebP files.

A warm gradient with a pale sun
Width, Height, Format
A grid of colored circles
Fit cover
A warm gradient with a pale sun
Fit contain
Razor
<div class="flex flex-wrap items-start gap-6">
    @* 320 by 180 pixels, converted to WebP: the srcset holds the 1x and 2x candidates the local optimizer produced. *@
    <figure class="flex flex-col gap-2">
        <KlyImage Src="/img/sample-gradient.png" Alt="A warm gradient with a pale sun" Width="320" Height="180" Format="KlyImageFormat.WebP" Quality="80"
                 Class="rounded-lg" data-testid="image-basic" />
        <figcaption class="text-sm text-(--klyroo-text-muted)">Width, Height, Format</figcaption>
    </figure>
    @* The same source cropped to a square (fit cover, the default) and shrunk to fit inside it (fit contain). *@
    <figure class="flex flex-col gap-2">
        <KlyImage Src="/img/sample-grid.png" Alt="A grid of colored circles" Width="160" Height="160" Format="KlyImageFormat.WebP" Class="rounded-lg" data-testid="image-cover" />
        <figcaption class="text-sm text-(--klyroo-text-muted)">Fit cover</figcaption>
    </figure>
    <figure class="flex flex-col gap-2">
        <KlyImage Src="/img/sample-gradient.png" Alt="A warm gradient with a pale sun" Width="160" Height="160" Format="KlyImageFormat.WebP" Fit="KlyImageFit.Contain"
                 Class="rounded-lg bg-(--klyroo-bg-muted)" data-testid="image-contain" />
        <figcaption class="text-sm text-(--klyroo-text-muted)">Fit contain</figcaption>
    </figure>
</div>

Responsive sizes

Sizes take space-separated responsive entries: an unprefixed entry is the default, and each breakpoint applies from its width upward.

Layered waves from blue to violet
Razor
@* The sizes say how wide the image is at each breakpoint; the browser picks the candidate of the srcset that fits. *@
<KlyImage Src="/img/sample-waves.png" Alt="Layered waves from blue to violet" Width="640" Height="360"
         Sizes="100vw sm:50vw md:400px" Densities="@(new[] { 1, 2 })" Format="KlyImageFormat.WebP"
         Class="h-auto w-full max-w-xl rounded-lg" data-testid="image-responsive" />

Placeholder and priority

A warm gradient with a pale sun Layered waves from blue to violet
Razor
@* KlyImagePlaceholder.Auto paints a tiny version from the provider behind the image while the real one loads.
   Eager loading with a high fetch priority is for the image visible at first paint. *@
<div class="flex flex-wrap items-start gap-6">
    <KlyImage Src="/img/sample-gradient.png" Alt="A warm gradient with a pale sun" Width="320" Height="180" Format="KlyImageFormat.WebP"
             Placeholder="KlyImagePlaceholder.Auto" Class="rounded-lg" data-testid="image-placeholder" />
    <KlyImage Src="/img/sample-waves.png" Alt="Layered waves from blue to violet" Width="320" Height="180" Format="KlyImageFormat.WebP"
             Loading="KlyImageLoading.Eager" FetchPriority="KlyFetchPriority.High" Class="rounded-lg" data-testid="image-eager" />
</div>

Caption

A Caption wraps the image in a figure with a figcaption; nothing is wrapped without it.

A green rectangle
A green rectangle, drawn as an SVG
Razor
<div class="w-full max-w-md">
    <KlyImage Src="data:image/svg+xml;utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 5'%3E%3Crect width='8' height='5' fill='%2310b981'/%3E%3C/svg%3E"
              Alt="A green rectangle" Caption="A green rectangle, drawn as an SVG" Width="480" Height="300" />
</div>

API

KlyImage

Image element that fills in the width, height, srcset, sizes and loading attributes with the help of an image provider.

Parameters

Name Type Description
Alt (required) string Text alternative. Required: pass an empty string for a decorative image.
Caption string A caption under the image. When set, the image is wrapped in a figure with a figcaption ; Class and the other attributes stay on the img . Nothing is wrapped when it is not set.
Class string Classes added to the root element, merged last so they win over the theme.
Densities IReadOnlyList<int> Densities of the srcset , like [1, 2] . Defaults to Densities .
FetchPriority KlyFetchPriority? high speeds up the image that matters most (the hero); preloading sets it to high unless you choose. Values: Auto, High, Low.
Fit KlyImageFit? How the image fills Width by Height . Cover when unset. Values: Cover, Contain, Fill, Inside, Outside.
Format KlyImageFormat? Output format. Only a provider that converts honors it. Values: WebP, Avif, Jpeg, Png, Gif, Svg, Auto, Jxl.
Height int? Height of the image in CSS pixels, with the same rules as Width .
ImageOptions KlyImageOptions Settings shared with the other image parameters: the parameters below override the ones set here.
Loading KlyImageLoading? Lazy by default, eager when Preload is set. Use eager for the image visible at first paint. Values: Lazy, Eager.
Modifiers IReadOnlyDictionary<string, string> Provider specific modifiers (for example gravity ), merged over the preset's.
Placeholder KlyImagePlaceholder A placeholder shown behind the image while it loads: String) for a small image of yours, or Auto to ask the provider for a tiny (and, when it can blur, blurred) version of the image. It is painted as the background of the element.
Preload bool? Adds <link rel="preload" as="image" imagesrcset imagesizes> to the document head, and loads eagerly. It goes through HeadContent , so the app needs a HeadOutlet (the Blazor Web App template has one). In a Blazor Hybrid web view no link is added (the image only loads eagerly with a high priority).
Preset string Name of an entry of Presets .
Provider string Name of a registered provider; DefaultProvider when unset.
Quality int? Quality from 1 to 100.
Sizes string Responsive sizes: "100vw sm:50vw md:400px" (an unprefixed entry is the default; each breakpoint applies from its width upward). Breakpoints come from Screens . It becomes the sizes attribute and decides the widths of the srcset .
Src (required) string Address of the image: a root-relative path ("/img/team.jpg") or an absolute URL.
Ui ImageUi Per-instance class overrides of the figure and caption slots, see ImageUi . They apply only with a Caption .
Width int? Width of the image in CSS pixels. It is also the base of the 1x, 2x… candidates, and it reserves the space before the image loads. A value that is not positive is written as given and takes no part in the resolution.

Ui slots

Ui takes a ImageUi with one class string per slot, merged after the theme.

Slot Styles
Figure Classes merged into the figure slot of KlyImage , after the theme and the app configuration.
Caption Classes merged into the caption slot of KlyImage , after the theme and the app configuration.