
CSS Web Fonts: Font Formats, @font-face, and Repositories
24/07/2023
How to Use SVG in HTML and CSS
25/07/2023In the web fonts lesson, we loaded typefaces from Google Fonts. This lesson shows how to use Google’s Material Icons and Symbols.
1. What are Google Fonts icons?
Google distributes scalable icons you can use as SVG files or through an icon font. CSS can change their color and size. Variable fonts give you additional controls over weight, fill, grade, and optical size.
For your own vector artwork, see how to use SVG in HTML and CSS.
2. How to find icons in Google Fonts
Open the Google Fonts icon library to browse the available icons.
2.1. Search for icons
Search by name or narrow the results by category.
2.2. Static icons and variable symbols
The library offers icons with fixed shapes and Material Symbols with adjustable axes.
Static icons
Static icons have a fixed shape and default stroke and fill. You can still change their color and size with CSS.
Variable symbols
A variable icon font lets you adjust several design properties of the symbol. Common axes include:
- Weight: Changes the thickness of the strokes.
- Fill: Switches between an outlined and a filled appearance, where the symbol supports both.
- Grade: Adjusts perceived stroke weight more subtly than the weight axis.
- Optical size: Adjusts the symbol’s drawing for the size at which it is displayed.
Use the icon library’s customization controls to preview these settings.
3. How to add Google icons to your site
The following examples show the static font and the variable font approaches.
3.1. Use static icons
Find the icon and copy the name displayed in the library. You will use that name as the icon font’s ligature text.
3.1.1. Add the stylesheet in <head>
Place the stylesheet link in your HTML document’s <head>:
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
3.1.2. Insert the icon in HTML
Add an <i> or <span> with the material-icons class and put the icon name inside it. In these examples the icons are decorative, so they are hidden from assistive technology.
<!-- Using the element <i> --> <i class="material-icons" aria-hidden="true">home</i> <!-- Using the element <span> --> <span class="material-icons" aria-hidden="true">favorite</span>
3.1.3. Style the icons with CSS
Use CSS to change size, color, shadow, rotation, or background. Apply one of these examples or give each variant its own selector.
/* Change the icon size */
.material-icons {
font-size: 36px;
}
/* Change the icon color */
.material-icons {
color: #ff6347;
}
/* Add a shadow */
.material-icons {
text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5);
}
/* Rotate the icon */
.material-icons {
transform: rotate(45deg);
}
/* Change the background behind the icon */
.material-icons {
background-color: #f0f0f0;
padding: 5px;
border-radius: 50%;
color: #333;
}
Use a variable font when you need to change the stroke weight, fill, or optical size without choosing a different icon family.
3.2. Use variable Material Symbols
Open the customization area in the icon library. Use its controls to preview the fill, weight, grade, optical size, and style before copying the embed code.
- Fill: Controls the fill of the icon, from outline to fully filled where available.
- Weight: Sets stroke thickness, from lighter to heavier shapes across the range supported by the font.
- Grade: Makes subtler adjustments to the strokes. A lower grade can reduce emphasis and a higher grade can increase it.
- Optical size: Tunes the icon’s drawing for its rendered size.
- Filter, style, and category: Narrow the selection when you are looking for a specific icon or family.
Select an icon to open its details, then copy the web embed code or download the asset in the available format.
4. Integration with frameworks
You can use Material Icons alongside a framework such as Bootstrap. The example uses a Bootstrap button class and an icon from the Google font.
<button class="btn btn-primary"> <i class="material-icons" aria-hidden="true">cloud_upload</i> Upload file </button>
5. Best practices for designing with icons
Icons work best when their meaning, size, and style are consistent across the interface.
- Size and style: Use similar sizes for icons that perform similar roles, and choose a consistent outlined or filled style.
- Meaning: Pick a recognizable icon that represents the action. Keep a text label when the icon could be ambiguous.
- Visual consistency: Choose icons that fit the site’s color scheme and visual style.
- Usability and accessibility: Make controls easy to identify and tap. Give an icon-only control an accessible name. When an icon repeats a visible label, add
aria-hidden="true"to the icon.
<button type="button" aria-label="Home"> <span class="material-icons" aria-hidden="true">home</span> </button>
6. Practical examples with Google Fonts icons
The following examples use icons on common page elements.
6.1. Links and buttons
Button with an icon:
<button class="btn btn-primary"> <i class="material-icons" aria-hidden="true">search</i> Search </button>
Link with an icon:
<a href="#" class="link"> <i class="material-icons" aria-hidden="true">arrow_forward</i> Read more </a>
6.2. Navigation menus
Navigation item with an icon:
<nav>
<ul>
<li><a href="#"><i class="material-icons" aria-hidden="true">home</i> Home</a></li>
<li><a href="#"><i class="material-icons" aria-hidden="true">shopping_cart</i> Shop</a></li>
<li><a href="#"><i class="material-icons" aria-hidden="true">mail</i> Contact</a></li>
</ul>
</nav>
6.3. Headings and sections
Heading with an icon:
<h1> <i class="material-icons" aria-hidden="true">info</i> About us </h1>
Featured section with an icon:
<div class="section"> <i class="material-icons" aria-hidden="true">star</i> <h2>Special offer!</h2> <p>Discover our exclusive offers.</p> </div>
6.4. Lists and other content
List item with an icon:
<ul> <li><i class="material-icons" aria-hidden="true">check_circle</i> Item 1</li> <li><i class="material-icons" aria-hidden="true">check_circle</i> Item 2</li> <li><i class="material-icons" aria-hidden="true">check_circle</i> Item 3</li> </ul>
6.5. Forms and controls
Input with an icon:
<div class="input-container"> <i class="material-icons" aria-hidden="true">person</i> <label for="name">Name</label> <input type="text" id="name" name="name"> </div>
Submit button with an icon:
<button type="submit" class="btn btn-primary"> <i class="material-icons" aria-hidden="true">send</i> Enviar </button>
Use CSS to adapt the size, color, and alignment of these examples. Check that the labels are still clear without the icon font.
Send questions or suggestions through the Eniun contact form. You can also subscribe to the Eniun newsletter for new articles and resources.