Skip to content

Add Icons with Vue

When adding icon in a project configured with vue-cli, you’ll first create a library of icons, and then you can call them anywhere in your UI.

Set up the library

You’ll first create a library of all the icons you want to use in your project in the src/main.js or src/main.ts file. Here’s an example that adds the Solid style User Secret icon to the library:

/* Set up using Vue 2 */
import Vue from 'vue'
import App from './App'
/* import the fontawesome core */
import { library } from '@fortawesome/fontawesome-svg-core'
/* import font awesome icon component */
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'
/* import specific icons */
import { faUserSecret } from '@fortawesome/free-solid-svg-icons'
/* add icons to the library */
library.add(faUserSecret)
/* add font awesome icon component */
Vue.component('font-awesome-icon', FontAwesomeIcon)
Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
el: '#app',
components: { App },
template: '<App/>'
})

Call the Icons

You can now call the icons, just use the syntax below wherever you want the icons to appear in your project, like in the src/App.vue file. Here’s the rest of the example we started above that adds the fa-user-secret icon into the app UI:

<template>
<div id="app">
<!-- Add the style and icon you want using the Array format -->
<font-awesome-icon :icon="['fas', 'user-secret']" />
</div>
</template>
<script>
export default {
name: 'App'
}
</script>

Several icons in different styles

Here’s an example with a number of icons in different styles, just to give you a sense of how the syntax changes from style to style.

/* Vue 2 and Vue 3 use the same icon importing syntax */
/* add fontawesome core */
import { library } from '@fortawesome/fontawesome-svg-core'
/* add some free styles */
import { faTwitter } from '@fortawesome/free-brands-svg-icons'
import { faUserSecret } from '@fortawesome/free-solid-svg-icons'
/* add some pro styles */
import { faBicycle } from '@fortawesome/pro-regular-svg-icons'
import { faEnvelope } from '@fortawesome/pro-light-svg-icons'
import { faHorseSaddle } from '@fortawesome/pro-duotone-svg-icons'
/* add each imported icon to the library */
library.add(faTwitter, faUserSceret, faBicycle, faCoffee, faHorseSaddle)

Time to add those icons.

<!-- Add Icons using Array format -->
<font-awesome-icon :icon="['fab', 'twitter']" />
<font-awesome-icon :icon="['fas', 'user-secret']" />
<font-awesome-icon :icon="['far', 'bicycle']" />
<font-awesome-icon :icon="['fal', 'envelope']" />
<font-awesome-icon :icon="['fad', 'horse-saddle']" />

Same icon, different styles

You can also import the same icon from different styles with some help from ES import. Import in the main.js file:

/* Vue 2 and Vue 3 use the same icon importing syntax */
import { library } from '@fortawesome/fontawesome-svg-core'
import { faCoffee as fasCoffee } from '@fortawesome/pro-solid-svg-icons'
import { faCoffee as farCoffee } from '@fortawesome/pro-regular-svg-icons'
import { faCoffee as falCoffee } from '@fortawesome/pro-light-svg-icons'
import { faCoffee as fadCoffee } from '@fortawesome/pro-duotone-svg-icons'
library.add(fasCoffee, farCoffee, falCoffee, fadCoffee)

Add the icons to your component.

<!-- Add Icons using Array format -->
<font-awesome-icon :icon="['fas', 'coffee']" />
<font-awesome-icon :icon="['far', 'coffee']" />
<font-awesome-icon :icon="['fal', 'coffee']" />
<font-awesome-icon :icon="['fad', 'coffee']" />

Watch out for self-closing tags in HTML

If you are using inline templates or HTML as templates you need to be careful to avoid self-closing tags. Read more about self-closing tags on Vue.js. If you are writing these types of templates, you’ll need to adjust the syntax to be valid HTML, like this:

<!-- Add Icons using Array format -->
<font-awesome-icon :icon="['far', 'envelope']"></font-awesome-icon>

Add Some Style

Now that you have some icons on the page, add some pieces of flair! Check out all the styling options you can use with Font Awesome and React.