Custom Fonts
This section will show you how to add custom fonts. Step one has been done but left it here for informational purposes.
Step One: Add Fonts
- Create a new
fonts/directory insidesrc/ - Drop your
.woff2fonts intosrc/fonts/(.woff2has support in all browsers now! It's the only font type you'll need)
Step Two: Configure Webpack
You will need to tell Webpack to copy the fonts over to the build/ directory.
- Add the following to the
new CopyPluginobject:
// webpack.config.js
{
from: '*.woff2',
to: 'fonts/[path][name][ext]',
context: path.resolve( process.cwd(), 'src/fonts' ),
},
The new CopyPlugin object should look similar to:
// webpack.config.js
new CopyPlugin( {
patterns: [
{
from: '**/*.{jpg,jpeg,png,gif,svg}',
to: 'images/[path][name].[ext]',
context: path.resolve( process.cwd(), 'src/images' ),
},
{
from: '*.woff2',
to: 'fonts/[path][name].[ext]',
context: path.resolve( process.cwd(), 'src/fonts' ),
},
],
} ),
- Build. In your terminal, type
npm run buildand press enter. This should copy all of the fonts over tobuild/fonts/, making them available in the theme.
Step 3: Create _fonts.scss
- Inside
src/scss/global/create a new file and name it_fonts.scss - Add your
@font-facedeclarations:
There is a scss mixin in place to generate the markup below, if wanted. Otherwise add @font-face however you'd like.
/* src/scss/base/_fonts.css */
@font-face {
font-family: 'Droid Serif';
src: local( 'Droid Serif' ), local( 'DroidSerif' ),
url( '../fonts/DroidSerif.woff2' ) format( 'woff2' );
font-weight: 400;
font-style: normal;
font-display: swap;
}
@font-face {
font-family: Cabin;
src: local( 'Cabin Regular' ), local( 'Cabin-Regular' ),
url( '../fonts/Cabin-Regular.woff2' ) format( 'woff2' );
font-weight: 400;
font-style: normal;
font-display: swap;
}
- Import
_fonts.scssinscss/base/index.scss
/* scss/base/index.scss */
@import 'globals';
@import 'fonts';