postcss-plugin-font-variation

PostCSS plugin adds font-variation property based on font-declarations

Usage no npm install needed!

<script type="module">
  import postcssPluginFontVariation from 'https://cdn.skypack.dev/postcss-plugin-font-variation';
</script>

README

PostCSS Font Variation Plugin

Test workflow status

PostCSS Font Variation Plugin adds font-variation-settings based on font-stretch, font-style, font-weight and font properties.

Source Transpiled
.foo {
    font: italic bold 16px/1.4 sans-serif;
}
.foo {
    font: italic bold 16px/1.4 sans-serif;
    --fws-ital: 1;
    --fws-wght: 700;
    font-variation-settings:
        'ital' var(--fws-ital),
        'wdth' var(--fws-wdth),
        'wght' var(--fws-wght);
}
:root {
    --fws-ital: 0;
    --fws-wdth: 100;
    --fws-wght: 400;
}

Plugin uses CSS variables, which allows combining settings from multiple matching rules. E.g.:

Source Transpiled

.foo {
    font: italic bold 16px/1.4 sans-serif;
}
.bar {
    font-weight: normal;
}
.foo {
    font: italic bold 16px/1.4 sans-serif;
    --fws-ital: 1;
    --fws-wght: 700;
    font-variation-settings:
        'ital' var(--fws-ital),
        'wdth' var(--fws-wdth),
        'wght' var(--fws-wght);
}
.bar {
    font-weight: normal;
    --fws-wght: 400;
    font-variation-settings:
        'ital' var(--fws-ital),
        'wdth' var(--fws-wdth),
        'wght' var(--fws-wght);
}
:root {
    --fws-ital: 0;
    --fws-wdth: 100;
    --fws-wght: 400;
}

This way element with class="foo bar" will have font-variation-settings set to 'ital' 1, 'wdth' 100, 'wght' 400, not just 'wght' 400.

Usage

Step 1: Install plugin:

npm install --save-dev postcss postcss-plugin-font-variation

Step 2: Check you project for existed PostCSS config: postcss.config.js in the project root, "postcss" section in package.json or postcss in bundle config.

If you do not use PostCSS, add it according to official docs and set this plugin in settings.

Step 3: Add the plugin to plugins list:

module.exports = {
  plugins: [
+   require('postcss-plugin-font-variation'),
    require('autoprefixer')
  ]
}