Jekyll Picture Tag
repository·master·Indexed 20 days ago
https://github.com/rbuchberger/jekyll_picture_tagA Jekyll plugin for automating responsive images, handling resizing, cropping, and format conversion (including WebP and AVIF). It generates complex HTML markup such as <picture> and <img> tags to support art direction and resolution switching via a simple {% picture %} Liquid tag and YAML configuration.
What's inside jekyll_picture_tag
- Jekyll Picture Tag is a tool for automating responsive images in Jekyll sites. It handles the generation of cropped, resized, and reformatted image files, and automatically produces the corresponding HTML markup. It is designed to solve both art direction (using different images for different screen sizes) and resolution switching (serving different sizes of the same image) problems through YAML configuration and a simple template tag.
Understand the generated image filename format
masterWhen Jekyll Picture Tag processes images, it creates resized versions using a specific naming convention. This allows for predictable file identification within your assets. The format is:
(original name without extension)-(width)-(id-string).(filetype)Generate srcsets and images
masterWhen producing markup, you can use the provided srcsets located in../srcsets/. Using these will both generate the required images and provide the appropriate HTML attribute string (the content inside the quotes) for your markup.Choose a markup format for Jekyll Picture Tag
masterWhen using Jekyll Picture Tag, you can specify how the resulting HTML is structured using one of three markup format presets:
picture: Generates a full<picture>element. This includes a<source>tag for every requiredsrcsetand a fallback<img>tag. Use this when you need complex responsive logic (e.g., different media queries or MIME types).img: Generates a single<img>tag that includes asrcsetattribute. Use this for simpler responsive images where a single attribute is sufficient.auto: Automatically decides between an<img>tag and a<picture>tag. It will supply an<img>tag if you only have onesrcsetentry, otherwise it defaults to a<picture>tag.
Use presets to configure JPT output
masterA
presetis a collection of settings that determines the behavior of Jekyll Picture Tag (JPT), including the image formats used and the structure of the final HTML markup. You can specify a preset in the liquid tag to act as a blueprint for how JPT processes your images.If you omit the
presetargument, JPT will use thedefaultpreset. In its simplest form, thedefaultpreset generates an<img>tag with asrcsetattribute pointing to the newly generated images.Configure the `sizes` attribute for responsive images
masterThe
sizesattribute tells the browser how large an image will be on the screen at different breakpoints, allowing it to choose the correct source from thesrcsetbefore the page is fully rendered.In your
_data/picture.ymlpreset, you map your definedmedia_queriesto the expected display width.Important Notes:
- You cannot use percentages (e.g.,
50%) because the browser needs to know the size without calculating the parent container's width. - Use viewport units like
vworcalc()(e.g.,calc(100vw - 16px)). - If you do not provide
sizes, the browser assumes the image is100vw(full viewport width). - If you have a constant image size (like an icon), use the
sizekey (e.g.,size: 800px) instead ofsizesto generate a pixel-ratiosrcset.
# Example configuration in _data/picture.yml media_queries: mobile: 'max-width: 480px' tablet: 'max-width: 768px' presets: default: sizes: mobile: calc(100vw - 16px) tablet: 80vw size: 800px- You cannot use percentages (e.g.,
Understand deployment challenges with Jekyll Picture Tag
masterDeploying a site using Jekyll Picture Tag (JPT) can be difficult because the plugin depends on system libraries (
libvipsand its dependencies) to generate images. Build environments vary significantly; a build that works locally may fail in a cloud container if specific image formats (likeaviforjp2) require libraries that are not pre-installed.General Strategies:
- Local Build + Upload: Building the site on your local machine and then uploading the generated
_sitefolder to a hosting provider is the most reliable method, as it uses your local system's libraries. - Containerized Build: If using a service that pulls a git repository and builds it in a container (e.g., Netlify, AWS Amplify), you must verify that the container's environment supports your required image formats.
- Local Build + Upload: Building the site on your local machine and then uploading the generated
Limitations of dimension_attributes
masterWhile
dimension_attributeshelps prevent layout jank, it has the following technical limitations:- Art Direction: It does not work when using art direction (providing multiple different source images for different screen sizes). This is because the attributes can only be applied to the single
<img>tag, and art direction relies on multiple<source>elements. - Compatibility: It works for standard
<img>tags and<picture>tags that offer multiple widths/formats, but only if they share the same aspect ratio logic applied to the single underlying<img>tag.
- Art Direction: It does not work when using art direction (providing multiple different source images for different screen sizes). This is because the attributes can only be applied to the single
Configure srcset types and widths
masterDepending on your image use case, you must configure widths differently:
Width-based srcset (Responsive)
Used for images that change size based on screen width.
- Setting:
widths: - Requirement: You must provide a
sizesattribute in your Liquid tag.
Pixel-ratio srcset (Fixed size)
Used for images that stay the same size (e.g., avatars, icons) but need high-density support.
- Settings:
base_width:andpixel_ratios:
- Setting:
Use smartcrop via the `keep` setting
masterVersion 2.x replaces the old
gravitysetting with a Libvips-powered smartcrop feature calledkeep. This feature attempts to preserve the most interesting parts of an image.Replace your old
gravityvalues (likenorthorsoutheast) with one of the followingkeepoptions:attention(the default)entropycentreorcenter
Note: Any crop geometry settings that are not in the
w:hformat should be removed.Use defined media queries in Jekyll Picture Tag
masterOnce defined in
_data/picture.yml, you can reference the custom media query names in several parts of the plugin:- Specifying alternate source images within your Liquid tag.
- Building the
sizesattribute within your presets. - Various configuration settings.
Create Subpresets using YAML merge keys
masterYou can base a preset entirely on another by using the YAML merge key
<<. This allows you to define abasepreset and then create specialized presets that inherit all its properties while adding or overriding specific values.Important: Shallow Merging The merge operation is a shallow merge, not a deep merge. If you define a key in a subpreset that already exists in the base preset (such as
attributes), the entire value of that key from the base preset will be overwritten by the new value. Nested properties within that key will not be merged; they will be replaced.base: &base formats: [webp, original] format_quality: webp: 90 attributes: img: 'loading="lazy"' default: <<: *base widths: [500, 600, 700, 800, 900, 1000, 1200, 1600] link_source: true project_showcase: <<: *base widths: [700, 864, 900, 1296, 1600, 1728]