How To Load and Use Custom Fonts with CSS

CSS Custom Fonts allow web developers to integrate unique typography into their websites by specifying fonts that may not be available by default on users' devices. This is achieved using the @font-face rule, which allows developers to define custom fonts and their sources, such as self-hosted font files or third-party services like Google Fonts. Once the custom font is defined, it can be applied to specific elements in the CSS, enhancing the visual design and branding of the website. Fallback fonts should also be provided to ensure readability and consistency across different devices and browsers, ensuring a seamless user experience.

Understanding Web Fonts

CSS Custom Fonts enable the integration of unique typography on websites, unlike system fonts pre-installed on user devices. These fonts are downloaded and displayed on the page, offering a diverse range of options to enhance design and branding. Common formats like WOFF, WOFF2, TTF, and EOT support compatibility across various browsers and devices. Utilizing the @font-face rule, developers can specify custom fonts and their sources, whether self-hosted or from third-party services like Google Fonts. Fallback fonts ensure consistent readability, bolstering accessibility and user experience.

How To Load and Use Custom Fonts with CSS

Custom fonts in CSS allow web developers to use fonts that are not commonly available on users' devices. This means you can choose unique typography to enhance the design and branding of your website. Here's how you can use custom fonts in CSS:

Font Formats

Custom fonts come in various formats like TrueType (.ttf), OpenType (.otf), Web Open Font Format (.woff), and Web Open Font Format 2 (.woff2). It's essential to provide font files in different formats to ensure compatibility across various browsers.

Font Sources

There are multiple sources from which you can obtain custom fonts:

  1. Self-hosted: You can host font files on your server.
  2. Third-party services: Platforms like Google Fonts, Adobe Fonts, and Font Squirrel offer a wide range of fonts that you can easily integrate into your website.

Using @font-face

The @font-face rule in CSS allows you to specify custom fonts and their sources. Here's a basic example:

@font-face { font-family: 'CustomFont'; src: url('customfont.woff2') format('woff2'), url('customfont.woff') format('woff'); /* Add more src declarations for different font formats */ }

Applying the Font

Once you've defined the custom font, you can apply it to specific elements in your CSS:

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

Fallback Fonts

It's crucial to provide fallback fonts in case the custom font fails to load. This ensures that users still see content even if the custom font isn't available:

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

Let's say you want to use a custom font called "Montserrat" from Google Fonts:

First, include the font in your HTML file:

<link rel="stylesheet" href="">

Then, in your CSS, you can apply this font:

body { font-family: 'Montserrat', sans-serif; }
run this source code Browser View

Welcome to My Website

This is an example of using the custom font Montserrat from Google Fonts.

Feel free to explore!

Full Source:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Using Custom Font Montserrat from Google Fonts</title> <link rel="stylesheet" href=""> <style> body { font-family: 'Montserrat', sans-serif; /* Fallback font in case Montserrat fails to load */ /* font-family: sans-serif; */ } /* Additional CSS styles can be added here */ </style> </head> <body> <h1>Welcome to My Website</h1> <p>This is an example of using the custom font Montserrat from Google Fonts.</p> <p>Feel free to explore!</p> </body> </html>

In this example, we've included the Montserrat font from Google Fonts in the HTML <head> section using a <link> tag. Then, in the CSS section within <style> tags, we've applied the font to the body element. Now, all text within the body will be displayed using the Montserrat font.

Alternatively, if you have a font file hosted on your server:
  1. Place the font files in your project directory.
  2. Define the @font-face rule in your CSS to specify the font source.
  3. Apply the font to desired elements.

Font Sources and Hosting

You have two options:


Self-hosting involves uploading font files directly to your server and then referencing them in the src property of the @font-face rule in your CSS. This approach gives you complete control over the fonts you use on your website and ensures that they are served directly from your server to the users' browsers. However, self-hosting requires additional server configuration and management, as you need to ensure that the fonts are properly served, optimized for web usage, and have appropriate caching policies in place to improve loading times. Despite the added complexity, self-hosting can be advantageous in terms of performance and privacy, as it reduces reliance on external services and ensures consistent font rendering across different platforms and browsers.

Font hosting services

Font hosting services, such as Google Fonts or Adobe Fonts, provide a convenient solution for accessing a wide range of fonts without the need for self-hosting. These platforms offer both free and paid fonts with hosting services and easy integration options. To use fonts from these services, you simply need to include the provided URL in the src property of the @font-face rule in your CSS. This URL serves as a reference to the hosted font files, allowing seamless usage without the need to manually upload and manage font files on your server. Font hosting services handle aspects like optimization, delivery, and compatibility across different devices and browsers, simplifying the process of integrating custom fonts into your website and reducing the burden of font management and maintenance.

Browser Compatibility

  1. Different browsers support different font formats. Use multiple formats like WOFF, WOFF2, and TTF for wider compatibility.
  2. Check browser compatibility charts for specific font formats.

Performance and Optimization

Excessive font usage can significantly impact loading times on websites, potentially leading to a poor user experience. To mitigate this, consider using font subsets to include only the specific characters required for your content, thereby reducing the overall file size of the font. By including only necessary characters, you minimize the amount of data that needs to be transferred, resulting in faster loading times. Additionally, employing font optimization tools can further enhance performance by compressing font files without compromising quality. These tools help minimize file sizes while maintaining the visual integrity of the fonts, ultimately improving loading speeds and ensuring a smoother browsing experience for your users.


Google Fonts

@font-face { font-family: "Open Sans"; src: url(;400;700&display=swap); } h1, p { font-family: "Open Sans", sans-serif; }

Self-hosted font

@font-face { font-family: "MyHandwrittenFont"; src: url("fonts/myfont.woff") format("woff"), url("fonts/myfont.ttf") format("truetype"); } .title { font-family: "MyHandwrittenFont", cursive; }


Custom Fonts in CSS enable web developers to enhance the visual appeal and branding of their websites by incorporating unique typography that may not be available on users' devices by default. Developers can specify custom fonts using the @font-face rule, allowing for flexible integration from various sources such as self-hosted files or third-party services like Google Fonts. This customization not only enriches the design landscape but also contributes to improved user engagement and overall aesthetics of the website.