# Font filters

Font filters are called on font objects.

Font filters are called on [`font`](https://developer.versacommerce.de/articles/liquid-objects-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`](https://developer.versacommerce.de/articles/liquid-objects-font), but `font_modify` makes it easier to get specific styles and weights.

```liquid
{% 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 \| font_modify: "weight", "+100"` returns the variant with weight `500` of the same style. |
| `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 \| font_modify: "weight", "-100"` returns the variant with weight `300` of the same style. |
| `weight` | `lighter` | Returns a lighter variant of the same style, following the rules of the [CSS font-weight property](https://developer.mozilla.org/en-US/docs/Web/CSS/font-weight#Meaning_of_relative_weights) and browser [fallback weights](https://developer.mozilla.org/en-US/docs/Web/CSS/font-weight#Fallback_weights) (if it exists). |
| `weight` | `bolder` | Returns a bolder variant of the same style, following the rules of the [CSS font-weight property](https://developer.mozilla.org/en-US/docs/Web/CSS/font-weight#Meaning_of_relative_weights) and browser [fallback weights](https://developer.mozilla.org/en-US/docs/Web/CSS/font-weight#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:

```liquid
{% assign bolder_font = settings.body_font | font_modify: 'weight', 'bolder' %}
h2 {
  font-weight: {{ bolder_font.weight }};
}
```

```css
h2 {
  font-weight: ;
}
```

To prevent this, check the value of `bolder_font` with a [control flow tag](https://developer.versacommerce.de/articles/liquid-tags-control-flow-tags#if) or set a fallback with the [default filter](https://developer.versacommerce.de/articles/liquid-filters-additional-filters#default):

```liquid
{% 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.

```liquid
<style>
  {{ settings.heading_font | font_face }}
</style>
```

```html
<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.

```liquid
<style>
  {{ settings.heading_font | font_face: font_display: 'swap' }}
</style>
```

```html
<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.

```liquid
{{ settings.heading_font | font_url }}
```

```text
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.

```liquid
{{ settings.heading_font | font_url: 'woff' }}
```
