🔥 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 SVG Components: Import SVGs as Components

Astro SVG components explained: import an .svg file and render it inline, override attributes with props, style icons with currentColor, and optimise SVGs with SVGO.

#Astro#Astro SVG#Components#Icons#Images

In Astro, any .svg file you import becomes a component: import Logo from '../assets/logo.svg' and <Logo /> renders the SVG inline in your HTML, with any props you pass merged onto the root <svg> element. There is nothing to install, no client JavaScript, and the same import also exposes the file’s src, width and height if you would rather use it in an <img> tag.

SVG components became stable in Astro 5.7 and are part of every Astro 7 project. This explainer covers how they work, how props are handled, how to style and size icons, accessibility, and the optional SVGO optimiser.

Key Takeaways

  • Importing an .svg gives you a component that renders inline <svg> markup at build or request time, with no JavaScript shipped.
  • Props such as width, height, class and fill override the file’s own attributes on the root element.
  • Use fill="currentColor" or stroke="currentColor" so icons follow the surrounding text colour.
  • The same import doubles as image metadata: Logo.src, Logo.width and Logo.height.
  • experimental.svgOptimizer with svgoOptimizer() (Astro 6.2+) shrinks imported SVGs at build time.

How Astro SVG components work

When Vite resolves an .svg import in an Astro project, Astro reads the file, parses the root <svg> element’s attributes and its inner markup, and wraps both in a component. At render time that component outputs <svg ...attributes>children</svg> directly into your HTML.

Three details follow from that design:

  1. It is inline. The browser does not make a separate request for the file, and the shapes are part of the DOM, so CSS can reach them.
  2. It is static. No JavaScript is shipped; the component exists only on the server, the same as any .astro component without a client directive.
  3. Some attributes are cleaned up. Astro drops xmlns, xmlns:xlink and version from the root element, since they are redundant for inline SVG in HTML.

Using an SVG component

Import the file and use it like any component:

---
// src/components/Header.astro
import Logo from '../assets/logo.svg';
import MenuIcon from '../assets/icons/menu.svg';
---
<header class="site-header">
  <a href="/" aria-label="Home">
    <Logo width={120} height={32} />
  </a>
  <button type="button" aria-label="Open menu">
    <MenuIcon width={24} height={24} aria-hidden="true" />
  </button>
</header>

Props are spread over the SVG’s own attributes, with your props winning. If logo.svg declares width="240" height="64", the width={120} height={32} props replace them. Keep the file’s viewBox in place, because that is what lets the SVG scale cleanly to any size.

You can pass any valid SVG attribute: class, fill, stroke, stroke-width, role, aria-label, aria-hidden, style and data-* attributes all work. Props apply to the root element only; to change a nested path, use CSS.

Styling icons with currentColor

The most useful habit with inline SVG icons is to make them inherit colour from text. If the source files use hard-coded colours, override them on the component:

---
// src/components/IconLink.astro
import Arrow from '../assets/icons/arrow-right.svg';

interface Props {
  href: string;
}
const { href } = Astro.props;
---
<a href={href} class="icon-link">
  <slot />
  <Arrow width={16} height={16} fill="none" stroke="currentColor" stroke-width={2} aria-hidden="true" />
</a>

<style>
  .icon-link {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    color: var(--color-brand);
  }
  .icon-link:hover {
    color: var(--color-text);
  }
</style>

Hover the link and the icon changes colour with the text. For icon sets you control, the cleaner fix is to save the files with currentColor already in them, so no props are needed.

A parent component’s scoped <style> block is scoped to that parent’s own markup, so treat the SVG like any child component. Colour inheritance through currentColor always works, because it flows from the parent element. To style nested paths, give the SVG a class and target it from a global stylesheet or with :global(), for example .icon-link :global(path) { stroke-linecap: round; }. Our Astro styling guide explains how scoping works and when :global() is the right tool.

Accessibility for inline SVG

Inline SVG is invisible to screen readers unless you label it, and noisy if you label decorative icons. Two patterns cover almost every case:

Use Props
Decorative icon next to text aria-hidden="true"
Meaningful graphic on its own (logo link, status icon) role="img" and aria-label="..."
Icon-only button aria-hidden="true" on the SVG, aria-label on the button
---
// src/components/Status.astro
import Check from '../assets/icons/check.svg';
---
<Check role="img" aria-label="Payment received" width={20} height={20} />

Labelling the button rather than the icon, as in the header example above, keeps the accessible name on the interactive element where screen readers expect it.

SVG components vs img vs the Image component

The same import works three ways, and each has a place:

---
// src/pages/about.astro
import { Image } from 'astro:assets';
import Diagram from '../assets/architecture-diagram.svg';
---
<!-- 1. Inline component: styleable, no request -->
<Diagram class="diagram" />

<!-- 2. Plain img: cached separately, not styleable -->
<img src={Diagram.src} width={Diagram.width} height={Diagram.height} alt="Architecture diagram" />

<!-- 3. Image component: adds lazy loading and dimensions -->
<Image src={Diagram} alt="Architecture diagram" />
Approach Best for Trade-off
<Diagram /> component Icons, logos, anything you style or animate Markup repeats on every page that uses it
<img src={Diagram.src}> Large illustrations used on many pages No CSS access to shapes
<Image src={Diagram} /> Consistent attributes with your other images SVGs are not rasterised unless image.dangerouslyProcessSVG is on

Use inline components for small, frequently styled graphics. For a 200 KB illustration that appears site-wide, an <img> lets the browser cache it once. Our Image vs Picture vs img comparison goes deeper on the raster side.

Typing SVG imports

TypeScript knows about .svg imports through Astro’s client types, which astro sync and the default tsconfig.json set up. Each import is typed as a component that accepts SVG attributes and also carries ImageMetadata, so Logo.width autocompletes and a typo in a prop name shows up in your editor before the build.

Optimising SVGs with SVGO

Design tools export SVGs with editor metadata, unused definitions and over-precise coordinates. Astro 6.2 added an experimental build-time optimiser, and Astro ships SVGO for it:

// astro.config.mjs
import { defineConfig, svgoOptimizer } from 'astro/config';

export default defineConfig({
  experimental: {
    svgOptimizer: svgoOptimizer(),
  },
});

With the flag on, every imported SVG is optimised before it becomes a component. svgoOptimizer() accepts an SVGO config object if a default plugin breaks a particular file, for example one that relies on IDs referenced from CSS:

// astro.config.mjs
import { defineConfig, svgoOptimizer } from 'astro/config';

export default defineConfig({
  experimental: {
    svgOptimizer: svgoOptimizer({
      plugins: [
        {
          name: 'preset-default',
          params: { overrides: { cleanupIds: false } },
        },
      ],
    }),
  },
});

If SVGO fails on a file, the build stops with CannotOptimizeSvg (“Cannot optimize SVG.”), naming the file and the optimiser. Fix or re-export that file, or adjust the plugin overrides. As an experimental flag, the option may change in a minor release; check the experimental SVG optimisation docs when upgrading.

Icon sets and the old workarounds

Before SVG components were stable, Astro projects used set:html with a ?raw import, or the community astro-icon integration. Both still work:

---
// src/components/RawIcon.astro
import star from '../assets/icons/star.svg?raw';
---
<span class="icon" set:html={star} />

For a handful of local icons, plain SVG components are now simpler and type-safe. For large icon libraries that you want to reference by name from many packages, an icon integration can still save effort. Starter projects and most Astro themes now ship icons as local .svg files imported as components; the folder structure docs show where src/assets/ fits.

FAQ

How do I use an SVG as a component in Astro?

Import the file from your project, for example import Logo from '../assets/logo.svg', then render <Logo />. Astro inlines the SVG markup into the page. Pass props like width, height or class to override attributes on the root element.

Can I change the colour of an Astro SVG component?

Yes. Pass fill or stroke as props to change the root element, or set them to currentColor so the icon inherits the text colour from CSS. For nested elements, target them with CSS selectors.

Do Astro SVG components ship JavaScript?

No. They render to static inline <svg> markup on the server and ship no client JavaScript, like any Astro component without a client directive.

Should I inline SVGs or use img tags?

Inline small icons and logos you want to style or animate. Use an <img> tag for large illustrations repeated across many pages, so the browser downloads and caches the file once instead of repeating the markup in every page.

How do I make SVGs smaller in Astro?

Enable experimental.svgOptimizer with svgoOptimizer() from astro/config, which runs SVGO on imported SVGs during the build. You can also clean files once with SVGO and commit the optimised versions.