Create Stunning Interfaces with Font Awesome in React (Step-by-Step)

Font Awesome is a library that displays information and messages in symbolic format. We can add an excellent font library and show fonts or symbols in various ways. In React JS, there are two methods to use font awesome.

  • Using an online link with html page
  • Using react.js installation

Using an Online Link with HTML Page

The html page works with the form, element, tags and fonts with links. We can use React’s online link with the font excellent online link.

How to Use Font Awesome

The following steps are used to add font awesome in React.js.

Step 1:

Create an HTML page with the react JS online links in the head section.

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <script src = “https://unpkg.com/react@18/umd/react.development.js” crossorigin></script>  
  5. <script src = “https://unpkg.com/react-dom@18/umd/react-dom.development.js” crossorigin></script>  
  6. <script src = “https://unpkg.com/@babel/standalone/babel.min.js”></script>  
  7. <script src = “https://unpkg.com/react@18/umd/react.production.min.js” crossorigin></script>  
  8. <script src = “https://unpkg.com/react-dom@18/umd/react-dom.production.min.js” crossorigin></script>  
  9. </head>  
  10. </html>  

Step 2:

Use Font Awesome’s online link with the required version in the head section.

  1. <link rel = “stylesheet” href = “https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css”>  

Step 3:

Create a reacts class and show the output element. Use font awsomes link with the <i></i> tags and their size.

  1. Class HelloMessage extends React.Component {  
  2. // function call name and id for message  
  3. render() {  
  4. return <div>  
  5. Hello Student <br/>  
  6. <i class = “fa fa-lg fa-address-book” aria-hidden = “true”></i>  
  7. }  
  8. }  

Step 4:

Shows the output of the font awesome in the container.

  1. root.render(<HelloMessage id=”Javatpoint” name=” website” />);  

Examples:

The following examples show the font is awesome with different styles and UI.

Example 1:

The following examples show the font awesome with basic symbols in the html page.

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <script src = “https://unpkg.com/react@18/umd/react.development.js” crossorigin></script>  
  5. <script src = “https://unpkg.com/react-dom@18/umd/react-dom.development.js” crossorigin></script>  
  6. <script src = “https://unpkg.com/@babel/standalone/babel.min.js”></script>  
  7. <script src = “https://unpkg.com/react@18/umd/react.production.min.js” crossorigin></script>  
  8. <script src = “https://unpkg.com/react-dom@18/umd/react-dom.production.min.js” crossorigin></script>  
  9. <link rel = “stylesheet” href = “https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css”>  
  10. <style>  
  11. #demo1{  
  12. background-color: lightgrey;  
  13. border: 1px solid black;  
  14. width: 370px;  
  15. }  
  16. </style>  
  17. </head>  
  18. <body>  
  19. <div id = “demo1”>  
  20. <b> React Getting Started with id and name </b>  
  21. <div id = “mydiv”></div>  
  22. </div>  
  23. <script type = “text/babel”>  
  24. //Use class for the message  
  25. class HelloMessage extends React.Component {  
  26. // function call name and id for message  
  27. render() {  
  28. return <div>  
  29. Hello Student <br/>  
  30. <i class = “fa fa-address-book” aria-hidden = “true”></i>  
  31.   
  32. Id: {this.props.id} <br/><br/>  
  33. <i class = “fa fa-address-card” aria-hidden = “true”></i>  
  34.   
  35. name:{this.props.name}  
  36. </div>;  
  37. }  
  38. }  
  39. //create react root to show the message  
  40. const container = document.getElementById(‘mydiv’);  
  41. const root = ReactDOM.createRoot(container);  
  42. // Get output and place on the container  
  43. root.render(<HelloMessage id=”Javatpoint” name=” website” />);  
  44. </script>  
  45. </body>  
  46. </html>  

Output:

The image shows the font’s awesome symbols on the web page.

1401 React JS with Font Awesome

Example 2:

The following examples show the font awesome with basic symbols in the html page. We can use font awesome with the form tag.

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <script src = “https://unpkg.com/react@18/umd/react.development.js” crossorigin></script>  
  5. <script src = “https://unpkg.com/react-dom@18/umd/react-dom.development.js” crossorigin></script>  
  6. <script src = “https://unpkg.com/@babel/standalone/babel.min.js”></script>  
  7. <script src = “https://unpkg.com/react@18/umd/react.production.min.js” crossorigin></script>  
  8. <script src = “https://unpkg.com/react-dom@18/umd/react-dom.production.min.js” crossorigin></script>  
  9. <link rel = “stylesheet” href = “https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css”>  
  10. <style>  
  11. #demo1{  
  12. background-color: lightgrey;  
  13. border: 1px solid black;  
  14. width: 370px;  
  15. }  
  16. </style>  
  17. </head>  
  18. <body>  
  19. <div id = “demo1”>  
  20. <b> User Form Using React Js </b>  
  21. <div id = “mydiv”></div>  
  22. </div>  
  23. <script type = “text/babel”>  
  24. //Use class for the message  
  25. class HelloMessage extends React.Component {  
  26. // function call name and id for message  
  27. render() {  
  28. return <form>  
  29. <br/>  
  30. <b> Welcome User </b> <br/> <br/>  
  31. <i class = “fa fa-user-circle” aria-hidden = “true”></i>  
  32.   
  33. User Name: <input type= ‘text’/><br/><br/>  
  34. <i class = “fa fa-envelope” aria-hidden = “true”></i>  
  35.   
  36. User Name: <input type= ’email’/><br/><br/>  
  37. <input type= ‘submit’/><br/>  
  38. </form>;  
  39. }  
  40. }  
  41. //create react root to show the message  
  42. const container = document.getElementById(‘mydiv’);  
  43. const root = ReactDOM.createRoot(container);  
  44. // Get output and place on the container  
  45. root.render(<HelloMessage id=”Javatpoint” name=” website” />);  
  46. </script>  
  47. </body>  
  48. </html>  

Output:

The image shows the font’s awesome symbols on the web page.

1401 React JS with Font Awesome

Example 3:

The following examples show the font awesome with basic symbols in the html page. We can use font awesome with sizes in the table.

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <script src = “https://unpkg.com/react@18/umd/react.development.js” crossorigin></script>  
  5. <script src = “https://unpkg.com/react-dom@18/umd/react-dom.development.js” crossorigin></script>  
  6. <script src = “https://unpkg.com/@babel/standalone/babel.min.js”></script>  
  7. <script src = “https://unpkg.com/react@18/umd/react.production.min.js” crossorigin></script>  
  8. <script src = “https://unpkg.com/react-dom@18/umd/react-dom.production.min.js” crossorigin></script>  
  9. <link rel = “stylesheet” href = “https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css”>  
  10. <style>  
  11. #demo1{  
  12. background-color: lightgrey;  
  13. border: 1px solid black;  
  14. width: 370px;  
  15. }  
  16. </style>  
  17. </head>  
  18. <body>  
  19. <div id = “demo1”>  
  20. <b> User Form Using React Js </b>  
  21. <div id = “mydiv”></div>  
  22. </div>  
  23. <script type = “text/babel”>  
  24. //Use class for the message  
  25. class HelloMessage extends React.Component {  
  26. // function call name and id for message  
  27. render() {  
  28. return <table>  
  29. <tr>  
  30. <th> Sr No </th>  
  31. <th> Name </th>  
  32. <th> Symbol </th>  
  33. </tr>  
  34. <tr>  
  35. <td> 1 </td>  
  36. <td> Home </td>  
  37. <td> <i class = “fa fa-sm fa-home” aria-hidden = “true”></i> </td>  
  38. </tr>  
  39. <tr>  
  40. <td> 2  </td>  
  41. <td> Email </td>  
  42. <td> <i class = “fa fa-md fa-envelope” aria-hidden = “true”></i>  
  43. </td>  
  44. </tr>  
  45. <tr>  
  46. <td> 3  </td>  
  47. <td> User </td>  
  48. <td> <i class = “fa fa-lg fa-user-circle” aria-hidden = “true”></i>  
  49. </td>  
  50. </tr>  
  51. </table>  
  52. }  
  53. }  
  54. //Create react root to show the message  
  55. const container = document.getElementById(‘mydiv’);  
  56. const root = ReactDOM.createRoot(container);  
  57. // Get output and place on the container  
  58. root.render(<HelloMessage id=”Javatpoint” name=” website” />);  
  59. </script>  
  60. </body>  
  61. </html>  

Output:

The image shows the font’s awesome symbols on the web page.

1401 React JS with Font Awesome

Using React.JS Installation

React.js needs to install the font awesome library with the node js package. We need to install the node js package locally and set up the requirement of the react with font awesome. Create a React app and import a file with the symbol package.

How to Use Font Awesome in React.JS

The following steps are used to work with Font Awesome.

Step1: Using font awesome

The font awesome creates React Js components to operate simultaneously. We can import fontawesome icons and required icon packages.

  1. import React from “react”;  
  2. import { render } from “react-dom”;  

Step2: Choosing Icon

We can choose the required icon and import it with the package. Create an awesome library and use it in React JS.

When we know the package and the font, then keep in mind the three-letter abbreviation for that package:

  • Solid Font Style – fas
  • Regular Font Style – far
  • Light Font Style – fal
  • Duotone Font Style – fad

Step 3: Installing Font Awesome library in React JS.

The library or package can be imported through React to support required fonts.

  1. import { FontAwesomeIcon } from “@fortawesome/react-fontawesome”;  

Step 4: create or import font library and import it.

  1. import { faHome } from “@fortawesome/free-solid-svg-icons”;  

Step 5: Use the required icons in the tags.

We can use font style in the following ways and display fonts of the information.

  1. <FontAwesomeIcon icon = {faCode} />  
  2. Or  
  3. <FontAwesomeIcon icon = {[‘fas’, ‘code’]} />  
  4. Or  
  5. <FontAwesomeIcon icon = {[‘fall, ‘code’]} />  

Examples

The following examples show the font awesome with different styles and sizes. We can use react for different types of symbolic fonts and messages.

Example 1:

The following examples show the font awesome with basic symbols and information. Using the import package, we can use two font awesome simultaneously in React JS.

  1. import React from “react”;  
  2. import { render } from “react-dom”;  
  3. import “./styles.css”;  
  4. import { FontAwesomeIcon } from “@fortawesome/react-fontawesome”;  
  5. import { faHome } from “@fortawesome/free-solid-svg-icons”;  
  6. export default function App() {  
  7. return (  
  8. <div className = “App”>  
  9. <div>  
  10. <br/> <br/>  
  11. <FontAwesomeIcon icon = {faHome } />  
  12. Welcome To javatpoint Home  
  13. </div>  
  14. </div>  
  15. );  
  16. }  

Output:

The image shows the font’s awesome symbols on the web page.

1401 React JS with Font Awesome

Example 2:

The following examples show the font awesome with basic symbols and information. Using the import package, we can use two font awesome simultaneously in React JS.

  1. import React from “react”;  
  2. import { render } from “react-dom”;  
  3. import “./styles.css”;  
  4. import { FontAwesomeIcon } from “@fortawesome/react-fontawesome”;  
  5. import { faMugHot, faHome } from “@fortawesome/free-solid-svg-icons”;  
  6. export default function App() {  
  7. return (  
  8. <div className = “App”>  
  9. <div>  
  10. <FontAwesomeIcon icon = {faMugHot} />  
  11. Welcome To React Js!  
  12. <br/> <br/>  
  13. <FontAwesomeIcon icon = {faHome } />  
  14. The javatpoint Home  
  15. </div>  
  16. </div>  
  17. );  
  18. }  

Output:

The image shows the font’s awesome symbols on the web page.

1401 React JS with Font Awesome

Example 3:

The following examples show the font awesome with basic symbols and information. We can use the different sizes 2 or 3 times bigger than normal.

  1. import React from “react”;  
  2. import { render } from “react-dom”;  
  3. import “./styles.css”;  
  4. import { FontAwesomeIcon } from “@fortawesome/react-fontawesome”;  
  5. import { faMugHot, faHome } from “@fortawesome/free-solid-svg-icons”;  
  6. export default function App() {  
  7. return (  
  8. <div className = “App”>  
  9. <div>  
  10. <FontAwesomeIcon icon = {faMugHot} size = {“3x”} />  
  11. Coffee Please!!  
  12. <br/> <br/>  
  13. <FontAwesomeIcon icon = {faHome } size = {“2x”} />  
  14. Welcome To React Js! The javatpoint Home!  
  15. </div>  
  16. </div>  
  17. );  
  18. }  

Output:

The image shows the font’s awesome symbols on the web page.

1401 React JS with Font Awesome

Conclusion

The font Awesome is an essential function to create attractive functions. It symbolically defines function names using React.

Similar Posts