ESL Image Utils

Lightweight helpers for use with native img and picture elements.

ESL Image Container Mixin

ESLImageContainerMixin (esl-image-container) is a custom attribute used to set an image container class once the image has loaded.

This mixin is intended to be added to the image container element (e.g., div, picture, etc.) but can also be added directly to the image element.

The mixin observes all images inside the host element. A ready class is applied to the host element when all images have finished loading (either successfully or with an error). An error class is applied to the host element if any image fails to load.

Configuration

The mixin uses a primary attribute, esl-image-container, with optional configuration passed as a JSON attribute value.

Configuration options:

Usage

Apply the default img-container-loaded class to the image container once the image has loaded. In the most common use case, the host element is the container.

<div class="img-container img-container-16-9" esl-image-container>
  <img alt src="image.jpg"/>
</div>

or

<picture class="img-container img-container-16-9" esl-image-container>
  <source srcset="image.webp" type="image/webp">
  <img alt src="image.jpg"/>
</picture>

Apply the custom class loaded to the image container element when the image loads, and the class error if an image loading error occurs.

<div class="img-container" esl-image-container="{readyCls: 'loaded', errorCls: 'error'}">
  <img alt src="image.jpg"/>
</div>

Apply the custom class loaded directly to the image element when it loads.

<img alt src="image.jpg" esl-image-container="{readyCls: 'loaded'}"/>

Apply the custom class loaded to the direct parent of the image element when the image loads successfully. Note: The error class query is executed after the ready class query, so the error class could override the ready class.

<div class="img-container" esl-image-container="{readyCls: 'loaded', errorCls: '!loaded'}">
  <img alt src="image.jpg"/>
</div>

or simply

<div class="img-container" esl-image-container="{readyCls: '', errorCls: '!loaded'}">
  <img alt src="image.jpg"/>
</div>

ESL Image Container (CSS Only)

A set of common CSS classes to use with native images. Seamless integration with ESLImageContainerMixin defaults.

Main container & image classes

Source: esl-image-utils.container.less

Aspect Ratio Container Classes

Source: esl-image-utils.ratios.less

Image Fade-In Animation

Source: esl-image-utils.fade.less

Usage

<picture class="img-container img-container-16-9" esl-image-container>
  <source srcset="image.webp" type="image/webp">
  <img alt src="image.jpg" class="img-fade img-cover" loading="lazy"/>
</picture>