HTML Icon Fonts
In this page:
What Are Icon Fonts?
An icon font is a typeface where each character maps to a small icon instead of a letter or number. Because icons are just font glyphs, they inherit ordinary text styling like size, color, and shadow. Font Awesome is the most popular icon font library on the web, offering a huge catalog of icons and powering millions of sites.
Note: Icon fonts are a good choice when you need many simple, single-color icons that must resize cleanly at any zoom level.
Warning: Icon fonts render as plain squares or missing-character boxes if their font file fails to load.
Example: What Are Icon Fonts?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
</head>
<body>
<i class="fa-solid fa-star"></i>
</body>
</html>
Linking an Icon Font Library
Most icon fonts are distributed as a stylesheet plus one or more font files, similar to a web font. Linking the stylesheet in your document's head makes every icon class available across the page. When choosing a library: Font Awesome is the most popular option with by far the largest icon set, Google's Material Icons follow Google's own Material Design system and suit apps already built around it, and Bootstrap Icons are deliberately lightweight, making them a good fit for projects already using the Bootstrap framework.
Note: Host the icon font files yourself if your site needs to work reliably without depending on a third-party server.
Warning: Loading an icon font library twice on the same page can cause conflicting class names and unexpected icon glyphs.
Example: Linking an Icon Font Library
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
</head>
<body>
<i class="fa-solid fa-heart"></i>
</body>
</html>
Using Icon Classes in HTML
Icon fonts are usually triggered with a base class plus a specific icon class, applied to an empty inline element. The base class sets up the font family, while the specific class selects which character (icon) to display. Google's Material Icons are drawn directly from Google's Material Design system, making them a natural choice for interfaces that follow Material Design guidance.
Note: Use a semantic wrapping element, like a button or link, around the icon so its purpose is clear even without the icon rendering.
Warning: Adding text content inside the icon element can cause stray letters to appear alongside the icon glyph.
Example: Using Icon Classes in HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
</head>
<body>
<button>
<i class="fa-solid fa-trash"></i> Delete
</button>
</body>
</html>
Sizing and Coloring Icons with CSS
Because icon font glyphs are text characters, ordinary CSS properties like font-size and color control how large and what color each icon appears, without needing separate icon image files. Bootstrap Icons are intentionally lightweight, which makes them well suited to projects already using the Bootstrap framework and wanting to avoid pulling in a larger icon library.
Note: Set icon size in em units so icons scale automatically alongside surrounding text.
Warning: Changing color on a parent element can unintentionally recolor every icon nested inside it.
Example: Sizing and Coloring Icons with CSS
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
</head>
<body>
<i class="fa-solid fa-star" style="font-size: 2em; color: gold;"></i>
</body>
</html>
Accessibility Considerations for Icon Fonts
Since icon fonts render as decorative characters, screen readers may either skip them or read the underlying character incorrectly. Providing a text alternative ensures the icon's meaning is not lost for visitors using assistive technology.
Note: Add aria-hidden=true to purely decorative icons and rely on visible or screen-reader-only text for meaning instead.
Warning: Relying on an icon alone to convey a critical action, like delete or submit, can confuse visitors who cannot see it clearly.
Example: Accessibility Considerations for Icon Fonts
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
<style>
.sr-only {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
}
</style>
</head>
<body>
<button>
<i class="fa-solid fa-trash" aria-hidden="true"></i>
<span class="sr-only">Delete</span>
</button>
</body>
</html>
- Using icon fonts without any accessible text alternative, leaving screen reader users with no idea what the icon represents.
- Forgetting to load the icon font's stylesheet, which causes icons to display as empty boxes or missing characters.
- Overriding font-family accidentally on a parent element, which breaks every icon nested inside it.
- Icon fonts store icons as characters inside a font file, letting you style them with normal CSS text properties.
- Icon classes are usually added to an empty <span> or <i> element that loads the correct character from the font.
- Always pair icon fonts with accessible labels so the icon's meaning is not lost on assistive technology.
Icon fonts rely on standard font-loading and rendering, so they work in every modern browser without extra plugins.
Chapter Quiz — Complete all 20 topics to unlock
0/20 topics done
Complete these topics first:
- HTML Keyboard Shortcuts
- HTML Browser Support
- HTML Character Sets
- HTML Doctypes
- HTML Audio/Video Reference
- HTML Meta Tags
- HTML ARIA Roles
- HTML Input Validation
- HTML Iframe Security
- HTML Responsive Images
- HTML Web Fonts
- HTML Icon Fonts
- HTML Progress & Meter
- HTML Output Element
- HTML Datalist
- HTML Image Maps
- HTML Browser DevTools
- HTML Validation
- HTML Quiz
- HTML Interview Prep