Color scheme

SVG Sprites

Icons are one of the most repeated UI elements on any site. A nav bar, a form, a card grid: every one of them may use a dozen icons, and each approach to delivering them has real tradeoffs. Individual SVG files require one HTTP request per icon. Icon fonts are a single file but carry significant accessibility baggage and cannot be styled per-path. Base64-inlined SVGs in CSS bloat your stylesheet and cannot be cached independently.

SVG sprites solve all of those problems: one file, one request, fully cacheable, styleable with CSS, and accessible when authored correctly. You define each icon once using <symbol>, then stamp it anywhere in the document with <use>. This page covers the full workflow: building the sprite, theming with currentColor, understanding the fill vs. stroke authoring difference, and deciding between inline and external sprite delivery.

SVG sprites are one tool in the icon toolkit. If you are curious about building icons entirely from CSS using borders and pseudo-elements instead, that is covered in the Pure CSS Icons choose topic for this unit. For a ready-made library of stroke-based SVG icons you can copy directly into a sprite, Lucide is an excellent free option with a consistent 24x24 viewBox across all icons.

symbol and use

The <symbol> element defines a reusable graphic with its own coordinate system. It is never rendered directly: it lives in a hidden SVG container and exists only as a definition. The <use> element references a symbol by ID and renders a copy of it wherever you place the <use> tag.

The most important attribute on <symbol> is viewBox. Each symbol gets its own viewBox that describes the coordinate space the icon was drawn in, independent of how large it will be rendered. This is what makes icons scale perfectly to any size without distortion.


    <!-- Hidden sprite container: placed once at the top of the body -->
    <svg xmlns="http://www.w3.org/2000/svg" style="display: none;" aria-hidden="true">

        <symbol id="icon-arrow-right" viewBox="0 0 24 24">
            <path d="M5 12h14M13 6l6 6-6 6"/>
        </symbol>

        <symbol id="icon-check" viewBox="0 0 24 24">
            <path d="M5 13l4 4L19 7"/>
        </symbol>

        <symbol id="icon-close" viewBox="0 0 24 24">
            <path d="M18 6L6 18M6 6l12 12"/>
        </symbol>

    </svg>

    <!-- Using icons anywhere in the document -->
    <svg width="24" height="24" aria-hidden="true">
        <use href="#icon-arrow-right"/>
    </svg>

    <svg width="32" height="32" aria-hidden="true">
        <use href="#icon-check"/>
    </svg>
Always Set width and height on the use Wrapper

The <svg> wrapper around <use> controls the rendered size. Without explicit dimensions, the SVG defaults to 300x150 in most browsers. Set width and height in HTML or control them with CSS. Using em units in CSS (width: 1em; height: 1em;) makes icons scale automatically with the surrounding text size.

currentColor and Theming

currentColor is a CSS keyword that resolves to whatever the color property is on the element or its nearest ancestor that sets it. When you use currentColor as the fill or stroke value on an SVG path, the icon inherits the text color of its context automatically. Change the parent's color and the icon changes with it.


    .icon {
        width: 1em;
        height: 1em;
        fill: currentColor;    /* icon inherits text color */
        vertical-align: -0.125em; /* nudge to optical baseline alignment */
    }

    <!-- Icon inherits the button's color -->
    <button class="btn-primary" style="color: white;">
        <svg class="icon" aria-hidden="true"><use href="#icon-arrow-right"/></svg>
        Continue
    </button>

    <!-- Icon inherits the error state color -->
    <p class="error-msg" style="color: #dc2626;">
        <svg class="icon" aria-hidden="true"><use href="#icon-close"/></svg>
        Something went wrong
    </p>

This makes currentColor icons theme-aware by default. In a dark mode context where text flips to near-white, icons flip with it. In a button where hover changes the text color, the icon changes color simultaneously with no extra CSS needed.

Fill vs. Stroke Icons

SVG has two ways to render shape: fill paints the inside of a closed shape, and stroke draws a line along a path. Most icons use one or the other, though some use both. The authoring approach matters because the SVG default is fill: black; stroke: none, which means an icon that relies on stroke will be invisible if you only set fill: currentColor.

Open an icon SVG in a text editor and look at the paths. If the icon is a solid shape (a filled star, a solid home), it uses fill. If the icon looks like it is drawn with a pen (outlined arrow, line-style checkbox), it uses stroke. You need to match your CSS to the icon's authoring:


    /* For fill-based icons */
    .icon-fill {
        fill: currentColor;
        stroke: none;
    }

    /* For stroke-based icons */
    .icon-stroke {
        fill: none;
        stroke: currentColor;
        stroke-width: 2;
        stroke-linecap: round;
        stroke-linejoin: round;
    }
Mixed Icon Libraries

If you are pulling icons from multiple sources, some may be fill-based and others stroke-based. Applying a blanket fill: currentColor rule will make stroke-only icons invisible. Either normalize all icons to one approach when building your sprite, or use a class per type (icon-fill, icon-stroke) and apply the appropriate rule to each.

Inline vs. External Sprite

There are two ways to deliver a sprite to the browser, and the right choice depends on your hosting environment and icon set size.

Inline Sprite

The sprite SVG is pasted directly into the HTML document, typically as the first element inside <body> with display: none. The <use href="#id"> references work immediately because the symbol definitions are in the same document.

  • Works on any host including GitHub Pages and static file servers with no configuration.
  • No CORS or same-origin issues since everything is in one document.
  • The sprite markup is re-downloaded on every page unless the HTML itself is cached. On large icon sets this adds measurable HTML weight.
  • Best for small to medium icon sets (under 30 to 40 icons) on sites without server-side includes or build tools.

External Sprite File

The sprite is a standalone icons.svg file on the server. Each <use> references it with a path and fragment: href="/assets/icons.svg#icon-arrow-right".

  • The sprite file is cached by the browser after the first request, so subsequent pages pay no bandwidth cost for icons.
  • The HTML stays lean regardless of how many icons are in the sprite.
  • Requires the file to be served from the same origin as the page. Cross-origin SVG <use> is blocked by browsers without CORS headers. GitHub Pages serves files from the same origin so it works without configuration.
  • Best for large icon sets or multi-page sites where caching efficiency matters.

Caching Notes for Static Hosting

On static hosts like GitHub Pages, the external sprite file is served with default cache headers. The browser caches it after the first visit. If you update the sprite and push new icons, returning visitors may see the old cached version until the cache expires or they hard-refresh. The standard fix is cache-busting: append a version query string to the sprite URL whenever you change it.


    <!-- Cache-busted external sprite reference -->
    <svg class="icon" aria-hidden="true">
        <use href="/assets/icons.svg?v=2#icon-arrow-right"/>
    </svg>

For a Practice Site on GitHub Pages, the inline approach is simpler and avoids the caching question entirely. Use the external file approach if your site grows to the point where repeating the sprite markup across pages becomes a maintenance problem.

Sprites in Action

The inline sprite below defines five icons. Each one is referenced with <use> and styled via currentColor. Four icons are stroke-based and one is fill-based, so each wrapper uses the matching class from the section above. The color and size controls show how the same sprite adapts to different contexts with no changes to the SVG markup.

arrow-right
check
close
star
user

Check Your Understanding

Put your new skills to the test with this coding challenge. You will grab two real icons from Lucide, turn them into a sprite, and wire them up with currentColor. Use the copy icon to copy your completed code and paste it as a prompt to your AI assistant for feedback.


        <!--
            Step 1: Go to https://lucide.dev/icons/ and pick any two icons.
            Click an icon, then click "Copy SVG" to get its markup.

            Step 2: Build an inline sprite by wrapping each icon's path(s)
            inside a symbol element with a matching viewBox="0 0 24 24".
            The sprite container must be hidden and aria-hidden.

            Step 3: Use each icon twice below the sprite:
            once at 24px and once at 48px, using different colors
            via the parent element's color property.

            Step 4: Write the CSS. Lucide icons are stroke-based,
            so target stroke: currentColor, not fill.

            Write both the HTML and CSS here.
        -->

    

Resources

  • MDN: <symbol> — Element reference including viewBox, width, height, and rendering behavior
  • MDN: <use> — Reference for href, shadow DOM behavior, and cross-origin restrictions
  • MDN: currentColor — How the keyword resolves and inherits through the element tree
  • Lucide Icons — Free, open source stroke-based icon library with consistent 24x24 viewBox; click any icon and use "Copy SVG" to grab the markup for your sprite
  • MDN: stroke — Stroke properties including stroke-width, stroke-linecap, and stroke-linejoin