SVG Symbol Sprite

suggest change

An SVG symbol sprite stores reusable vector shapes in one document:

<svg class="svg-sprite" aria-hidden="true" width="0" height="0">
  <symbol id="icon-star" viewBox="0 0 24 24">
    <path d="M12 2l3 6 7 .9-5 4.8 1.3 6.8L12 17l-6.3 3.5L7 13.7 2 8.9 9 8z"></path>
  </symbol>
  <symbol id="icon-search" viewBox="0 0 24 24">
    <path d="M10 3a7 7 0 1 0 4.9 12l5.1 5 1-1-5-5.1A7 7 0 0 0 10 3zm0 2a5 5 0 1 1 0 10 5 5 0 0 1 0-10z"></path>
  </symbol>
</svg>

<button type="button">
  <svg class="icon" aria-hidden="true">
    <use href="#icon-search"></use>
  </svg>
  <span>Search</span>
</button>
.icon {
  inline-size: 1.25em;
  block-size: 1.25em;
  fill: currentColor;
}

.svg-sprite {
  position: absolute;
  overflow: hidden;
}

button {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
}

The visible text gives the button its accessible name, so the repeated decorative SVG is marked aria-hidden. Symbols may also live in an external SVG file when the deployment and browser policy support external use references.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents