Start building

Tips

The Shopify image_url filter: responsive images that load fast

Copy the two patterns below, then read why each parameter is there. Most slow Shopify images come from a sizes value nobody set or a hero that's lazy-loaded by accident.

The Klorr teamUpdated 8 min read

  • Images
  • Liquid
  • Performance

Key takeaways

  • image_url needs a width or height, and that number caps your srcset. An image is never upscaled past its original size.
  • sizes tells the browser how wide the image displays. Without an accurate value, a grid thumbnail can download a screen-sized file.
  • image_tag already lazy-loads from the fourth section on, so leave loading alone unless you have a specific reason.
  • section.index is nil in the editor and static sections, so test the lazy case as section.index > 3, never <= 3.
  • img_url has been replaced. '600x' becomes width: 600.
section.liquid
{%- comment -%} A full-width hero, likely the largest thing on screen. {%- endcomment -%}
{% unless section.index > 1 %}
  {{ section.settings.image
    | image_url: width: 2400
    | image_tag:
      loading: 'eager',
      fetchpriority: 'high',
      widths: '800, 1200, 1600, 2000, 2400',
      sizes: '100vw',
      class: 'hero__image'
  }}
{% else %}
  {{ section.settings.image
    | image_url: width: 2400
    | image_tag:
      loading: 'lazy',
      widths: '800, 1200, 1600, 2000, 2400',
      sizes: '100vw',
      class: 'hero__image'
  }}
{% endunless %}
section.liquid
{%- comment -%} 4 columns on desktop, 2 on phones, inside a page no wider than 1200px. {%- endcomment -%}
{{ product.featured_image
  | image_url: width: 800
  | image_tag:
    widths: '200, 300, 400, 600, 800',
    sizes: '(min-width: 1200px) 280px, (min-width: 750px) 25vw, 50vw',
    class: 'card__image'
}}

The grid image has no loading parameter on purpose. Everything else on this page explains a decision in those two blocks.

What image_url accepts

`image_url` returns a CDN URL. It works on article, collection, image, line_item, product, variant and country objects, so {{ product | image_url: width: 600 }} is valid and uses that product's main image. You must give a width or a height, or you get an error instead of a URL. When you only give one, the other follows from the image's proportions.

ParameterValuesWorth knowing
width, heightUp to 5760An image can never be resized larger than its original.
croptop, center, bottom, left, right, regionDefaults to center. Only has an effect when the requested shape differs from the image's, so give both dimensions.
formatpjpg, jpgYou rarely want this. Left out, Shopify picks a format the browser supports.
quality10 to 90Left out, Shopify picks a value automatically. Shopify notes that leaving it out isn't the same as asking for 100.
pad_colorA hex colorFills the empty space when padding changes the shape, useful for logos on a fixed tile.

A square thumbnail from any photo is {{ image | image_url: width: 600, height: 600, crop: 'center' }}. If the subject sits in a corner, crop: 'center' may cut it, so set a focal point on the image in the admin and let CSS crop instead, as covered below.

Why widths and sizes matter so much

`image_tag` builds the <img>: src, width, height, alt text and a srcset. Left alone, it generates a set of widths up to the size in your URL. So the number you hand to image_url is your ceiling, and width: 400 means no large candidate exists at all, however big the screen. Set it to the largest size the image will ever display at, times two if you care about sharp images on high-density screens.

The browser picks from srcset before CSS has been applied, so it can't know your layout. sizes is how you tell it. Without an accurate value it assumes something close to the full viewport, and a phone with a two-column grid can fetch a file twice as wide as it needs. Here's how the grid's sizes above was worked out:

  • Phones show two columns, so each image is about half the screen: 50vw.
  • From 750px the grid has four columns: 25vw.
  • From 1200px the page stops growing, so a column is a fixed width, about 280px with gaps: 280px.

Order matters: the browser takes the first media condition that matches, so the largest breakpoint goes first and the last value has no condition. You don't need to be exact. Rounding gaps and padding is fine, but a wrong column count isn't. To check it, open your browser's dev tools, click the image in the Elements panel and compare its rendered size with the file it downloaded. If a 280px image pulled an 800px file on a normal-density screen, your sizes is off.

Pass widths when you know your layout and want fewer, more useful candidates. The default set is sensible, so this is tuning and not a requirement.

Lazy loading, the hero and the nil trap

According to Shopify's performance docs, image_tag applies loading="eager" for sections 1 to 3 and loading="lazy" for section 4 onward, and it stays eager whenever the index is unavailable. So the default is usually correct, and hard-coding loading: 'lazy' on every image in a section is how heroes end up slow. The trouble starts with a hero that's lazy because someone added it to be safe.

When you do override the default, watch out for section.index. It's nil in three places: while rendering in the online store editor, in static sections and through the Section Rendering API. Comparisons against nil are false in Liquid, which turns the obvious condition into a bug:

section.liquid
{%- comment -%} Wrong: a nil index falls through to lazy, including every static hero. {%- endcomment -%}
{% if section.index <= 2 %} eager {% else %} lazy {% endif %}

{%- comment -%} Right: test the lazy case, so nil lands on eager. {%- endcomment -%}
{% if section.index > 2 %} lazy {% else %} eager {% endif %}

fetchpriority: 'high' tells the browser this image comes first. Use it on the one image likely to be your largest element, and not across a whole slideshow, or nothing is prioritized. In a product grid, make the first few cards eager through forloop.index and let the rest be lazy, since the top row is often on screen at load.

preload: true is stronger: it sends a Link header so the browser starts fetching before reading the HTML. Shopify says to use it sparingly, and preloading an image that isn't near the top wastes bandwidth on every page view. Don't use it in a section that might not be first.

Alt text, layout shift and focal points

image_tag fills alt from the media's alt text, or from the resource title for product, variant, collection, article and line item images. That's a sensible start, but it means an image without alt text gets the product title, which repeats the heading next to it on many cards. Fill in alt text in the admin where it adds something. For a purely decorative image, pass alt: '' so screen readers skip it.

The width and height attributes let the browser reserve space, which prevents layout shift. They only work if CSS doesn't fight them, so a responsive image needs height: auto alongside max-width: 100%. Otherwise the height attribute holds while the width shrinks, and your image looks squashed. Fixed-ratio images should use aspect-ratio with object-fit: cover.

If a merchant sets a focal point on an image, image_tag carries it through, so CSS-cropped images keep the subject in frame when you use object-fit: cover. That's a good reason to crop with CSS over crop: 'center' when the same picture has to fit several shapes.

Different photos on phone and desktop

image_tag makes one <img>, so a separate portrait crop for phones means writing a <picture> yourself. The <img> inside is still image_tag:

section.liquid
<picture>
  {%- if section.settings.image_mobile != blank -%}
    <source
      media="(max-width: 749px)"
      srcset="{{ section.settings.image_mobile | image_url: width: 750 }} 750w,
              {{ section.settings.image_mobile | image_url: width: 1100 }} 1100w"
      sizes="100vw"
    >
  {%- endif -%}
  {{ section.settings.image | image_url: width: 2400 | image_tag: sizes: '100vw', fetchpriority: 'high' }}
</picture>

Avoid hiding one of two images with display: none. Don't use a CSS background-image for a hero either: Shopify's guidance is that background images are found late, only after CSS has loaded, which delays your largest image. An absolutely positioned <img> with object-fit: cover looks identical and loads properly.

Moving off img_url

Shopify's docs state that `img_url` has been replaced by image_url, and img_tag by image_tag. Old code keeps rendering, but nothing new should use them, and you lose the srcset that image_tag gives you for nothing extra.

OldNew
image | img_url: '600x'image | image_url: width: 600
image | img_url: 'x400'image | image_url: height: 400
image | img_url: '600x600', crop: 'center'image | image_url: width: 600, height: 600, crop: 'center'
image | img_url: 'master'image | image_url: width: 2400, or whatever your largest display size is
image | img_url: '600x' | img_tagimage | image_url: width: 600 | image_tag
A hand-written srcset with img_url per widthimage_tag with a widths list

The old scale: 2 parameter has no direct equivalent and doesn't need one, since srcset handles high-density screens. Search your theme for img_url and img_tag and work through them, then remove any JavaScript lazy loader that swapped data-src: Shopify warns these hide the URL from the browser's preload scanner. Theme Check will flag old filter usage for you if you work from the CLI.

For a broader pass over a slow section, see how to speed up Shopify sections. If you generate a section with Klorr or buy one, check its sizes against the layout you ended up with, as you would with any code.

Questions, answered

What is the difference between image_url and img_url in Shopify?

img_url is the older filter, and Shopify's docs say it has been replaced by image_url. The new one takes explicit width and height parameters in place of size strings such as '600x', and it pairs with image_tag to produce srcset, sizes and dimensions.

Why does image_url give an error?

It requires a width or a height. Without either, it returns an error and not a URL. A blank image setting is another common cause of an empty result, so check that a setting isn't blank before you use it.

Does image_tag lazy load images automatically?

Yes. It applies eager loading for sections 1 to 3 and lazy loading from section 4, and stays eager when section.index is nil, for example in the theme editor.

What should I put in the sizes attribute?

How wide the image displays at each breakpoint, with the largest breakpoint first and a final value with no condition, such as '(min-width: 750px) 25vw, 50vw'. Rough numbers are fine as long as the column counts are right.

Can image_url make an image bigger than the original?

No. Shopify never resizes an image beyond its original dimensions, so a small upload stays soft however large a width you ask for.

Get the whole section written for you

Describe the section you want. Klorr writes the Liquid, the CSS, the schema and every theme-editor setting — code that lives in your theme and stays yours.