🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Components & Islands••By Komal Nakrani

Astro Icons: SVG Components, astro-icon and Iconify

Astro icons compared: built-in SVG components, astro-icon with Iconify sets, unplugin-icons, framework icon packages and sprites, with code and performance trade-offs.

#Astro#Astro Icons#SVG#Iconify#Components

The simplest way to use icons in Astro is to import an .svg file and render it as a component: import Star from '../icons/star.svg' then <Star width={20} height={20} />, which inlines the SVG at build time with no JavaScript. For access to thousands of icons by name, astro-icon renders any Iconify set (Material Design Icons, Lucide, Tabler, Heroicons and more) as inline SVG. Avoid icon fonts and client-side icon components on content pages; both add weight that Astro’s static output doesn’t need.

This guide compares the Astro icon options that work well, with setup code for each and advice on which to pick.

Key Takeaways

  • Astro 7 turns imported .svg files into components; props you pass become attributes on the <svg>.
  • astro-icon gives you <Icon name="mdi:home" /> for any Iconify set, rendered to inline SVG at build time.
  • React icon packages ship JavaScript only inside hydrated islands; on static components they render to HTML.
  • Use fill="currentColor" or stroke="currentColor" so icons follow the text colour.
  • Decorative icons need aria-hidden="true"; meaningful icons need an accessible label.

Astro icon options compared

Option Setup Icon source JavaScript Best for
SVG components None Your own SVG files None Small, curated icon sets
astro-icon Integration + icon set packages Local SVGs and Iconify sets None Many icons from popular sets
unplugin-icons Vite plugin Iconify sets None Teams already using it with Vite
Framework icon packages UI framework integration e.g. lucide-react Only in hydrated islands Icons inside React/Vue/Svelte islands
SVG sprite Manual Your own SVGs None Many repeats of the same icons
Icon fonts A stylesheet Font Awesome and similar None, but a font download Legacy projects

Built-in SVG components

Since the SVG component feature became stable, any .svg file you import is a component. Put your icons in src/icons/ or src/assets/icons/:

---
// src/components/FeatureItem.astro
import Check from '../icons/check.svg';
const { text } = Astro.props;
---
<li class="feature">
  <Check width={18} height={18} aria-hidden="true" class="icon" />
  {text}
</li>

<style>
  .feature { display: flex; gap: 0.5rem; align-items: center; }
  .icon { color: var(--accent); flex-shrink: 0; }
</style>

Astro reads the file at build time, keeps its attributes such as viewBox, merges in the props you pass, and writes the <svg> inline. There’s no request per icon and no runtime.

For icons to follow the text colour, the SVG should use currentColor. Many exported icons have hard-coded colours; open the file and replace fill="#000" with fill="currentColor" (or stroke for outline icons), or pass it as a prop:

<Check fill="currentColor" width={18} height={18} />

You can also get the icon’s metadata: the imported value exposes src, width, height and format, so <img src={Check.src} alt="" /> works when you want an image instead of inline markup.

Astro has an experimental svgOptimizer option (added in Astro 6.2) that runs SVGO on imported SVGs. It’s optional; icons from good libraries are usually already optimized.

Where to get SVG files

Most open-source icon sets publish plain SVG files you can copy in: Lucide, Heroicons, Tabler Icons, Phosphor, Feather and Material Symbols. Copy only the ones you use. For a site with 15 or 20 icons, this is the lightest setup there is.

astro-icon with Iconify

When you need more icons or want to switch sets easily, astro-icon is the most common community integration. It renders icons from your own src/icons/ folder and from any Iconify JSON set:

# terminal
npx astro add astro-icon
npm install @iconify-json/lucide @iconify-json/mdi
// astro.config.mjs
import { defineConfig } from 'astro/config';
import icon from 'astro-icon';

export default defineConfig({
  integrations: [icon()],
});
---
// src/components/SocialLinks.astro
import { Icon } from 'astro-icon/components';
---
<nav aria-label="Social">
  <a href="https://github.com/example">
    <Icon name="lucide:github" size={20} aria-hidden="true" />
    <span class="sr-only">GitHub</span>
  </a>
  <a href="/rss.xml">
    <Icon name="mdi:rss" size={20} aria-hidden="true" />
    <span class="sr-only">RSS feed</span>
  </a>
  <Icon name="logo" size={32} />
</nav>

name="logo" loads src/icons/logo.svg; name="lucide:github" loads from the installed Lucide set. Only the icons you use end up in the HTML. When the same icon appears many times on a page, astro-icon can reuse a single <symbol> definition to reduce markup; check its docs for the current options.

Install only the @iconify-json/* packages you need. Each one contains a whole set, which affects install size and build time, not page weight.

unplugin-icons

unplugin-icons imports Iconify icons as components through a virtual module, and it supports Astro as a compile target:

// astro.config.mjs
import { defineConfig } from 'astro/config';
import Icons from 'unplugin-icons/vite';

export default defineConfig({
  vite: {
    plugins: [Icons({ compiler: 'astro' })],
  },
});
---
// src/components/HomeLink.astro
import IconHome from '~icons/lucide/house';
---
<a href="/"><IconHome aria-hidden="true" /> Home</a>

Choose it over astro-icon if your team already uses it in other Vite projects or you want icons imported like modules. For TypeScript, add unplugin-icons/types/astro to your type references.

Icons inside framework islands

Inside a React, Vue or Svelte island, use that framework’s icon package, such as lucide-react, @heroicons/react or lucide-svelte:

// src/components/CopyButton.tsx
import { useState } from 'react';
import { Check, Copy } from 'lucide-react';

export default function CopyButton({ text }: { text: string }) {
  const [copied, setCopied] = useState(false);
  return (
    <button
      type="button"
      onClick={async () => {
        await navigator.clipboard.writeText(text);
        setCopied(true);
      }}
      aria-label={copied ? 'Copied' : 'Copy to clipboard'}
    >
      {copied ? <Check size={16} /> : <Copy size={16} />}
    </button>
  );
}

Import icons individually, as above, so tree-shaking keeps the bundle small. Don’t use these packages in plain .astro files just for static icons: they work, but they require the framework integration for what an SVG import does with none.

SVG sprites

For a site that repeats the same few icons hundreds of times, such as a long table with status icons, a sprite avoids repeating the full path data:

---
// src/components/IconSprite.astro
---
<svg width="0" height="0" style="position:absolute" aria-hidden="true">
  <symbol id="i-check" viewBox="0 0 24 24">
    <path d="M20 6 9 17l-5-5" fill="none" stroke="currentColor" stroke-width="2" />
  </symbol>
  <symbol id="i-x" viewBox="0 0 24 24">
    <path d="M18 6 6 18M6 6l12 12" fill="none" stroke="currentColor" stroke-width="2" />
  </symbol>
</svg>

Include it once in the layout, then reference symbols with <svg width="16" height="16"><use href="#i-check" /></svg>. An external sprite file in public/ can be cached across pages, at the cost of one extra request.

Icon fonts

Font Awesome and similar icon fonts still work: load the stylesheet and use <i class="fa-solid fa-house">. For new Astro sites, prefer SVG. Icon fonts download glyphs for the whole set or need subsetting, can show a blank box or fallback character before the font loads, and are harder to make accessible. Font Awesome also publishes its icons as SVG, which works with either of the first two options.

Accessibility for icons

  • Decorative icons next to text: add aria-hidden="true".
  • Icon-only buttons and links: give the control an accessible name with aria-label or visually hidden text, as in the social links example.
  • Meaningful standalone icons: add role="img" and an aria-label, or a <title> inside the SVG.
  • Size and contrast: icons that carry meaning need the same contrast as text.

Which Astro icon option to choose

  • Fewer than about 30 icons: SVG components. No dependency, no config.
  • Many icons, or you want to browse sets by name: astro-icon.
  • Icons in React, Vue or Svelte islands: that framework’s icon package.
  • The same icon repeated many times: a sprite or astro-icon’s symbol reuse.

Komal themes use inline SVG icons in their .astro components, so swapping a set is a matter of replacing files; browse Astro themes or see the folder structure docs to find where components live. For more on what’s built in, see the best Astro integrations and the official Astro images guide, which documents SVG components.

FAQ

How do I use SVG icons in Astro?

Import the .svg file in the component frontmatter, for example import Star from '../icons/star.svg', and render it with <Star width={20} height={20} />. Astro inlines the SVG at build time and passes your props through as attributes.

What is astro-icon?

astro-icon is a community integration that provides an <Icon /> component. It renders icons from a local src/icons/ folder or from any installed Iconify set, such as @iconify-json/mdi, as inline SVG with no client JavaScript.

Can I use Lucide icons in Astro?

Yes. Copy Lucide’s SVG files and import them as components, use astro-icon with @iconify-json/lucide, or use lucide-react inside React islands.

How do I change the colour of an SVG icon in Astro?

Make the SVG use currentColor for its fill or stroke, then set the CSS color on the icon or its parent. You can also pass fill or stroke as props to an imported SVG component.

Are icon fonts bad for performance?

They’re usually heavier than inline SVG because the browser downloads a font file that can include many unused icons, and the icons may flash or show a fallback while it loads. Inline SVG icons render with the HTML.