Open In App

Create a Like and Dislike Toggle using HTML and CSS

Last Updated : 04 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Creating a Toggle “Like” and “Dislike” button involves using HTML and CSS to structure and style the buttons. We will use the font awesome icon for the like/dislike button.

We will use Font Awesome Icon to create like and dislike buttons with the help of HTML, and CSS to add styles on the button. To Toggle the Like/Dislike button, we will use JavaScript.

Example 1: We create a like/dislike toggle button.

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
  
    <title>
        Toggle Like/Dislike Button with HTML and CSS
    </title>
  
    <!-- Include Font Awesome for icons -->
    <link rel="stylesheet" href=
  
    <style>
        body {
            display: flex;
            align-items: center;
            justify-content: center;
            height: 100vh;
            margin: 0;
        }
  
        .like-btn i {
            font-size: 40px;
            cursor: pointer;
            color: #4CAF50;
            transition: color 0.3s ease;
        }
  
        .like-btn.active i {
            color: #af4c4c;
        }
    </style>
</head>
  
<body>
    <div class="like-btn" onclick="toggleLike()">
        <i class="fas fa-thumbs-up"></i>
    </div>
  
    <script>
        let likeBtn = document.querySelector('.like-btn');
  
        function toggleLike() {
            likeBtn.classList.toggle('active');
  
            // Toggle Font Awesome class for the 
            // thumbs-up and thumbs-down icons
            if (likeBtn.classList.contains('active')) {
                likeBtn.innerHTML = 
                    '<i class="fas fa-thumbs-down"></i>';
            } else {
                likeBtn.innerHTML = 
                    '<i class="fas fa-thumbs-up"></i>';
            }
        }
    </script>
</body>
  
</html>


Output:

like-dislike-toggle-button

Example 2: We create a toggle like/dislike button on a card.

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
  
    <title>Card with Like/Dislike Toggle</title>
      
    <!-- Include Font Awesome for icons -->
    <link rel="stylesheet" href=
      
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            height: 100vh;
            margin: 0;
        }
  
        .card {
            width: 300px;
            border: 1px solid #ccc;
            border-radius: 8px;
            padding: 20px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            margin-bottom: 20px;
            display: flex;
            flex-direction: column;
            align-items: center;
        }
  
        .like-btn {
            cursor: pointer;
            transition: color 0.3s ease;
        }
  
        .like-btn i {
            font-size: 36px;
        }
  
        .like-btn.active i {
            color: #4CAF50;
            /* Green color for active state */
        }
    </style>
</head>
  
<body>
    <div class="card">
        <h2>GeeksforGeeks</h2>
        <p>A computer Science Portal for Geeks</p>
        <div class="like-btn" onclick="toggleLike()">
            <i class="fas fa-thumbs-up"></i>
        </div>
    </div>
  
    <script>
        let likeBtn = document.querySelector('.like-btn');
  
        function toggleLike() {
            likeBtn.classList.toggle('active');
  
            // Toggle Font Awesome class for the
            // thumbs-up and thumbs-down icons
            if (likeBtn.classList.contains('active')) {
                likeBtn.innerHTML = 
                    '<i class="fas fa-thumbs-down"></i>';
            } else {
                likeBtn.innerHTML = 
                    '<i class="fas fa-thumbs-up"></i>';
            }
        }
    </script>
</body>
  
</html>


Output:

like-dislike-toggle



Previous Article
Next Article

Similar Reads

Design a Like and Dislike Toggle Button in Tailwind CSS
The Like and Dislike Toggle in Tailwind CSS is used for gathering user feedback on content, products, or services with better interaction. Creating a like/dislike toggle button with Tailwind CSS, a powerful utility-based framework. It readers through the process of creating customizable toggle buttons, complete with responsive design. Output Previe
2 min read
Why do people dislike using SAP software so much?
SAP software is disliked by some users for several reasons. Its user interface, which hasn't changed much in decades and is thought to be antiquated and challenging to use, is one frequent grievance. Furthermore, a significant problem with the software is its complexity, which makes it difficult for many users to comprehend and utilise. A few custo
10 min read
Create a Mobile Toggle Navigation Menu using HTML, CSS and JavaScript
To create a Mobile Toggle Navigation Menu you need HTML, CSS, and JavaScript. If you want to attach the icons with the menu then you need a font-awesome CDN link. This article is divided into two sections: Creating Structure and Designing Structure. A glimpse of complete Navigation: Creating Structure: In this section, we will create a basic site s
3 min read
Create a Toggle Profile Card Details using HTML CSS and JavaScript
In this article, we will develop an interactive Toggle Profile Card Details application using HTML, CSS, and JavaScript Language. In this application, we have a Card Component with the user's details like their profile picture and Name. There is a toggle button, and when the button is clicked, the detailed information of the user will be displayed
3 min read
How to Create Toggle Switch by using HTML and CSS ?
To create a toggle switch, we will use HTML and CSS. If you want to add a more attractive toggle switch then you can add sliding animation, bouncing effect, etc. In this article, we will divide the whole thing into two different sections structure creating and designing the structure. Steps:Create an HTML file with a title, heading, and a checkbox
3 min read
How to Design Homepage like Facebook using HTML and CSS ?
HTML: HTML stands for Hyper Text Markup Language. It is used to design web pages using a markup language. HTML is the combination of Hypertext and Markup language. Hypertext defines the link between the web pages. A markup language is used to define the text document within tag which defines the structure of web pages. CSS: Cascading Style Sheets,
3 min read
Design a Webpage like Technical Documentation using HTML and CSS
In this article, we will design a webpage-like technical documentation using HTML and CSS . Technical documentation is any document that explains the features of the respective product. In this project, we are going to create technical documentation of C++ by using HTML and CSS. The webpage has a menu section that helps to navigate to different sec
9 min read
Design a Navigation Bar with Toggle Dark Mode in HTML CSS & JavaScript
One can create a navigation bar with a toggle switch for dark mode using HTML, CSS, and JavaScript. If we use the dark mode effect in our website then it can attract user attention and users can use our website in any situation. With the help of JavaScript, we'll implement the functionality to toggle between light and dark modes. [caption width="80
6 min read
How to create slide left and right toggle effect using jQuery?
The task here is to create a slide left and right toggle effect in the JQuery, you can use the jQuery animate() method. .animate() method: It is used to change the CSS property to create the animated effect for the selected element. Syntax: (selector).animate({styles}, para1, para2, para3); Approach: Actual Box width is stored in the variable whose
3 min read
How to Make a Toggle Button using Checkbox and CSS ?
Toggle means switching actions from one state to another. In simple words, when we switch from one state to the other state and back again to the former state we say that we are toggling. In this article, we are using the checkbox and CSS to create a toggle button. In web development, we can use this toggle feature to do various things like switchi
3 min read