The Responsive Image module provides a field formatter for image fields that use the HTML5 picture tag and source sets. Utilizing the Breakpoint module, mappings to breakpoints are made to denote an image style to be used at each breakpoint.
The responsive image field formatter works with using a defined responsive image style. Responsive image styles are configurations that map image formats to specific breakpoints and modifiers. First, you need to define a responsive image style, and then you can apply it to an image field.
In this recipe, we will create a responsive image style set called Article image
and apply it to the Article
content type's image field.
You will need to enable the Responsive Image
module as it is not automatically enabled with the standard installation.
label
that will be used to administratively identify the Responsive image style set.fieldset
. Expand the fieldset
and select a single image style, and then, pick an appropriate image style:The Responsive image style provides three components: a responsive image element, the responsive image style configuration entity, and the responsive image field formatter. The configuration entity is consumed by the field formatter and displayed through the responsive image element.
The responsive image style entity contains an array of breakpoints to image style mappings. The available breakpoints are defined by the selected breakpoint groups. Breakpoint groups can be changed anytime; however, the previous mappings will be lost.
The responsive image element prints a picture
element with each breakpoint defining a new source
element. The breakpoint's media query value is provided as the media
attribute for the element.
A benefit of using the responsive image formatter is performance. Browsers will only download the resources defined in the srcset
of the appropriate source
tag. This not only allows you to a deliver a more appropriate image size but also a smaller payload on smaller devices.
The Responsive Image module attaches the picturefill
library to the responsive image element definition. The element's template also provides HTML to implement the polyfill
. The polyfill
can be removed by overriding the element's template and overriding the picturefill
library to be disabled.
The following snippet, when added to a theme's info.yml
, will disable the picturefill
library:
libraries-override: core/picturefill: false
Then, the responsive-image.html.twig
must be overridden by the theme to remove the extra HTML generated in the template for the polyfill
:
picturefill
for IE9 at http://scottjehl.github.io/picturefill/#ie9