How do I add icons to buttons?

I have read about using some software called font awesome to add icons to things in HTML but I am not entirely sure how to implement this in my case. I have made a basic page with buttons but want to add the relevant icon to each button. Here is my code so far:

    body {
      font-family: Arial, sans-serif;
      background-color: #f5f5f5;
      margin: 0;
      padding: 0;
    }

    .container {
      max-width: 600px;
      margin: 0 auto;
      padding: 20px;
      background-color: #fff;
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
      text-align: center;
    }

    h1 {
      color: #333;
    }

    .social-buttons {
      margin-top: 20px;
    }

    .social-buttons button {
      display: inline-block;
      padding: 10px 20px;
      margin: 10px;
      background-color: #007bff;
      color: #fff;
      font-size: 16px;
      border: none;
      border-radius: 4px;
      cursor: pointer;
      transition: background-color 0.3s ease;
    }

    .social-buttons button:hover {
      background-color: #0056b3;
    }
<!DOCTYPE html>
<html>

  <head>
    <title>Social Media Buttons</title>
  </head>

  <body>
    <div class="container">
      <h1>Social Media Buttons</h1>
      <div class="social-buttons">
        <button onclick="window.location.href='https://www.facebook.com'">Facebook</button>
        <button onclick="window.location.href='https://www.twitter.com'">Twitter</button>
        <button onclick="window.location.href='https://www.instagram.com'">Instagram</button>
        <button onclick="window.location.href='https://www.linkedin.com'">LinkedIn</button>
        <button onclick="window.location.href='https://www.youtube.com'">YouTube</button>
      </div>
    </div>
  </body>

</html>

Please advise on how I add the icons.

>Solution :

Yes font awesome can be used to add all sorts of icons, to implement you can add the following to your code. A further tutorial can be found here: https://fontawesome.com/docs/web/add-icons/how-to

Implement the library:

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">

Then change the code of each button as follows (facebook for example):

<button onclick="window.location.href='https://www.facebook.com'">
        <i class="fab fa-facebook-f"></i> Facebook

Full code below:

<!DOCTYPE html>
<html>
<head>
<title>Social Media Buttons</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
<style>
    body {
    font-family: Arial, sans-serif;
    background-color: #f5f5f5;
    margin: 0;
    padding: 0;
    }

    .container {
    max-width: 600px;
    margin: 0 auto;
    padding: 20px;
    background-color: #fff;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    text-align: center;
    }

    h1 {
    color: #333;
    }

    .social-buttons {
    margin-top: 20px;
    }

    .social-buttons button {
    display: inline-block;
    padding: 10px 20px;
    margin: 10px;
    background-color: #007bff;
    color: #fff;
    font-size: 16px;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    transition: background-color 0.3s ease;
    }

    .social-buttons button:hover {
    background-color: #0056b3;
    }
</style>
</head>
<body>
<div class="container">
    <h1>Social Media Buttons</h1>
    <div class="social-buttons">
    <button onclick="window.location.href='https://www.facebook.com'">
        <i class="fab fa-facebook-f"></i> Facebook
    </button>
    <button onclick="window.location.href='https://www.twitter.com'">
        <i class="fab fa-twitter"></i> Twitter
    </button>
    <button onclick="window.location.href='https://www.instagram.com'">
        <i class="fab fa-instagram"></i> Instagram
    </button>
    <button onclick="window.location.href='https://www.linkedin.com'">
        <i class="fab fa-linkedin-in"></i> LinkedIn
    </button>
    <button onclick="window.location.href='https://www.youtube.com'">
        <i class="fab fa-youtube"></i> YouTube
    </button>
    </div>
</div>
</body>
</html>

Leave a Reply