How do you implement custom fonts in a Magento theme?

How do you implement custom fonts in a Magento theme?

To implement custom fonts in a Magento theme, you can follow these steps:

Steps to Add Custom Fonts

  1. Upload Font Files: Place your custom font files (e.g., .woff, .woff2, .ttf) in the theme directory, typically in:

Example

app/design/frontend/Vendor/theme/web/fonts/

Add CSS for the Fonts: Create or modify your CSS file to include the font-face declaration. You can do this in your theme’s CSS file:

Example

@font-face {
    font-family: 'CustomFont';
    src: url('../fonts/CustomFont.woff2') format('woff2'),
         url('../fonts/CustomFont.woff') format('woff');
    font-weight: normal;
    font-style: normal;
}

body {
    font-family: 'CustomFont', sans-serif;
}

Include the CSS File: Ensure your CSS file is included in the layout XML file (e.g., default_head_blocks.xml):

Example

<link rel="stylesheet" src="css/custom.css" />

Deploy Static Content: After making changes, deploy the static content:

Example

php bin/magento setup:static-content:deploy

Example CSS

Example

/* In app/design/frontend/Vendor/theme/web/css/custom.css */
@font-face {
    font-family: 'CustomFont';
    src: url('../fonts/CustomFont.woff2') format('woff2'),
         url('../fonts/CustomFont.woff') format('woff');
    font-weight: normal;
    font-style: normal;
}

body {
    font-family: 'CustomFont', sans-serif;
}

With these steps, your custom fonts will be applied in your Magento theme.

Related Questions & Topics