Skip to Content

What is the like button in HTML?

What is the like button in HTML?

The like button in HTML refers to creating a button that allows users to click to “like” or upvote content on a website. This is commonly seen on social media sites and forums where users can click a button to show approval or appreciation for a post, comment, photo, etc. There are a few different ways to create a like button in HTML.

Using the button element

The simplest way is to use an HTML

<button>Like</button>

This will display a basic clickable button with the text “Like”. You can style the button using CSS to change the appearance. Here is an example styled like button:

<button style="background-color: #4267B2; border: none; color: white; padding: 10px 15px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px;">Like</button>

This adds a Facebook-esque blue background color, white text, no borders, padding, center aligned text, and font styling to make it look more like a professional like button.

Using forms

Another way is to use an HTML form with a submit button. This allows you to tie the like button to form processing logic on the backend. For example:

<form action="/like" method="post">
  <button type="submit">Like</button>
</form>

When clicked, this will submit the form to the /like endpoint using an HTTP POST request. You can then have backend code to process the request and update your app’s like counts or store like data in a database.

Using Font Awesome icons

For a more realistic icon, you can use Font Awesome which provides scalable vector icons that can be customized with CSS. To add a thumbs up icon like button:

<button>
  <i class="fas fa-thumbs-up"></i> Like  
</button>

This uses the fa-thumbs-up icon from Font Awesome. You can further customize the styling with CSS:

<button style="border: none; background: transparent;">
  <i class="fas fa-thumbs-up" style="color: #007bff;"></i> Like
</button>  

Now the icon will be blue and the button has no borders or background.

Using images

You can also use an image for the like button icon. For example:

<button>
  <img src="like-icon.png" alt="Like">
</button>

This uses a custom image file like-icon.png as the button icon. You can create your own icons and style them with CSS.

Adding interactivity

To make the button interactive, you need JavaScript code to attach click handlers and update the like counts.

Here is example JavaScript code:

// Get button element
const likeBtn = document.getElementById('like-btn');

// Handle click event
likeBtn.addEventListener('click', function() {

  // Update like count
  let currentLikes = Number(likeBtn.textContent);
  likeBtn.textContent = currentLikes + 1;
  
  // Post request to server (optional)
  fetch('/like-post', { method: 'POST' });

});

This code does the following:

  • Gets a reference to the like button element
  • Attaches a click event listener
  • Increments the like count displayed on the button
  • Sends a POST request to the server to update the database (optional)

To disable multiple likes, you can track if the button has been liked:

let alreadyLiked = false;

likeBtn.addEventListener('click', function() {

  if (!alreadyLiked) {

    // Update like count
    
    alreadyLiked = true;
    
    likeBtn.disabled = true;

  }

});  

This logic lets you disable the button once it’s been clicked to prevent duplicate likes.

Accessibility considerations

When implementing like buttons, consider the following for accessibility:

  • Add aria-label attribute to clearly convey the purpose of the button for screen readers
  • Add title attribute for hover text explaining the action
  • Make sure color contrast meets minimum standards
  • Use semantic HTML elements like

Here is an example accessible like button:

<button 
  aria-label="Like this post"
  title="Click to like this post"
  style="..."
>
  <i class="..."></i>
</button>

Tracking likes

To track how many times a post or piece of content has been liked, you need to store and update like counts in a database. Here are some approaches:

  • Store counts in a separate database table that can be incremented when users click like
  • Embed the like count directly in the content’s database row and increment it there
  • Use a distributed cache like Redis for super fast update of counts
  • Periodically update slower persistence storage from the fast cache

Here is an example table to store like counts:

Post ID Like Count
123 42
456 65

Updating this table in real-time from a web app requires optimized database writes under load.

Displaying like counts

To display live updating like counts, you need to read the current value from the database and render it in your UI. Some options include:

  • Render the count directly in server-side templates
  • Make AJAX requests from the client to API endpoints to get the counts
  • Use web sockets or streaming to push updates to the client

Postgres, MySQL, Redis etc all support fast real-time reads of like counts. Updating counts without page refreshes requires fetching data client-side.

Conclusion

Implementing the like button and counts in HTML requires a combination of HTML, CSS, JavaScript and backend work. The key steps are:

  • Creating the like button UI with HTML/CSS
  • Attaching click handlers with JavaScript
  • Sending data to the server when clicked
  • Updating and querying like counts in a database
  • Displaying the updated counts in real-time

This provides the seamless and snappy like button functionality users expect. Optimizing the backend and database to support large volumes and spikes in traffic allows the like feature to scale.