SVG Icons
The SVG icons feature allows you to add SVG icon assets to your project and embed them anywhere in your theme Liquid.
Icons should be placed in assets/icons/, for example assets/icons/cart.svg:
assets/icons/cart.svghtml<svg>...</svg>
assets/icons/cart.svghtml<svg>...</svg>
SVG icons are placed in a subdirectory of the assets/ folder at the root of your project - not inside of static/assets/!
The {! icon !} macro
To include an icon in Liquid, use the {! icon !} macro with the icon name:
static/snippets/example.liquidhtml<span>Cart{! icon 'cart' !}</span>
static/snippets/example.liquidhtml<span>Cart{! icon 'cart' !}</span>
dist/snippets/example.liquidhtml<span>Cart<svg>...</svg></span>
dist/snippets/example.liquidhtml<span>Cart<svg>...</svg></span>
You can use the {! icon !} macro in any static Liquid file (static/**/*.liquid), as well as in component templates.
Working with currentColor
Any fill or stroke colors in your SVG icons are automatically converted to currentColor. This means your icons will inherit the text color of their container (i.e. color in CSS.)
This allows you to reuse icons without needing extra versions for each possible color:
- Liquid
- Compiled
Using SVG icons with currentColorhtmlThis is a black icon:<span style="color: black">{! icon 'circle' !}</span><br>This is a red icon:<span style="color: red">{! icon 'circle' !}</span>
Using SVG icons with currentColorhtmlThis is a black icon:<span style="color: black">{! icon 'circle' !}</span><br>This is a red icon:<span style="color: red">{! icon 'circle' !}</span>
Using SVG icons with currentColorhtmlThis is a black icon:<span style="color: black"><svg viewBox="0 0 100 100"><circle fill="currentColor" cx="50" cy="50" r="50"></svg></span><br>This is a red icon:<span style="color: red"><svg viewBox="0 0 100 100"><circle fill="currentColor" cx="50" cy="50" r="50"></svg></span>
Using SVG icons with currentColorhtmlThis is a black icon:<span style="color: black"><svg viewBox="0 0 100 100"><circle fill="currentColor" cx="50" cy="50" r="50"></svg></span><br>This is a red icon:<span style="color: red"><svg viewBox="0 0 100 100"><circle fill="currentColor" cx="50" cy="50" r="50"></svg></span>
If you're not using monotone icons or you want more control over which elements of your icons use currentColor, you can disable this feature by setting optimizations.icons.convertToCurrentColor to false:
- Liquid
- Compiled
Using SVG icons with hard-coded colorshtmlThis is a black icon:<span>{! icon 'circle-black' !}</span><br>This is a red icon:<span>{! icon 'circle-red' !}</span>
Using SVG icons with hard-coded colorshtmlThis is a black icon:<span>{! icon 'circle-black' !}</span><br>This is a red icon:<span>{! icon 'circle-red' !}</span>
Using SVG icons with hard-coded colorshtmlThis is a black icon:<span><svg viewBox="0 0 100 100"><circle fill="#000000" cx="50" cy="50" r="50"></svg></span><br>This is a red icon:<span><svg viewBox="0 0 100 100"><circle fill="#ff0000" cx="50" cy="50" r="50"></svg></span>
Using SVG icons with hard-coded colorshtmlThis is a black icon:<span><svg viewBox="0 0 100 100"><circle fill="#000000" cx="50" cy="50" r="50"></svg></span><br>This is a red icon:<span><svg viewBox="0 0 100 100"><circle fill="#ff0000" cx="50" cy="50" r="50"></svg></span>
Including class names and extra attributes
You can specify a classname or additional attributes to inject on to the <svg> element by passing them as arguments to the {! icon !} macro:
- Liquid
- Compiled
Providing a classnamehtml{! icon 'circle', class: 'icon icon-large' !}
Providing a classnamehtml{! icon 'circle', class: 'icon icon-large' !}
Providing extra attributeshtml{! icon 'circle', role: 'img', aria-label: 'This is the circle icon' !}
Providing extra attributeshtml{! icon 'circle', role: 'img', aria-label: 'This is the circle icon' !}
Providing a classnamehtml<svg viewBox="0 0 100 100" class="icon icon-large"><circle fill="currentColor" cx="50" cy="50" r="50"></svg>
Providing a classnamehtml<svg viewBox="0 0 100 100" class="icon icon-large"><circle fill="currentColor" cx="50" cy="50" r="50"></svg>
Providing extra attributeshtml<svg viewBox="0 0 100 100" role="img" aria-label="This is the circle icon"><circle fill="currentColor" cx="50" cy="50" r="50"></svg>
Providing extra attributeshtml<svg viewBox="0 0 100 100" role="img" aria-label="This is the circle icon"><circle fill="currentColor" cx="50" cy="50" r="50"></svg>
Classnames will be appended to any existing classnames in the original SVG. Other attributes will just be added to the end of the element.
You cannot use a Liquid variable as an argument to the {! icon !} macro. Macros are resolved at compile-time, so only static strings are supported.