Install Dependencies

npm
npm install @fortawesome/free-solid-svg-icons

svelte-fa is the recommended package for Svelte integration

Usage

How to use FontAwesome icons in Svelte components

<script> import { faHome, faUser, faGear, faSpinner } from '@fortawesome/free-solid-svg-icons'; </script> <div> <i class="fa-solid fa-house"></i> <i class="fa-solid fa-user" style="font-size: 2em;"></i> <i class="fa-solid fa-gear fa-spin"></i> </div> Works great with Tailwind CSS for styling: <div class="text-blue-500 text-2xl"> <i class="fa-solid fa-house"></i> </div>

Component API

The svelte-fa component supports various props for customization

&lt;!-- FaIcon.svelte --&gt; <script> export let icon = 'house'; export let size = '1x'; export let spin = false; export let color = ''; $: cls = `fa-solid fa-${icon} ${size !== '1x' ? 'fa-' + size : ''} ${spin ? 'fa-spin' : ''}`; </script> <i class="{cls}" style="{color ? 'color:' + color : ''}"></i> Basic example of using an icon in Svelte: <FaIcon icon="house" /> <FaIcon icon="spinner" spin /> <FaIcon icon="heart" size="2x" color="red" />

Best Practices

On-demand Import

Import individual icons to reduce bundle size through tree-shaking

SSR Compatibility

FontAwesome works with SvelteKit server-side rendering out of the box

Performance

Use CSS animations instead of JavaScript for better performance

Share

Related