How to add a custom fonts to your Website

Custom fonts are fonts that are not included as part of the standard set of fonts that come with a computer or a web browser. These fonts are designed specifically for use on a website or in a particular application, and are often used to create a unique and consistent brand identity across all of a company's communication channels.

There are many different types of custom fonts, including display fonts, which are used for headings and other large text; script fonts, which are designed to look like handwriting or calligraphy; and serif and sans-serif fonts, which are used for body text. Custom fonts can also include unique symbols, icons, or glyphs that are not included in standard fonts.

To use custom fonts on a website, the font files must be hosted on a server and linked to in the website's HTML or CSS code. There are several different file formats that can be used for custom fonts, including TrueType (.ttf), OpenType (.otf), and Web Open Font Format (.woff and .woff2).

Incorporate custom fonts into an HTML page

Following is a step-by-step guide on how to incorporate custom fonts into an HTML page:

Choose your font

First, you need to choose the custom font you want to use. There are many websites that offer custom fonts for free or for purchase, such as Google Fonts, Adobe Fonts, or Typekit. Once you've found a font you like, download the font files to your computer.

Upload your font files

Next, you need to upload your font files to a web server so that they can be accessed by your HTML page. You can either host the font files yourself, or use a third-party service such as Google Fonts to host the files for you.

Link to your font files

In your HTML code, you need to link to the font files using the link tag. You'll need to specify the href attribute to point to the location of the font file on your web server, and the rel attribute with the value "stylesheet" to indicate that this is a CSS file.

<link href="https://example.com/fonts/myfont.woff2" rel="stylesheet">

You can also specify additional attributes, such as type and media, if needed.

Define your font family

Once you've linked to your font files, you need to define the font family in your CSS code using the @font-face rule.

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

In this example, defined a font family called "MyFont" and linked to the font files using the src attribute. Specified two different file formats (woff2 and woff) to ensure maximum compatibility across different browsers. Also set the font-weight and font-style attributes to "normal" to indicate that this is a regular font, but you can customize these values as needed.

Use your custom font

Finally, you can use your custom font in your HTML code by specifying the font family in your CSS.

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

In this example, specified that the font family for the body element should be "MyFont", followed by a fallback font family of sans-serif in case the custom font isn't available or can't be loaded.

Full Source : HTML
<!DOCTYPE html> <html> <head> <title>Custom Font Example</title> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@400;700&display=swap" rel="stylesheet"> <style> body { font-family: 'Open Sans', sans-serif; } h1 { font-family: 'Open Sans', sans-serif; font-weight: 700; font-size: 2em; } </style> </head> <body> <h1>Hello, world!</h1> <p>This text is using the Open Sans font.</p> </body> </html>
run this source code Browser View

Hello, world!

This text is using the Open Sans font.

In the above example, specified that the font family for the body element and the h1 element should be "Open Sans", followed by a fallback font family of sans-serif in case the custom font isn't available or can't be loaded. Also set the font-weight attribute to 700 for the h1 element to make it bold, and set the font size to 2em.

Conclusion:

While custom fonts can be a powerful tool for creating a distinctive brand identity, it's important to use them carefully and sparingly. Large or overly decorative fonts can be difficult to read, particularly on smaller screens or in low-contrast environments, and can negatively impact the user experience. Additionally, some custom fonts may not be available on all devices or may load more slowly than standard fonts, which can lead to performance issues.