SVGスプライトシステムの構築:効率的なアイコン管理
SVGスプライトシステムで大規模アプリケーションのアイコンを効率管理。スプライト生成、使用パターン、パフォーマンス最適化を解説。
Founder of VectoSolve
Self-taught developer, founder and sole developer of VectoSolve since 2024. He builds the conversion engine and the cutting, embroidery and 3D printing exports.
How this article was made: drafted with AI assistance, then edited and fact-checked before publication. About the author
Key Takeaways
- SVGスプライトは複数のアイコンを1つのHTTPリクエストにまとめ、ネットワーク負荷を大幅に削減します
-
<symbol>+<use>パターンでアイコンを一度定義し、ページ内のどこからでも参照できます - 外部スプライトは大規模アプリに最適(キャッシュ可能)、インラインスプライトは小規模サイトに最適(遅延ゼロ)
- CSSカスタムプロパティでサイズ、カラーテーマ、ホバー状態を統一的にスタイリング
-
装飾アイコンには
aria-hidden="true"を、意味のあるアイコンにはrole="img"と<title>を追加
なぜSVGスプライトなのか?
個別のSVGファイルはそれぞれ別のHTTPリクエストが必要です。スプライトシステムはアイコンを1つのファイルにまとめ、パフォーマンスを向上させ管理を簡素化します。
スプライトシステムのメリット
パフォーマンス
個別アイコン:
50アイコン x 1リクエスト = 50リクエスト
SVGスプライト:
50アイコン = 1リクエスト
管理性
メリット:
- 唯一の信頼できる情報源
- 簡単な更新
- 一貫したスタイリング
- バージョン管理に最適
スプライトの種類
外部スプライト
別ファイルとして参照:
<!-- sprites.svg -->
<svg xmlns="http://www.w3.org/2000/svg">
<symbol id="icon-home" viewBox="0 0 24 24">
<path d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z"/>
</symbol>
<symbol id="icon-search" viewBox="0 0 24 24">
<path d="M15.5 14h-.79l-.28-.27..."/>
</symbol>
</svg>
<!-- 使用方法 -->
<svg class="icon">
<use href="/sprites.svg#icon-home"/>
</svg>
インラインスプライト
HTMLに埋め込み:
<body>
<!-- 非表示のスプライトコンテナ -->
<svg style="display: none;">
<symbol id="icon-home" viewBox="0 0 24 24">
<path d="..."/>
</symbol>
<!-- その他のシンボル -->
</svg>
<!-- ページ内のどこでも使用可能 -->
<svg class="icon">
<use href="#icon-home"/>
</svg>
</body>
CSS背景スプライト
Data URIアプローチ:
.icon-home {
background-image: url("data:image/svg+xml,...");
}
Pro Tip: ファーストビューのアイコンには即座にレンダリングされるインラインスプライトを、大規模なアイコンセットにはページ遷移間でブラウザキャッシュの恩恵を受ける外部スプライトを使いましょう。
A Full Year of Conversions for $49
Starter Annual: 25 conversions every month, $49 for the whole year. That is about $4.08 a month · Cancel anytime
Get the Annual Dealスプライトの作成
手動作成
構造:
<svg xmlns="http://www.w3.org/2000/svg">
<defs>
<!-- 共有スタイル -->
<style>
.icon-stroke { stroke-width: 2; fill: none; }
</style>
</defs>
<symbol id="icon-1" viewBox="0 0 24 24">
<!-- アイコンパス -->
</symbol>
<symbol id="icon-2" viewBox="0 0 24 24">
<!-- アイコンパス -->
</symbol>
</svg>
自動生成
svg-spriteを使用:
// gulpfile.js
const svgSprite = require('gulp-svg-sprite');
gulp.task('sprite', () => {
return gulp.src('icons/*.svg')
.pipe(svgSprite({
mode: {
symbol: {
dest: '.',
sprite: 'sprites.svg'
}
}
}))
.pipe(gulp.dest('dist'));
});
Webpack統合
// webpack.config.js
const SpriteLoaderPlugin = require('svg-sprite-loader/plugin');
module.exports = {
module: {
rules: [{
test: /\.svg$/,
use: [{
loader: 'svg-sprite-loader',
options: { extract: true }
}]
}]
},
plugins: [new SpriteLoaderPlugin()]
};
使用パターン
基本的な使い方
<svg class="icon icon-home" aria-hidden="true">
<use href="#icon-home"/>
</svg>
Reactコンポーネント
function Icon({ name, size = 24, className }) {
return (
<svg
className={`icon ${className}`}
width={size}
height={size}
aria-hidden="true"
>
<use href={`#icon-${name}`}/>
</svg>
);
}
// 使用例
<Icon name="home" size={20} className="nav-icon"/>
Vueコンポーネント
<template>
<svg :class="['icon', className]" :width="size" :height="size">
<use :href="`#icon-${name}`"/>
</svg>
</template>
<script>
export default {
props: {
name: String,
size: { type: Number, default: 24 },
className: String
}
}
</script>
"優れたSVGスプライトシステムは、あらゆるスケーラブルなデザインシステムの基盤です。すべてのページ、すべてのコンポーネントに関わるアセットです。
スプライトのスタイリング
CSSスタイリング
.icon {
width: 24px;
height: 24px;
fill: currentColor;
}
.icon-primary {
fill: var(--color-primary);
}
.icon-lg {
width: 32px;
height: 32px;
}
CSSカスタムプロパティ
:root {
--icon-size: 24px;
--icon-color: currentColor;
}
.icon {
width: var(--icon-size);
height: var(--icon-size);
fill: var(--icon-color);
}
.icon-lg { --icon-size: 32px; }
.icon-danger { --icon-color: #dc3545; }
ストロークアイコン
.icon-stroke {
fill: none;
stroke: currentColor;
stroke-width: 2;
stroke-linecap: round;
stroke-linejoin: round;
}
Free SVG Tools for Crafters
Design Checker · Color Separator · Mat Preview · Offset Generator · Text to SVG
Check Your SVG Freeアクセシビリティ
装飾アイコン
<button>
<svg class="icon" aria-hidden="true">
<use href="#icon-settings"/>
</svg>
設定
</button>
意味のあるアイコン
<a href="/home">
<svg class="icon" role="img" aria-label="ホーム">
<title>ホーム</title>
<use href="#icon-home"/>
</svg>
</a>
パフォーマンス最適化
キャッシュ戦略
<!-- 外部スプライトをキャッシュ -->
<link rel="preload" href="/sprites.svg" as="image" type="image/svg+xml">
コード分割
// スプライトをオンデマンドで読み込み
async function loadSprite() {
const response = await fetch('/sprites.svg');
const svg = await response.text();
document.body.insertAdjacentHTML('afterbegin', svg);
}
スプライトのサブセット化
セクション別に小さなスプライトを作成:
/sprites
/navigation-icons.svg
/social-icons.svg
/editor-icons.svg
100以上のアイコンを持つアプリケーションでは、ページセクションや機能エリアごとにスプライトを分割しましょう。ナビゲーションアイコンだけが必要なページでエディタアイコンを読み込むことを避け、初期ペイロードを小さく保てます。
大規模アイコンセットの管理
構成
/icons
/navigation
home.svg
menu.svg
/actions
edit.svg
delete.svg
/social
twitter.svg
facebook.svg
ドキュメント
アイコンリファレンスを生成:
<div class="icon-grid">
<div class="icon-item">
<svg><use href="#icon-home"/></svg>
<span>home</span>
</div>
<!-- ... -->
</div>
A Full Year of Conversions for $49
Starter Annual: 25 conversions every month, $49 for the whole year. That is about $4.08 a month · Cancel anytime
Get the Annual DealVectoSolveでアイコンセットを作成
アイコンの変換
VectoSolveでアイコンを作成:
- ソース画像を変換
- クリーンアップと最適化
- スプライトにまとめる
- デプロイ
一貫した出力
VectoSolveの特長:
- クリーンなパス
- 一貫したviewBox
- 最適化された出力
- スプライト対応
まとめ
SVGスプライトシステムはアイコン管理とパフォーマンスを劇的に改善します。プロジェクトに適したアプローチを選びましょう——小規模セットにはインライン、大規模アプリケーションには外部スプライト。VectoSolveでクリーンで一貫したアイコンを作成し、スプライトに統合しましょう。