Skip to main content
Syllabus
On this page

Icons (SVG & Icon Fonts)

Badar KhalilUpdated September 30, 2026 10 min read

Icons in Web Design: SVG and Icon Fonts

Icons communicate meaning faster than words. A magnifying glass means search, a heart means favourite, a cart means shopping. Choosing the right method to add SVG icons and icon fonts affects your page speed, accessibility, sharpness on high-DPI screens, and how easy the icons are to style with CSS. This lesson explains every method, compares them, and gives you live code for each.

Focus keyword: SVG icons and icon fonts. Related keywords: inline SVG, SVG sprite, currentColor, viewBox, Font Awesome, Material Symbols, Bootstrap Icons, Lucide icons, Heroicons, Phosphor icons, Iconify, CSS mask-image icons, accessible icons, aria-hidden.

1. Ways to Add Icons to a Web Page

MethodHow it worksCSS colour controlBest for
Inline SVGSVG code written directly in the HTMLFull (fill, stroke, currentColor)A few important icons, animations
SVG sprite with useMany symbols in one SVG, referenced by idGood (via currentColor)Large icon sets reused across pages
img tag with SVG fileIcon loaded as an image fileNone (colour is fixed in the file)Decorative, multicolour illustrations
CSS background or mask-imageIcon used as a CSS imagemask-image allows colour with background-colorPseudo-element icons, list bullets
Icon fontIcons are glyphs in a web fontUses the text color propertyQuick prototypes, legacy projects

2. SVG Basics

SVG (Scalable Vector Graphics) is an XML-based format that describes shapes with math instead of pixels. That means it stays sharp at any size and on any screen density, and files are usually tiny.

  • viewBox='0 0 24 24' defines the internal coordinate system. The icon scales to whatever width and height you set in CSS.
  • fill colours the inside of shapes. stroke colours the outline. Outline icon sets such as Feather and Lucide use fill='none' and a stroke.
  • stroke-width, stroke-linecap and stroke-linejoin control the line style.
  • currentColor makes the icon inherit the text colour, so a single CSS color change restyles both text and icon.

Sizing Icons

Set icon size in em (for example 1.25em) so it scales with the surrounding font size. Use vertical-align: -0.125em or flexbox with align-items: center to line icons up with text.

3. SVG Sprites

A sprite is one hidden SVG containing multiple <symbol> elements. You display an icon with <svg><use href='#icon-id'/></svg>. This avoids repeating long path data and keeps HTML clean. Sprites can be inlined in the page or loaded from an external file, though external sprites have cross-origin restrictions.

4. Icon Fonts

An icon font maps icons to characters in a custom font. Popular examples are Font Awesome, Bootstrap Icons and Material Symbols. You add a stylesheet, then use an element with a class name such as <i class='fa-solid fa-house'></i>. Icons are sized with font-size and coloured with color.

Icon Font Drawbacks

  • The whole font file downloads even if you use only a few icons.
  • If the font fails to load, users may see empty boxes or the wrong character.
  • Icons render as text, so anti-aliasing can look slightly blurry and alignment is finicky.
  • Screen readers may announce the glyph. Add aria-hidden='true' for decorative icons.
  • They are single colour only.

SVG vs Icon Fonts: Which to Choose?

For most modern projects, SVG is the recommended choice: sharper rendering, better accessibility, multicolour support, animation, and tree-shaking (shipping only the icons you use). Icon fonts remain useful for fast prototyping, CMS themes, or when a library offers a convenient class-based system. Material Symbols is a modern variable icon font that lets you adjust fill, weight, grade and optical size with font-variation-settings.

5. Accessibility for Icons

  • Decorative icons (next to visible text): hide them with aria-hidden='true' and focusable='false' on the SVG.
  • Icon-only buttons: give the button an accessible name with aria-label or visually hidden text. Never leave an icon button unlabelled.
  • Meaningful standalone icons: use role='img' and a <title> linked with aria-labelledby.
  • Ensure a 3:1 contrast ratio for meaningful icons against their background.
  • Keep touch targets at least 24 by 24 CSS pixels (44 by 44 is ideal on mobile).
  • Do not communicate status using icon colour alone.

6. CSS mask-image Icons

With mask-image you can use an SVG as a shape and colour it with background-color: currentColor. This gives you the colour control of inline SVG while keeping the icon in CSS, which is perfect for pseudo-elements like ::before and ::after. Include the -webkit-mask prefix for wider browser support.

7. Performance Tips

  • Optimise SVG files with SVGO or SVGOMG to strip metadata and shorten paths.
  • Inline only critical icons, and use a sprite for the rest.
  • Use preconnect for external icon font CDNs, and pin library versions.
  • Prefer subsetted fonts or individual SVGs over loading a full icon font.
  • Use font-display: swap so text is not blocked while an icon font loads.
  • Lucide, Heroicons, Phosphor and Tabler Icons: modern, consistent SVG sets that work well with React, Vue and plain HTML.
  • Iconify: access many icon sets through a single API and on-demand loading.
  • Material Symbols: variable icon font with adjustable weight and fill.
  • currentColor everywhere for easy dark mode and theming.
  • CSS mask-image icons for lightweight, themeable UI details.
  • Animated SVG and micro-interactions with CSS transitions and keyframes, while respecting prefers-reduced-motion.

9. Common Mistakes

  • Forgetting the viewBox, which stops the icon from scaling properly.
  • Using hard-coded fill colours instead of currentColor.
  • Icon-only buttons with no accessible name.
  • Loading an entire icon font for two icons.
  • Not hiding decorative icons from screen readers.
  • Mixing icon sets with different stroke widths and styles.

Summary

Use inline SVG or sprites for crisp, accessible and stylable icons. Use icon fonts when you need a fast, class-based system. Always inherit colour with currentColor, size icons in em, hide decorative icons from assistive technology, and label icon-only controls. Try all the live examples below.

Try it Yourself HTML
Output

Press Run to execute.

Try it Yourself HTML
Output

Press Run to execute.

Try it Yourself HTML
Output

Press Run to execute.

Try it Yourself HTML
Output

Press Run to execute.

Try it Yourself HTML
Output

Press Run to execute.

Try it Yourself HTML
Output

Press Run to execute.

Try it Yourself HTML
Output

Press Run to execute.

Try it Yourself HTML
Output

Press Run to execute.

Exercise: Theme an Inline SVG with currentColorHTML

Replace the hard-coded stroke colour in the SVG with currentColor, set the icon size to 1.5em, and hide it from screen readers. Then make the link text and icon turn orange on hover by changing only the CSS color property of the link.

Try it Yourself HTML
Output

Press Run to execute.

Show expected output
The search icon and the word Search are blue by default and both turn orange on hover, using one color change on the link. The SVG has aria-hidden set to true.

This is a self-check — compare your result with the expected output above.

Exercise: Accessible Icon-Only ButtonHTML

Make the icon-only button accessible. Add an aria-label of Close, hide the SVG from assistive technology with aria-hidden and focusable set to false, give the button a visible :focus-visible outline, and make the button at least 44px by 44px.

Try it Yourself HTML
Output

Press Run to execute.

Show expected output
A 44px by 44px button with a close icon, an aria-label of Close, an aria-hidden SVG, and a clearly visible focus outline when reached by keyboard.

This is a self-check — compare your result with the expected output above.

Quiz: Icons (SVG & Icon Fonts) Quiz Pass 70%
Loading quiz…

Was this page helpful?