Add support for CSS's image-set()
syntax
#9931
-
Craft already supports Can we please also have support for the CSS Reference: https://css-tricks.com/using-performant-next-gen-images-in-css-with-image-set/ |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Proper That said, I’ve just added a new {% macro bgCss(urls) %}
/* Fallback */
background-image: url('{{ urls|first }}');
/* Chrome/Edge/Opera/Samsung, Safari will fallback to this as well */
background-image: -webkit-image-set({{ urls|map((url, size) => "url('#{url}') #{size}")|join(', ') }});
/* Standard use */
background-image: image-set({{ urls|map((url, size) => "'#{url}' #{size}")|join(', ') }});
{% endmacro %}
{% set asset = ... %}
{% set urls = asset.getUrlsBySize(['1x', '2x'], {width: 500}) %}
<div style="{{ _self.bgCss(urls) }}">
...
</div> |
Beta Was this translation helpful? Give feedback.
Proper
image-set
support is still basically nonexistent without using a-webkit-
prefix, so the only way we could do this right now that would be of any help would be to add agetBgCss()
method, which returns multiplebackground-image
style definitions, per the example in that CSS Tricks article. That feels a little too gross to me to be part of the core API, though. Maybe better suited for a plugin, implemented via a behavior.That said, I’ve just added a new
getUrlsBySize()
method (d6930cc), which handles all the grunt work that used to live withingetSrcset()
. It returns an array of image transform URLs, indexed by size. You can use that along with your ownbgCss()
macro: