Imgix.js
Responsive images in the browser, simplified
Install / Use
/learn @imgix/Imgix.jsREADME
imgix.js is a dependency-free JavaScript library for the browser that allows for easy integration of imgix into websites.
<!-- /ix-docs-ignore -->
Overview / Resources
imgix.js allows developers to easily generate responsive images using the srcset and sizes attributes, or the picture element. This lets you write a single image URL that is parsed and used to make images look great at any screen size, by using imgix to process and resize your images on the fly.
Note: imgix.js is designed to run in the browser, manipulating existing <img> elements on an HTML page. If you're looking for a JavaScript library that can programmatically generate imgix URLs, consider using imgix-core-js instead.
Before getting started with imgix.js, it is highly recommended that you read Eric Portis' seminal article on srcset and sizes. This article explains the history of responsive images in responsive design, why they're necessary, and how all these technologies work together to save bandwidth and provide a better experience for users. The primary goal of imgix.js is to make these tools easier for developers to implement, so having an understanding of how they work will significantly improve your imgix.js experience.
Below are some other articles that help explain responsive imagery, and how it can work alongside imgix:
- Using imgix with
<picture>. Discusses the differences between art direction and resolution switching, and provides examples of how to accomplish art direction with imgix. - Responsive Images with
srcsetand imgix. A look into how imgix can work withsrcsetandsizesto serve the right image.
Installation
There are several ways to install imgix.js. The appropriate method depends on your project.
- npm:
npm install --save imgix.js - Bower:
bower install --save imgix.js - Manual: Download the latest release of imgix.js, and use
dist/imgix.jsordist/imgix.min.js.
If your build process will re-run dist/imgix.js or dist/imgix.min.js through Browserify, you'll need to add noParse: [require.resolve('imgix.js')] to your Browserify config. If you skip this, Browserify will attempt to re-require imgix.js' dependencies, which have already been inlined.
Once imgix.js has been included on the page, it will automatically run once, after the DOMContentLoaded event fires. This will detect and process all img, picture, and source tags on the page that are set up to use imgix.js as described in the Usage section of this README.
Configuration
imgix.js has two important global options:
host: A string corresponding to the desired imgix hostname (defaults tonull). This enables the use ofix-pathandix-paramsto define images, instead of having to manually provide URLs out inix-src. See theix-pathandix-paramssection below for details.useHttps: A boolean (defaults totrue), specifying whether to generatehttporhttps-prefixed URLs.
These configuration options (as well as other options described in the "Advanced Usage" section) can be defined in two ways. The easiest way is to specify them with meta tags in your document's <head>:
<head>
<meta property="ix:host" content="assets.imgix.net">
<meta property="ix:useHttps" content="true">
</head>
The other way is to manually set these options on the imgix.config object. Note that these options should be set after loading imgix.js, but before the DOMContentLoaded event is fired on the page:
<script src="imgix.js"></script>
<script>
imgix.config.host = 'assets.imgix.net';
imgix.config.useHttps = false;
</script>
Usage
After installation and set up are complete, one can begin adding responsive images to the page through one of few ways:
ix-src
Creates an img tag with the ix-src attribute:
<img
ix-src="https://assets.imgix.net/unsplash/hotairballoon.jpg?w=300&h=500&fit=crop&crop=right"
alt="A hot air balloon on a sunny day"
sizes="100vw"
>
Please note: 100vw is an appropriate sizes value for a full-bleed image. If your image is not full-bleed, you should use a different value for sizes. Eric Portis' "Srcset and sizes" article goes into depth on how to use the sizes attribute.
This will generate HTML something like the following:
<img
ix-src="https://assets.imgix.net/unsplash/hotairballoon.jpg?w=300&h=500&fit=crop&crop=right"
alt="A hot air balloon on a sunny day"
sizes="100vw"
srcset="
https://assets.imgix.net/unsplash/hotairballoon.jpg?w=100&h=167&fit=crop&crop=right 100w,
https://assets.imgix.net/unsplash/hotairballoon.jpg?w=200&h=333&fit=crop&crop=right 200w,
…
https://assets.imgix.net/unsplash/hotairballoon.jpg?w=2560&h=4267&fit=crop&crop=right 2560w
"
src="https://assets.imgix.net/unsplash/hotairballoon.jpg?w=300&h=500&fit=crop&crop=right"
ix-initialized="ix-initialized"
>
Since imgix can generate as many derivative resolutions as needed, imgix.js calculates them programmatically, using the dimensions you specify (note that the w and h params scale appropriately to maintain the correct aspect ratio). All of this information has been placed into the srcset and sizes attributes. Because of this, imgix.js no longer needs to watch or change the img tag, as all responsiveness will be handled automatically by the browser as the page is resized.
ix-path and ix-params
If configured with a global host option, imgix.js can use the ix-path and ix-params attributes instead of ix-src. The ix-path attribute is used to specify the path to an image, and the ix-params attribute is used to define the imgix URL API parameters to be applied to the image. Using these two attributes instead of ix-src has several advantages:
ix-paramsautomatically URL/Base64-encodes specified parameters, as appropriate.ix-paramsis a JSON string, which is easier to read than a URL and can be generated by other tools if necessary.- Not having to re-type
https://my-source.imgix.nethelps keep code DRY.
Here's how the previous example would be written out using ix-path and ix-params instead of ix-src. Regardless of the method chosen, the end result in-browser will be the same.
<img
ix-path="unsplash/hotairballoon.jpg"
ix-params='{
"w": 300,
"h": 500,
"fit": "crop",
"crop": "right"
}'
alt="A hot air balloon on a sunny day"
>
Please note: ix-params must be a valid JSON string. This means that keys and string values must be surrounded by double quotes, e.g., "fit": "crop".
ix-sizes attribute
When set to auto, automatically updates an img tag's sizes attribute to match the image's display size.
<img
ix-src="https://assets.imgix.net/unsplash/hotairballoon.jpg?w=300&h=500&fit=crop&crop=right"
alt="A hot air balloon on a sunny day"
ix-sizes="auto"
>
Please note: the image width has to be calculable before the image has loaded, otherwise
sizeswill not match the width of the displayed image. In most cases, using the CSS ruleimg[ix-sizes="auto"] { display: block; width: 100%; }will ensure the image'swidthis calculable before it has loaded.
Generates HTML similar to the following
``
