Comprehensive Guide on How to Import Fonts into CSS 

Green coding text projected over a dark background with a mysterious vibe

Integrating a new font into a webpage can be accomplished using HTML or CSS, with the choice of method depending on specific project needs. In the realm of web design, mastering the art of importing fonts into CSS serves as a crucial foundation. This skill becomes particularly relevant when exploring additional elements, such as creating stylish switch buttons through CSS, as detailed in this article.

This article will provide a comprehensive walkthrough of both techniques for embedding fonts.

Incorporating Fonts with HTML

Utilizing HTML to incorporate fonts is particularly advantageous when employing externally hosted options like Google Fonts.

This is achieved by employing the <link> tag within the <head> section of your webpage, setting its href attribute to the font’s URL.

Example:

<link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet">

Providers like Google Fonts supply a snippet of code that can be directly inserted into the HTML. It’s possible to reference multiple fonts by inserting separate <link> tags for each desired font family. Alternatively, Google Fonts provides a consolidated <link> element that amalgamates multiple font requests into a singular URL query.

Once the font is imported, it can be applied site-wide or to specific elements using the CSS font-family property.

Example:

p {
  font-family: 'Roboto', sans-serif;
}

Incorporating Multiple Fonts with Google Fonts

Google Fonts provides a swift and efficient means to incorporate a variety of fonts onto your webpage. Yet, there are considerations to keep in mind with this method:

  • Potential failure of the server request can lead to fonts not loading;
  • Server response times may be slower compared to hosting fonts locally, impacting page load times;
  • Privacy concerns may arise when loading fonts from external services like Google Fonts, especially in light of regulations such as GDPR.

Embedding Fonts Using CSS

Alternatively, fonts can be embedded directly via CSS, which proves to be useful when modifying the HTML directly isn’t an option. This method offers increased flexibility and customization options, allowing for a more tailored application of fonts within your design.

Incorporating a Font Using the @import Rule:

To include a font, the initial method involves utilizing the @import rule. Within your CSS file or the <style> tag on your page, declare the @import rule and specify the URL of the desired font.

Example:

@import url('https://fonts.googleapis.com/css2?family=Roboto&display=swap');

When importing Google Fonts, as demonstrated in the above example, you’ll receive a code snippet for the @import. Apply the font to your text using the font-family property in the same manner.

Example:

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

Hosting Fonts Locally for Web Use

An open laptop with scattered letter cutouts around it

For those prioritizing page load speeds, compliance with GDPR, or needing to use a proprietary font not available on platforms like Google Fonts, self-hosting fonts is a viable option.

This involves keeping font files within a dedicated directory in your project’s structure.

Within your CSS, you can declare these locally stored fonts using the @font-face rule. This requires naming the font and specifying the path to its files, ensuring the fonts are accessible and utilized correctly throughout the website.

Example:

@font-face {
  font-family: "Roboto";
  src: url("/fonts/Roboto-Medium.ttf") format("truetype");
  font-weight: 400;
}

@font-face {
  font-family: "Roboto";
  src: url("/fonts/Roboto-Bold.ttf") format("truetype");
  font-weight: 700;
}

To implement this font, once more, utilize the font-family property.

Example:

body {
  font-family: 'Roboto', sans-serif;
  font-weight: 400;
}

h1 {
  font-family: inherit;
  font-weight: 700;
}

Conclusion

Incorporating fonts into a website’s design using CSS is a straightforward process. This guide demonstrates that whether through HTML, direct CSS inclusion, or by using self-hosted fonts, each method offers specific advantages. When selecting the best approach, it’s important to evaluate aspects such as loading performance, GDPR considerations, and the extent of customization required. Implementing custom fonts effectively can significantly enhance the user’s experience, making the website more engaging. Mastering font imports in CSS is an essential skill for web designers, allowing for greater creative expression and design versatility.