Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Your Own Font

Fonts are in the theme too. The classes font-sans, font-serif and font-mono come from the variables --font-sans, --font-serif and --font-mono, and every variable whose name starts with --font- makes a class that sets font-family. Let's give the headings a rounded, friendly font that suits a cat shelter: Baloo 2 from Google Fonts.

In the CSS course, you loaded a web font with link elements in the head of the page. That works with Tailwind too, but there's another way, right in the stylesheet: the @import rule, which loads another stylesheet, just as @import "tailwindcss" does. Google Fonts offers it in its embed code (Get embed code) too: choose @import instead of <link>, and copy only the line with @import, without the <style> tags around it.

@import url("https://fonts.googleapis.com/css2?family=Baloo+2:wght@400;600;700&display=swap");
@import "tailwindcss";

The address is the same as in a link element: the Baloo 2 font in the weights 400, 600 and 700 (in the address, the space in the name is written as +). Mind the order: an @import must come before all other rules in a stylesheet. @import "tailwindcss" brings in a lot of rules (Preflight and the classes), so an @import written after it comes too late. The browser skips it, without any error, and the page simply uses the fallback font.

Then you add the font to the theme:

@theme {
	--color-brand-500: #f97316;
	--color-brand-600: #ea580c;
	--font-display: "Baloo 2", sans-serif;
}

The variable --font-display makes the class font-display:

.font-display {
	font-family: var(--font-display);
}

The value is a list of fonts, as in font-family: if Baloo 2 can't be downloaded, the browser uses its sans-serif font. The name has a space in it, so it must be in quotes, as you know from the CSS course. The name display is our choice: fonts for headings and other large text are called display fonts. You could just as well call the variable --font-headings and use the class font-headings.

You can also change a value that Tailwind already has. Preflight gives the whole page the font from --font-sans, so with --font-sans: "Nunito", sans-serif; in @theme, all the text on the page would be in Nunito (if you imported it too).

Instructions

  1. 1

    At the very beginning of style.css, before @import "tailwindcss";, add the line that imports Baloo 2 (copy it from the first example above).

  2. 2

    In the @theme block, add --font-display: "Baloo 2", sans-serif;.

  3. 3

    Add the class font-display to the logo in the header (🐾 Whiskers), to the headline h1 and to the heading h2 Our Cats.

Back

2/7

  1. 1. Your Own Colors
  2. 2. Your Own Font
  3. 3. Repeating Classes
  4. 4. Your Own Class with @apply
  5. 5. Your Own Utility
  6. 6. Which Way to Choose
  7. 7. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.