Skip to Content

How do I add a Facebook Like button in HTML?

How do I add a Facebook Like button in HTML?

Adding a Facebook Like button to your website is a great way to allow visitors to easily share your content on Facebook. This can help increase traffic and engagement with your site. In this article, we’ll walk through the full process of adding a Facebook Like button in HTML.

What is the Facebook Like Button?

The Facebook Like button is a small plugin provided by Facebook that lets users easily like and share your content on Facebook with one click. When someone clicks the Like button on your site, it will show up in their friends’ News Feeds and drive traffic back to your site.

Some key benefits of adding a Facebook Like button include:

  • Increased traffic and referral visits from Facebook
  • More likes and shares of your content, growing your reach
  • Provides an easy way for visitors to share your content
  • Helps build your brand and community on Facebook

How Does the Facebook Like Button Work?

When a user clicks the Facebook Like button on your site, it triggers an API call to Facebook which logs the action. It will show up on the user’s Facebook profile under their Likes and Activity Log.

It will also post an update into the user’s friends’ News Feeds saying that they liked your website, including a link back to your site. This is how liking and sharing your content enables you to gain more traffic and exposure.

The Facebook Like button also allows users to share your content directly on their Facebook profile or page if they click the share arrow icon next to the button.

Step 1: Get a Facebook App ID

In order to add a Facebook Like button, you first need to create a Facebook app and get an app ID. Here is how to create your Facebook app:

  1. Go to the Facebook Developers site and log in with your Facebook account.
  2. Click “+ Add App” to create a new app.
  3. Select “Consumer” as the app purpose and click “Create App”.
  4. Give your Facebook app a name and contact email.
  5. Go to the Facebook app dashboard and click “Settings” > “Basic”
  6. Scroll down and copy your “App ID” – this is needed to generate the code for your Like button.

Step 2: Generate the Facebook Like Button Code

Once you have your Facebook App ID, you can generate the code for your Like button:

  1. Go to the Facebook Like Button Configurator.
  2. Paste your Facebook App ID into the field.
  3. Customize the options for your Like button – layout, width, etc.
  4. Click “Get Code” to generate the code.
  5. Copy the code provided in the popup.

This will generate the necessary JavaScript code that you’ll paste into your HTML to add the Like button.

Step 3: Paste the Facebook Like Button Code

Now that you have the code for your Facebook Like button, you can paste it into your HTML page wherever you want the button to appear.

Here is an example HTML page with the Facebook Like button code added:

  <html>
  
  <body>
  
    <h1>My Website</h1>
    
    <p>Hello world! This is my website. Please like and share it!</p>
      
    <!-- Facebook Like Button Code -->
    <div id="fb-root"></div>
    <script async defer crossorigin="anonymous" src="https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v3.2"></script>
    
    <div class="fb-like" data-href="https://mywebsite.com" data-width="" data-layout="standard" data-action="like" data-size="small" data-share="true"></div>
    
    <p>Here is some more content...</p>

  </body>
  
</html>  

Paste the code anywhere you want the Like button to render. Typically this would go immediately after or below the content you want visitors to like and share.

Customizing the Facebook Like Button

When generating your Facebook Like button code, there are a number of options you can customize:

  • Layout – Standard or button_count layouts
  • Color scheme – Standard blue or white color schemes
  • Size – Small, medium, or large sized buttons
  • Verb to display – Like, recommend, or share verbs
  • Share button – Display separate share button

Test different button styles and sizes to see which one best fits your site’s design and converts well.

Where to Place the Facebook Like Button

Generally you will want to place your Facebook Like button near the content you want people to share, such as:

  • Next to a blog post title or featured image
  • At the bottom of a blog post or article page
  • Next to your site title or logo in the header
  • Sidebar or widgetized areas
  • On your homepage

Try placing the Like button in different spots across your site to find the placement that works best.

Integrating the Facebook SDK

Notice in the Facebook Like button code there is an inclusion of the Facebook SDK:

  <div id="fb-root"></div>
  
  <script async defer crossorigin="anonymous" src="https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v3.2"></script>

This Facebook JavaScript SDK is necessary for the Like button code to function. Be sure to leave this in place.

The SDK also allows you to integrate other Facebook capabilities like Login, Share, Comments, and more. So it’s useful to keep on your site even if just using the Like button.

Testing the Facebook Like Button

After adding the code for the Facebook Like button, be sure to test it out. Here are some tips for testing:

  • Load your page and confirm the button renders properly
  • Click the Like button and confirm it opens the sharing dialog
  • Share to your own Facebook profile or page and confirm it posts
  • Check that the share contains a link back to your site
  • View your site on mobile browsers and confirm responsiveness

Fix any issues with display or sharing before launching publicly. Also ask friends or social media followers to test liking and sharing your pages.

Facebook Open Graph Meta Tags

To optimize how your content appears when shared on Facebook, you should add some Open Graph meta tags to your page HTML code:


  <head>
  
    <title>Page Title</title>
    
    <!-- Facebook Open Graph meta tags -->
    <meta property="og:title" content="Page Title" />
    <meta property="og:url" content="https://www.yourwebsite.com/page-url" />
    <meta property="og:description" content="A description of the page" />
    <meta property="og:image" content="https://www.yourwebsite.com/image.jpg" />
    
  </head>
  

This configures the title, description, images, and other metadata that displays with your shared content on Facebook.

Tracking Likes and Shares

To view analytics on how well your Facebook Like button is performing, connect your Facebook page to Google Analytics:

  1. In Google Analytics, go to Admin and select the property for your site
  2. Under “Property”, enable data sharing with “Facebook Page”
  3. Enter your Facebook page username and select get ID
  4. Google Analytics will now track interactions with your Facebook Like button

You can view metrics like total likes, shares, clicks, and more under Acquisition > Social > Facebook in your Google Analytics reports.

Optimizing Your Facebook Page

In addition to adding a Facebook Like button, also ensure you have optimized your actual Facebook page. This provides another way to drive traffic from Facebook.

Key tips for optimizing your Facebook page:

  • Complete your page profile and bio sections
  • Add a recognizable profile and cover photo
  • Share engaging posts regularly
  • Promote your page and build your audience
  • Analyze your Facebook Insights to understand your followers

A optimized Facebook presence along with your website’s Facebook Like button can help maximize your traffic and engagement.

Conclusion

Adding a Facebook Like button is an easy way to let your website visitors share your content with their friends on Facebook. This can help grow your traffic, engagement, and brand exposure.

To implement it, first create a Facebook app and get your app ID. Then use the Facebook Like button configurator to generate your code snippet. Paste this into your HTML pages wherever you want the button to appear.

Customize the options to match your site design. Be sure to test sharing on mobile and desktop. And use Open Graph meta tags so links shared on Facebook render optimally.

With your Facebook Like button in place, monitor its effectiveness in Google Analytics. And continue optimizing your Facebook page itself to complement your website’s improved social sharing.