MJML head component for importing custom fonts into email templates
npx @tessl/cli install tessl/npm-mjml-head-font@4.15.0MJML Head Font provides the mj-font component for importing custom fonts into MJML email templates. This head component allows developers to register fonts that can be used throughout their email layouts, ensuring consistent typography across email clients.
npm install mjml-head-fontThis component is typically used within the MJML framework. When developing custom MJML components or extending MJML:
import MjFont from "mjml-head-font";For CommonJS:
const MjFont = require("mjml-head-font");The component is automatically registered when MJML preset-core is loaded. In MJML templates, you use the component directly without imports:
<mj-font name="FontName" href="https://example.com/font.css" /><mjml>
<mj-head>
<mj-font name="Raleway" href="https://fonts.googleapis.com/css?family=Raleway" />
<mj-font name="Open Sans" href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,700" />
</mj-head>
<mj-body>
<mj-section>
<mj-column>
<mj-text font-family="Raleway, Arial">
Custom font text
</mj-text>
</mj-column>
</mj-section>
</mj-body>
</mjml>The main component class that extends MJML's HeadComponent to provide font registration functionality.
/**
* MJML head component for importing custom fonts
* Extends HeadComponent from mjml-core
*/
export default class MjFont extends HeadComponent {
static componentName;
static allowedAttributes;
handler();
}/**
* The MJML tag name for this component
*/
static componentName = "mj-font";
/**
* Defines allowed attributes and their types
*/
static allowedAttributes = {
name: "string", // Font family name
href: "string" // URL to hosted CSS file with @font-face declarations
};/**
* Processes the component by registering the font with MJML context
* Called during MJML head processing phase
*/
handler();The handler method implementation:
handler() {
const { add } = this.context;
add('fonts', this.getAttribute('name'), this.getAttribute('href'));
}The handler method:
add function from this.contextname attribute using this.getAttribute('name')href attribute using this.getAttribute('href')add('fonts', name, href)/**
* Default export of the MjFont component class
*/
export default class MjFont extends HeadComponent;The MjFont component inherits the following methods from HeadComponent/Component:
/**
* Get the value of a component attribute
* @param {string} name - The attribute name
* @returns {string} The attribute value
*/
getAttribute(name);
/**
* Get the child context for nested components
* @returns {object} The component context
*/
getChildContext();
/**
* Get the text content from the component
* @returns {string} The trimmed content
*/
getContent();The mj-font component accepts the following attributes:
@font-face declarations for the fontExample CSS file content:
@font-face {
font-family: 'Raleway';
font-style: normal;
font-weight: 400;
src: url(https://fonts.gstatic.com/s/raleway/v28/1Ptxg8zYS_SKggPN4iEgvnHyvveLxVvaoorCP.woff2) format('woff2');
}This component is part of the MJML preset-core package and is automatically registered when MJML is initialized. For custom MJML setups, you can register it manually:
import { registerComponent } from 'mjml-core';
import MjFont from 'mjml-head-font';
registerComponent(MjFont);This component integrates with the MJML framework through:
mj-head processing phase via handlerChildren()context.add()<link> tags in the email HTML headfont-family attributes throughout the MJML template<mj-font> in <mj-head>handler() method is called during head processing<head> sectionThe component relies on MJML's built-in validation system. Invalid attributes or missing required attributes will be caught by the MJML validator based on the allowedAttributes definition.
Font support depends on the email client's CSS capabilities:
Always provide fallback fonts in your font-family declarations.