Font filters are called on font objects. Use them to load fonts or to get font variants.
font_modify
font_modify takes two arguments: the property to change and the change to make.
You can reach every variant of the selected font family through font.variants, but font_modify makes it easier to get specific styles and weights.
{% assign bold_italic = settings.body_font | font_modify: 'weight', 'bold' | font_modify: 'style', 'italic' %}
You can use these properties and modifications:
| Property | Modification | Behavior |
|---|---|---|
style |
normal |
Returns the normal variant of the same weight (if it exists). |
style |
italic |
Returns the italic variant of the same weight (if it exists). |
style |
oblique |
Same behavior as italic. |
weight |
100 → 900 |
Returns a variant of the same style with the given weight (if it exists). |
weight |
normal |
Same behavior as 400. |
weight |
bold |
Same behavior as 700. |
weight |
+100 → +900 |
Returns an incrementally bolder variant of the same style (if it exists). For example, if font has a weight of 400, `font \ |
weight |
-100 → -900 |
Returns an incrementally lighter variant of the same style (if it exists). For example, if font has a weight of 400, `font \ |
weight |
lighter |
Returns a lighter variant of the same style, following the rules of the CSS font-weight property and browser fallback weights (if it exists). |
weight |
bolder |
Returns a bolder variant of the same style, following the rules of the CSS font-weight property and browser fallback weights (if it exists). |
Handling font variants
If font_modify tries to create a font variant that does not exist, it returns nil. This example shows the output when no bolder variant of settings.body_font exists:
{% assign bolder_font = settings.body_font | font_modify: 'weight', 'bolder' %}
h2 {
font-weight: {{ bolder_font.weight }};
}
h2 {
font-weight: ;
}
To prevent this, check the value of bolder_font with a control flow tag or set a fallback with the default filter:
{% assign bolder_font = settings.body_font | font_modify: 'weight', 'bolder' %}
{% if bolder_font %}
h2 {
font-weight: {{ bolder_font.weight }};
}
{% endif %}
font_face
Returns a CSS @font-face declaration that loads the selected font.
<style>
{{ settings.heading_font | font_face }}
</style>
<style>
@font-face {
font-family: "Neue Haas Unica";
font-weight: 400;
font-style: normal;
src: url("https://fonts.versacommerce.io/s/neuehaasunica/v1/neuehaasunica_n4.woff2") format("woff2"),
url("https://fonts.versacommerce.io/s/neuehaasunica/v1/neuehaasunica_n4.woff") format("woff");
}
</style>
Additional properties
font_face takes an optional font_display argument that sets the font-display property of the @font-face declaration.
<style>
{{ settings.heading_font | font_face: font_display: 'swap' }}
</style>
<style>
@font-face {
font-family: "Neue Haas Unica";
font-weight: 400;
font-style: normal;
font-display: swap;
src: url("https://fonts.versacommerce.io/s/neuehaasunica/v1/neuehaasunica_n4.woff2") format("woff2"),
url("https://fonts.versacommerce.io/s/neuehaasunica/v1/neuehaasunica_n4.woff") format("woff");
}
</style>
font_url
Returns a CDN URL for the selected font.
{{ settings.heading_font | font_url }}
https://fonts.versacommerce.io/s/neuehaasunica/v1/neuehaasunica_n4.woff2
By default font_url returns the woff2 version. Pass a parameter to choose the format. woff and woff2 are supported.
{{ settings.heading_font | font_url: 'woff' }}