CSS Basics

CSS Icons

Learn different methods to add icons to your web pages: icon fonts, SVG icons, and CSS-only techniques for scalable, accessible visual elements.

What is the best way to add icons to a website?

Use inline SVG icons: they're sharp at any size, can be recoloured with fill: currentColor or stroke: currentColor, are accessible, and load no extra font file. Icon fonts still work but are larger and harder to make accessible. Size icons with width/height in em so they match the surrounding text.

<button>
  <svg class="icon" aria-hidden="true" viewBox="0 0 24 24">…</svg>
  Save
</button>

.icon {
  width: 1.25em; height: 1.25em;
  fill: none; stroke: currentColor; stroke-width: 2;
  vertical-align: -0.2em;
}

Icon Methods Demo

Font Awesome Icons

<i class="fas fa-home"></i>
<i class="far fa-user"></i>
<i class="fab fa-github"></i>

Popular icon font library with thousands of icons

SVG Icons

<svg viewBox="0 0 24 24">
  <path d="M10 20v-6h4v6..." />
</svg>

Scalable vector graphics for crisp, customizable icons

CSS Icon Styling

.icon { font-size: 24px; }
.icon { color: #3b82f6; }
.icon:hover { transform: scale(1.2); }

Style icons like text with CSS properties

Icon Sizes

.icon-sm { font-size: 14px; }
.icon-md { font-size: 18px; }
.icon-lg { font-size: 24px; }
.icon-xl { font-size: 32px; }

Consistent icon sizing across your interface

CSS-Only Icons

.arrow::after {
  content: '';
  border: solid black;
  border-width: 0 3px 3px 0;
}

Create simple icons using pure CSS techniques

Icon Accessibility

<i aria-label="Home" role="img"></i>
<svg aria-labelledby="title">
  <title>Settings</title>
</svg>

Ensure icons are accessible to screen readers

Code Editor

<button class="btn">
  <svg class="icon" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 5v14M5 12h14"/></svg>
  Add item
</button>
<button class="btn danger" aria-label="Delete">
  <svg class="icon" viewBox="0 0 24 24" aria-hidden="true"><path d="M4 7h16M9 7V4h6v3M6 7l1 13h10l1-13"/></svg>
</button>

Preview

Your Learning Path

What You've Learned

  • ▶Icon fonts vs SVG vs CSS-only icons
  • ▶Font Awesome and Material Icons integration
  • ▶SVG icon implementation and styling

What's Next

  • ▶CSS Links - Link styling and states
  • ▶CSS Lists - List styling and customization

Pro Icon Tip

Use SVG icons for the best performance and accessibility. They're scalable, customizable, and don't require external font downloads.

Icon Implementation — step by step

1 / 4
1

Choose icon format

SVG icons are the best choice for the web: they scale without quality loss, can be styled with CSS, and don't require extra HTTP requests when inlined.

1 of 4
💡currentColor Magic
Set fill to currentColor on your SVG icons. This makes them inherit whatever color the parent element has, so you can theme icons simply by changing the text color — no extra icon styles needed.

🎯 Quick Check

What attribute should you add to purely decorative SVG icons for accessibility?

Icons in CSS: summary

Key points

  • Decorative icons should have aria-hidden="true". Icon-only buttons need an aria-label.
  • currentColor makes the icon follow the text colour, including hover states.
  • Libraries like Heroicons, Lucide and Tabler provide free SVG icon sets.

Frequently asked questions

›Should I use SVG icons or an icon font?

SVG icons. They render more crisply, only load the icons you use, can be multicolour, and avoid the accessibility problems of icon fonts.

›How do I change the color of an SVG icon with CSS?

Set fill or stroke to currentColor in the SVG, then change the color of the icon or its parent.