How to Make Browser Download Fonts How to Add Fonts to a React Project
Total Page:16
File Type:pdf, Size:1020Kb
How to make browser download fonts How to add Fonts to a React Project. Traditionally, adding a font to a website meant downloading the necessary font files into your project and creating the necessary @font-face css code to adapt to different web browsers requirements. Table of Contents. Using @font-face Using Hosted Fonts Using Web Font Loader. The above approach works just fine and is probably one of the best solutions if your app or website will need to run within an intranet with no Internet access. But what if its an online website being accessed by thousands or millions of visitors everyday. You would probably look for a solution that can provide better performance and less load to your servers. Using Hosted Fonts. The recommended solution is to load your font from a Hosted Fonts Provider like Google Fonts. The logic behind this recommendation is that browsers usually download web page resources in a sequential order before it can fully construct a page for rendering. However, if some of the page resources are on a different server, it can download them in parallel, hence a page can be loaded much faster. Let's quickly get started with a basic react project which we shall add a font from Google Fonts. We are going to use the create-react-app tool to quickly kick-off the project. You can easily install it by executing: After you have installed the tool, we can now go ahead and create our project: When you the run the project, you will be greeted with a page like this: Let's add some content in the src/App.js file within the existing "App" div. The web page should now look like this with the default fonts. Let's now pick some fonts from Google Fonts. Select any font that hasn't been installed in your machine, in my case I chose Titillium Web. I also added 2 more font weights that I like. To quickly add the fonts to our react project, simple open public/index.html and paste the embed code provided within the tags. Save the file and edit the src/index.css and add this: After saving the above file, the web page should now look like this: Using Web Font Loader. The above approach is the simplest method of adding fonts to your web app. However, if you were to run your site on Google Page Speed to check for performance issues, that line we added in public/index.html will get flagged for the following reason: Render Blocking CSS. The best solution I found to fix this was to use Web Font Loader, a product that is co-developed by Google and Typekit. You can use this tool to load self-hosted fonts or fonts provided through APIs. To use it, simply install it as an npm package in your project: Go to your public/index.html and comment the link we added. Open src/index.js and add the following code: Save the file and restart the react app. The site should appear exactly the same. However, I must warn you that when users first launch the web app, they will experience a few seconds of FOUT(Flash of Unstyled Text). Luckily, most modern browsers have caching mechanisms and this phenomenon won't occur again on subsequent reloads unless the user clears or disables the browser's cache. If you would like to access the code used in this tutorial you can access it here. CSS Web Fonts. Web fonts allow Web designers to use fonts that are not installed on the user's computer. When you have found/bought the font you wish to use, just include the font file on your web server, and it will be automatically downloaded to the user when needed. Your "own" fonts are defined within the CSS @font-face rule. Different Font Formats. TrueType Fonts (TTF) TrueType is a font standard developed in the late 1980s, by Apple and Microsoft. TrueType is the most common font format for both the Mac OS and Microsoft Windows operating systems. OpenType Fonts (OTF) OpenType is a format for scalable computer fonts. It was built on TrueType, and is a registered trademark of Microsoft. OpenType fonts are used commonly today on the major computer platforms. The Web Open Font Format (WOFF) WOFF is a font format for use in web pages. It was developed in 2009, and is now a W3C Recommendation. WOFF is essentially OpenType or TrueType with compression and additional metadata. The goal is to support font distribution from a server to a client over a network with bandwidth constraints. The Web Open Font Format (WOFF 2.0) TrueType/OpenType font that provides better compression than WOFF 1.0. SVG Fonts/Shapes. SVG fonts allow SVG to be used as glyphs when displaying text. The SVG 1.1 specification define a font module that allows the creation of fonts within an SVG document. You can also apply CSS to SVG documents, and the @font-face rule can be applied to text in SVG documents. Embedded OpenType Fonts (EOT) EOT fonts are a compact form of OpenType fonts designed by Microsoft for use as embedded fonts on web pages. Browser Support for Font Formats. The numbers in the table specifies the first browser version that fully supports the font format. Font format TTF/OTF 9.0* 4.0 3.5 3.1 10.0 WOFF 9.0 5.0 3.6 5.1 11.1 WOFF2 14.0 36.0 39.0 10.0 26.0 SVG Not supported Not supported Not supported 3.2 Not supported EOT 6.0 Not supported Not supported Not supported Not supported. *IE: The font format only works when set to be "installable". Using The Font You Want. In the @font-face rule; first define a name for the font (e.g. myFirstFont) and then point to the font file. Tip: Always use lowercase letters for the font URL. Uppercase letters can give unexpected results in IE. To use the font for an HTML element, refer to the name of the font (myFirstFont) through the font-family property: Example. @font-face font-family: myFirstFont; src: url(sansation_light.woff); > div font-family: myFirstFont; > Using Bold Text. You must add another @font-face rule containing descriptors for bold text: Example. The file "sansation_bold.woff" is another font file, that contains the bold characters for the Sansation font. Browsers will use this whenever a piece of text with the font-family "myFirstFont" should render as bold. This way you can have many @font-face rules for the same font. Test Yourself with Exercises! CSS Font Descriptors. The following table lists all the font descriptors that can be defined inside the @font-face rule: How to Install and Use Web Fonts. Jo here with little bonus for you this week! You’ve already had a couple of tutorials for some inspiration on how to use the resources in the current 30 Best Selling Creative Fonts (With Web Fonts and Extended Licensing) deal, which we hope you’re enjoying. One of the great features of this bundle is the inclusion of desktop fonts and webfonts allowing you to utilise them not just to make your print/graphic designs stand out, but your web designs too. After several community members asked about exactly how to use the web fonts available in this bundle, we decided to put together a helpful guide, for all you guys who are new to working with web fonts. There’s no need to be intimidated or put off from using web fonts, and as this guide will show you, they’re quite easy to use once you understand the basics. Ok, lets get started! There is a full text version of this tutorial below, but I’ve also put together a short video walking you through each step of the process. You’ll be using your new web fonts in no time :). Step 1: Select your webfonts. Here we have our basic webpage: As you can see, it could do with a little webfont love to liven it up. The first step is to decide which fonts you want to use (there’s a list of resources on how to match fonts in ‘Step 9’ in my previous tutorial). In this example we’re going to use ‘Atlas Slab’ and ‘Polly’ as our fonts: Once you’ve decided on the font, navigate to the webfonts folder within it, and you should see something like this: You’ll notice there are a variety of different formats that webfonts come in: WOFF Stands for: Web Open Font Format. WOFF fonts were created specifically for web use, and often load faster than other formats because they use a compressed version of the structure used by OpenType (OTF) and TrueType (TTF) fonts. This format is generally preferred because it can also include metadata and license info within the font file itself. SVG / SVGZ Stands for: Scalable Vector Graphics (Font) An SVG is a vector re-creation of the font, which makes it a smaller file size, and is also great for mobile use. This format is the only one allowed by version 4.1 and below of Safari for iPhone. Currently SVG fonts are not supported by Firefox, IE or IE Mobile. SVGZ is a zipped version of SVG. EOT Stands for: Embedded Open Type. This format was created by Microsoft and is the only format that IE8 and below will recognize when using @font-face (we’ll cover this in Step 3) within a stylesheet.