Sling Academy
Home/Next.js/How to use Font Awesome with Next.js

How to use Font Awesome with Next.js

Last updated: January 08, 2023

Font Awesome is a font and icon toolkit based on CSS and Less. It is used by millions of designers, developers, and content creators. Font Awesome provides over a thousand free icons, and they can be resized and colored to suit your needs.

The example below shows you how to use Font Awesome 6 (the latest version) with Next.js.

1. Create a new Next.js project, then install the required packages:

npm install @fortawesome/fontawesome-svg-core @fortawesome/free-solid-svg-icons @fortawesome/react-fontawesome

2. Edit your page/_app.js file (create one if it doesn’t exist):

// import Font Awesome CSS
import "@fortawesome/fontawesome-svg-core/styles.css"; 

import { config } from "@fortawesome/fontawesome-svg-core";
// Tell Font Awesome to skip adding the CSS automatically 
// since it's already imported above
config.autoAddCss = false; 

const App = ({ Component, pageProps }) => {
  return <Component {...pageProps} />;
};

export default App;

Note that you can see all available icons in the following file:

/node_modules/@fortawesome/free-solid-svg-icons/index.d.ts

3. Now you can add and style (font size, color, etc.) a Font Awesome icon by using the <FontAwesomeIcon/> component like this:

<FontAwesomeIcon
    icon={faAmbulance}
    style={{ fontSize: 100, color: "orange" }}
/>

Full example:

// pages/index.js

// Import the FontAwesomeIcon component
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";

// import the icons you need
import {
  faSearch,
  faAmbulance,
  faAnchor,
} from "@fortawesome/free-solid-svg-icons";

const Home = (props) => {
  return (
    <div>
      <FontAwesomeIcon
        icon={faSearch}
        style={{ fontSize: 100, color: "blue" }}
      />

      <FontAwesomeIcon
        icon={faAmbulance}
        style={{ fontSize: 100, color: "orange" }}
      />

      <FontAwesomeIcon
        icon={faAnchor}
        style={{ fontSize: 100, color: "green" }}
      />
    </div>
  );
};

export default Home;

Run your project (restart if it’s already running) and see the result:

That’s it, my friend. From here, you’re pretty good to go and ready to build big things.

Next Article: How to use Ant Design in a Next.js project

Previous Article: How to use SASS in Next.js

Series: Working with CSS in Next.js

Next.js

Related Articles

You May Also Like

  • Solving Next.js Image Component Error with CSS Classes
  • How to Use MUI (Material UI) in Next.js 14
  • Fixing Data Update Issues in Next.js Production
  • Fixing Next.js Undefined ENV Variables in Production Build
  • Solving Next.js SyntaxError: Unexpected token ‘export’
  • Next.js Error: Found Page Without a React Component as Default Export – How to Fix
  • Fixing Next.js Error: Blank Page on Production Build
  • Fixing Next.js Error when Importing SVGs: A Step-by-Step Guide
  • Next.js Issue: useEffect Hook Running Twice in Client
  • Fixing ‘self’ is not defined error in Next.js when importing client-side libraries
  • How to Dockerize a Next.js App for Production
  • How to set up Next.js with Docker Composer and Nginx
  • Solving Next.js CORS Issues: A Comprehensive Guide
  • Fixing Next.js Hydration Mismatch Error in Simple Ways
  • Next.js Error: Cannot use import statement outside a module
  • Next.js: How to Access Files in the Public Folder
  • Fixing Next.js Import CSS Error: A Simple Guide
  • Fixing LocalStorage Not Defined Error in Next.js
  • Fixing Next.js Error: No HTTP Methods Exported