Open In App

font-awesome npm

Last Updated : 14 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Font Awesome is a popular toolkit offering scalable vector icons and social logos easily customizable with CSS. By installing it via npm (Node Package Manager), you simplify library updates and management. With over 675 icons, Font Awesome provides an extensive suite for web projects.

The icons, CSS, and documentation are open-source, licensed under SIL OFL, MIT, and CC BY 3.0, allowing broad usage and customization. Get started easily by installing it via npm with npm install font-awesome and stay updated with new features and bug fixes.

Steps to Setup Project

Step 1: Setting Up Your Project

Start by creating a new directory for your project. You can do this manually via your file explorer or using the terminal:

mkdir my-awesome-project
cd my-awesome-project

Step 2: Initialize the npm, run the following command:

npm init -y

The package.json file is crucial as it keeps track of your project dependencies, scripts, and configurations.

Step 3: Install Font Awesome

Install Package, To install the Font Awesome, execute:

npm install --save @fortawesome/fontawesome-free

Verify Installation

  • After installation, you should see @fortawesome/fontawesome-free listed under the dependencies section of your package.json file. You can also check your node_modules directory to confirm that Font Awesome files are there.
pic-6
folder structure after installation

Updated Dependencies:

{
  "name": "my-awesome-project",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "dependencies": {
    "@fortawesome/fontawesome-free": "^6.0.0"
  },
  "scripts": {
    "start": "http-server"
  },
  "author": "",
  "license": "ISC"
}

Step 4: Including Font Awesome in Your Project

Initially, install it via npm using command npm install --save @fortawesome/fontawesome-free. Then create an HTML file and link to Font Awesome CSS file within <head> section. You can now use the icons in your HTML by adding <i> tags with appropriate classes such as <i class="fas fa-coffee"></i>.

Example:

HTML
// index.html:
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
"width=device-width, initial-scale=1.0">
    <title>Font Awesome Example</title>
    <link rel="stylesheet" href=
"node_modules/@fortawesome/fontawesome-free/css/all.min.css">
    <style>
        .fas {
            color: #ff5733;
            /* Change icon color */
            font-size: 50px;
            /* Change icon size */
        }

        .fas:hover {
            color: #007bff;
            /* Change color on hover */
        }
    </style>
</head>

<body>
    <h1>Font Awesome Example</h1>
    <p>Here is a coffee icon: <i class="fas fa-coffee"></i></p>
    <p>Here is a heart icon: <i class="fas fa-heart"></i></p>
</body>

</html>


Explore Icon Classes, In the <i> tag:

  • fas: Stands for the Font Awesome Solid which means you are using the solid-style icons.
  • fa-coffee: Specific icon you want to display.

To find more icons visit the Font Awesome Icons Page. You can search for the different categories such as "Solid," "Regular" and "Brands," etc.

Step 5: Serving Your Project

To view your project, you will need the local server. http-server is simple option that you can install globally:

npm install -g http-server

Once the http-server is installed navigate to your project folder and start server:

npm start

Open your web browser and go to the http://localhost:8080. You should see your project rendered with Font Awesome icon displayed.

Alternative Local Server Options, If you prefer not to use the http-server then you can use other options like:

  • Live Server: The Visual Studio Code extension that allows you to serve your project with the live reloading.
  • BrowserSync: The powerful tool that provides synchronized browser testing.

Output:

pic-5

Step 6: Customizing the Icons

You can modify the styles of Font Awesome icons using CSS. Add the <style> section in your <head>:

<style>
    .fas {
        color: #ff5733; /* Change icon color */
        font-size: 50px; /* Change icon size */
    }
</style>

You can also apply hover effects, transitions or the animations to enhance user experience. For example:

.fas {
    transition: color 0.3s ease;
}
.fas:hover {
    color: #007bff; /* Changes color on hover */
}

Step 7: Explore More Icons

Font Awesome offers the vast library of icons including social media logos, user interface symbols and much more. To find and incorporate new icons:

  • Go to Font Awesome Icons Page to browse through the various icons. You can filter the icons based on style (Solid, Regular, Brands) and categories (e.g., web application, transportation).
  • To add new icons simply include corresponding class in your HTML. For example, if you want to add the heart icon:
<i class="fas fa-heart"></i> I love coding!
  • The Font Awesome Pro If you need more icons or the advanced features consider subscribing to Font Awesome Pro. It offers the additional icons, styles and tools.

Step 8: Building and Deploying Your Project

Once your project is ready you can build and deploy it. Here are few common deployment options:

  • GitHub Pages: If your project is on the GitHub then you can use GitHub Pages for free hosting. Simply push your project to the repository, go to Settings and enable the GitHub Pages.
  • Netlify: Netlify is the another great option for deploying static sites. Simply drag and drop your project folder into the Netlify's interface and it will be live in seconds.
  • Vercel: Similar to Netlify, Vercel offers the platform for deploying the web applications easily. Connect your GitHub repository and Vercel will handle deployment for you.

Conclusion

You have successfully set up the Font Awesome in your project using the npm. You can now customize the icons, add new ones and make your web pages visually appealing.

Feel free to experiment with the different icons and styles to enhance your project further. Whether it is the personal project or something professional, the Font Awesome can help you to create more engaging user experience.


Next Article
Article Tags :

Similar Reads