How to add "Ubuntu Font Family" to my site using CSS?
Categories:
Adding the Ubuntu Font Family to Your Website with CSS

Learn how to seamlessly integrate the distinctive Ubuntu Font Family into your web projects using various CSS methods, enhancing your site's typography and visual appeal.
The Ubuntu Font Family is a set of modern, humanist-style fonts designed for clarity and readability on screens. Developed by Dalton Maag for Canonical, the creators of the Ubuntu operating system, these fonts offer a distinctive look that can significantly enhance your website's aesthetic. This article will guide you through the process of incorporating the Ubuntu Font Family into your web projects using CSS, covering common methods and best practices.
Method 1: Using Google Fonts (Recommended)
The easiest and most common way to add the Ubuntu Font Family to your website is by leveraging Google Fonts. Google Fonts provides a robust and free service that hosts web fonts, making them readily available for use across the web. This method ensures optimal performance and broad browser compatibility.
1. Visit Google Fonts
Navigate to the Google Fonts website (fonts.google.com) and search for "Ubuntu".
2. Select Styles
Choose the specific font weights and styles (e.g., Regular 400, Bold 700, Italic) you wish to use. The more styles you select, the larger the font file size, which can impact page load times.
3. Embed Code
Google Fonts will generate a <link>
tag for your HTML or an @import
rule for your CSS. Copy the generated <link>
tag.
4. Add to HTML
Paste the <link>
tag into the <head>
section of your HTML document. It should look something like this:
<head>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Ubuntu:wght@400;700&display=swap" rel="stylesheet">
</head>
Embedding Ubuntu Font Family via Google Fonts link in HTML
1. Apply with CSS
Once the font is linked, you can apply it to your desired HTML elements using the font-family
CSS property. Google Fonts provides the exact font-family
declaration to use.
2. Consider Fallback Fonts
Always include generic font families (like sans-serif
) as fallbacks. If for any reason the Ubuntu font fails to load, the browser will use a similar system font, preventing your text from appearing in an unstyled default font.
body {
font-family: 'Ubuntu', sans-serif;
}
h1 {
font-family: 'Ubuntu', sans-serif;
font-weight: 700;
}
Applying the Ubuntu font to body and heading elements
<link>
tag for Google Fonts as early as possible in the <head>
section of your HTML. This allows the browser to start downloading the fonts sooner.Method 2: Self-Hosting the Ubuntu Font Family
Self-hosting fonts gives you more control over font files and can sometimes offer better performance, especially if you're already hosting other assets on your own server or using a CDN. This method involves downloading the font files and serving them directly from your web server.
1. Download Font Files
Download the Ubuntu Font Family files (typically in .woff
, .woff2
, .ttf
, .eot
, .svg
formats) from a reliable source like Google Fonts (by clicking the download icon) or the official Ubuntu Font Family website. .woff2
and .woff
are generally sufficient for modern browsers.
2. Organize Font Files
Create a dedicated folder for your fonts (e.g., /fonts
) in your project directory and place the downloaded font files there.
3. Declare with @font-face
Use the @font-face
CSS rule to declare your custom font. This rule tells the browser where to find the font files and how to refer to them in your CSS.
@font-face {
font-family: 'Ubuntu';
src: url('/fonts/Ubuntu-Regular.woff2') format('woff2'),
url('/fonts/Ubuntu-Regular.woff') format('woff');
font-weight: 400;
font-style: normal;
font-display: swap; /* Improves performance by displaying fallback text */
}
@font-face {
font-family: 'Ubuntu';
src: url('/fonts/Ubuntu-Bold.woff2') format('woff2'),
url('/fonts/Ubuntu-Bold.woff') format('woff');
font-weight: 700;
font-style: normal;
font-display: swap;
}
Self-hosting Ubuntu font using @font-face rule
1. Apply with CSS
Once declared, you can use font-family: 'Ubuntu', sans-serif;
in your CSS, just as you would with Google Fonts.
2. Configure Web Server (if necessary)
Ensure your web server is configured to serve font files with the correct MIME types. For example, .woff2
should be font/woff2
and .woff
should be font/woff
.
Understanding Font Loading Process
Regardless of the method you choose, it's helpful to understand how browsers load web fonts. This process can impact your site's perceived performance. The font-display
CSS property, used in the @font-face
rule, plays a crucial role in managing this behavior.
flowchart TD A[HTML Parsing] --> B{Font Linked/Declared?} B -->|Yes| C[Browser Requests Font File] C --> D{Font Downloaded?} D -->|Yes| E[Apply Font to Text] D -->|No (Timeout/Error)| F[Apply Fallback Font] B -->|No| F E --> G[Render Text with Ubuntu Font] F --> H[Render Text with Fallback Font]
Simplified Web Font Loading Process
The font-display: swap;
value is generally recommended for most cases. It tells the browser to immediately display text using a fallback font while the custom font is loading, and then 'swap' to the custom font once it's available. This prevents a 'flash of invisible text' (FOIT) and improves the user experience.