DEV Community

WebCraft Notes
WebCraft Notes

Posted on • Updated on • Originally published at webcraft-notes.com

Easy Way of Using Icons in Nuxt

Easy Way of Using Icons in Nuxt

Read this post in my web notes!

Icons make websites look cool and help users navigate better. If you're into Nuxt.js and want to add icons without the confusion, you're in the right spot! This guide will show you the simple steps to bring icons into your Nuxt projects.

No matter if you're new to coding or a pro with Nuxt, this guide is for you. We'll take it step by step, making it super easy to follow along. Let's jazz up your projects together by learning the simple way to use icons in Nuxt.js. Ready to make your sites look awesome? Let's do this!

If you do not have a Nuxt project then open a terminal and use the following command to create a new one: "npx nuxi@latest init "

Install the dependencies: "npm install"

And now you are ready to start a project in development mode: "npm run dev"

We have a Nuxt starting page and an almost empty project. We can remove tag from App.vue file and move to the next step.

If you have any issue with that please check Nuxt official documentation.

We will add some stiles and "div" container in the middle of the page so that it is easier to check our icons.

Now it's time to open a secret, we will use a "nuxt-icons" module. To install just execute a command: npm i nuxt-icons. Then add "nuxt-icons" to modules in nuxt.config.ts:

export default defineNuxtConfig({
  modules: ['nuxt-icons']
})
Enter fullscreen mode Exit fullscreen mode

That's it, we can use "nuxt-icons" in our app. Usage:

  • create "icons" folder in assets;
  • save all your icons with ".svg" extension in the "icons" folder;
  • use "" tag with the "name" attribute, where the name is the name of your icon from the "icons" folder; Here are some code examples:
<template>
  <div class="container">
    <div class="content">
      <div class="icon">
        <nuxt-icon name="car" filled />
      </div>
      <div class="icon">
        <nuxt-icon name="basketball" filled />
      </div>
      <div class="icon">
        <nuxt-icon name="beer" filled />
      </div>
      <div class="icon">
        <nuxt-icon name="house" filled />
      </div>
      <div class="icon">
        <nuxt-icon name="truck" filled />
      </div>
      <div class="icon">
        <nuxt-icon name="wand" filled />
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'IconsInNuxtApp',
}
</script>
Enter fullscreen mode Exit fullscreen mode

We added six different icons with a "filled" attribute which allows us to use the original color from the SVG file. Here is the result:
result
You can use any style for "nuxt-icons" as for typical "span" wrapped icon.
Incorporating icons into your Nuxt.js projects doesn't have to be complex. With the "nuxt-icons" module, you've unlocked an effortless way to elevate your website's visual appeal and user experience. By following these simple steps, you can seamlessly integrate icons, regardless of your coding expertise. Enhance your projects, captivate your audience, and add that extra touch of style effortlessly with icons in Nuxt.js.

Also check my new articles in my web notes.

Top comments (0)