Skip to main content
DrupalMaze
Free quote

USWDS icons in Drupal: sprite setup without a CDN dependency

Why not the CDN or the icon font

Government sites often cannot depend on third-party CDNs — uptime commitments, content security policies, and plain procurement rules. And icon fonts are an accessibility liability: empty glyphs, missing aria-hidden, and layout shift when the font loads late. The USWDS SVG sprite approach avoids both problems entirely.

Building the sprite

USWDS ships individual SVGs under dist/img/usa-icons. Do not ship all 240 — concatenate the ones you use into a single sprite with <symbol> wrappers:

<svg xmlns="http://www.w3.org/2000/svg" style="display:none">
  <symbol id="icon-check" viewBox="0 0 24 24">...</symbol>
  <symbol id="icon-warning" viewBox="0 0 24 24">...</symbol>
</svg>

Twenty icons sprite to under 15KB — smaller than the CSS you would have loaded for a font.

Referencing icons in Twig

With the sprite in your theme's templates/ directory, include it once in html.html.twig, then reference symbols anywhere:

<svg class="usa-icon" aria-hidden="true">
  <use href="#icon-warning"></use>
</svg>

Sizes come from usa-icon--size-3 through --size-9 utility classes — no custom CSS needed.

Editor-safe icon selection with SDC

Wrap the pattern in a Single Directory Component with an enum prop: icon: [check, warning, info, arrow]. Editors pick from a list instead of remembering symbol IDs, and the enum keeps invalid values out of the markup. Decorative icons get aria-hidden; meaningful ones get a <title> element inside the symbol.

Cache and CSP notes

Inline sprites ride inside the cached page — zero extra requests, but they add bytes to every response. For sprites over ~30KB, externalize to a file and reference <use href="/themes/custom/yourtheme/icons.svg#icon-x"> — it works in every modern browser, and your CSP stays img-src 'self'.

Componentizing your icon system? Our Drupal services build SDC libraries for government themes.

Bring us your beautifully complicated problem.

↗