Icon fonts are commonly used to display small, reusable graphical symbols on websites. While sprite sheets have their advantages, they come with notable drawbacks:
- Image files tend to be relatively large.
- Scaling images leads to loss of quality.
- Modifying or updating icons after creation is cumbersome.
Icon fonts address these issues effectively. Though they appear as graphics, they are fundamentally text-based—rendered using custom font files.
Advantages of Icon Fonts
- Lightweight: A single font file containing multiple icons is typically smaller than individual image assets. Once loaded, icons render instantly without additional HTTP requests.
- Styling flexibility: As text elements, icons can be easily styled—color, opacity, shadows, rotation, and sizing can all be controlled via CSS.
- Broad browser support: Compatible with virtually all modern (and many legacy) browsers.
1. Sourcing Icon Fonts
Two widely used platforms for generating custom icon fonts:
- IcoMoon: Offers extensive icon selection and custom font generation. Note: Hosted overseas, which may affect load speed in some regions.
- Alibaba IconFont: A free Chinese platform by Alibaba’s M2UX team, supporting uploads from vector tools like Adobe Illustrator.
2. Integrating into HTML
After downloading the icon font package:
- Place the
fonts/directory in your project’s root. - Include the necessary font formats to ensure cross-browser compatibility.
Supported Font Formats
- .ttf (TrueType): Supported by IE9+, Firefox 3.5+, Chrome 4+, Safari 3+, Opera 10+, iOS Safari.
- .woff (Web Open Font Format): Supported by IE9+, Firefox 3.5+, Chrome 6+, Safari 3.6+, Opera 11.1+.
- .eot (Embedded Open Type): Legacy format for IE4+.
- .svg: Supported by Chrome 4+, Safari 3.1+, Opera 10+, iOS Safari 3.2+.
3. Declaring and Using Icons
Define the font face in CSS and apply it to HTML elements:
<style>
@font-face {
font-family: 'CustomIcons';
src: url('fonts/custom.eot?hash');
src: url('fonts/custom.eot?hash#iefix') format('embedded-opentype'),
url('fonts/custom.ttf?hash') format('truetype'),
url('fonts/custom.woff?hash') format('woff'),
url('fonts/custom.svg?hash#CustomIcons') format('svg');
font-weight: normal;
font-style: normal;
font-display: block;
}
.icon {
font-family: 'CustomIcons';
font-size: 48px;
color: #ff69b4;
}
</style>
<body>
<span class="icon"></span>
<span class="icon"></span>
</body>
Two Ways to Insert Icons
- Direct insertion: Copy the Unicode character (e.g.,
) from the demo HTML provided in the download and place it inside an element like<span>. - Pseudo-element method: Use the icon’s Unicode value (prefixed with
\) in a::beforeor::afterpseudo-element:
.menu-item::after {
font-family: 'CustomIcons';
content: '\e920';
font-size: 14px;
color: white;
}
Adding New Icons Later
To extend an existing icon font:
- Locate the
selection.jsonfile from your original download. - Upload it to the icon font platform (e.g., IcoMoon or IconFont).
- Select additional icons, regenerate the font package, and replace the old files in your project.