Home » How to Create Toggle Button in HTML CSS & JavaScript

How to Create Toggle Button in HTML CSS & JavaScript

Toggle Button

Toggle buttons are a simple and effective way to add interactivity to your website. They are commonly used for tasks such as switching themes, turning features on or off, or controlling settings. In this article, we’ll show you how to create a toggle button using HTML, CSS, and JavaScript.

Key Benefits of a Toggle Button

  • User-Friendly: Easy for users to understand and use.
  • Space Saving: Replaces multiple buttons with a single switch.
  • Customizable: Can be styled to match the theme of your website.

Let’s dive into the code and learn how to create a toggle button from scratch.

Step 1: HTML Structure

The first step is to create the HTML structure for the toggle button. Here’s a basic example:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Toggle Button</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="toggle-container">
        <input type="checkbox" id="toggle-button" class="toggle-button">
        <label for="toggle-button" class="toggle-label"></label>
    </div>
    <script src="script.js"></script>
</body>
</html>

Explanation:

  1. input[type=checkbox]: Acts as the core of the toggle functionality.
  2. label: Used to style and display the toggle button.

Step 2: Styling with CSS

Next, we style the toggle button to make it look appealing.

/* styles.css */
body {
    font-family: Arial, sans-serif;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    margin: 0;
    background-color: #f0f0f0;
}

.toggle-container {
    position: relative;
    width: 60px;
    height: 30px;
}

.toggle-button {
    display: none;
}

.toggle-label {
    position: absolute;
    width: 100%;
    height: 100%;
    background-color: #ccc;
    border-radius: 50px;
    cursor: pointer;
    transition: background-color 0.3s ease;
}

.toggle-label::after {
    content: '';
    position: absolute;
    width: 26px;
    height: 26px;
    background-color: white;
    border-radius: 50%;
    top: 2px;
    left: 2px;
    transition: transform 0.3s ease;
}

.toggle-button:checked + .toggle-label {
    background-color: #4caf50;
}

.toggle-button:checked + .toggle-label::after {
    transform: translateX(30px);
}

Explanation:

  1. toggle-button: Hidden checkbox for the toggle functionality.
  2. toggle-label: Styled to look like a switch.
  3. ::after: Creates the circular knob of the switch.
  4. Transitions: Smooth animations for the toggle.

Step 3: Adding Interactivity with JavaScript

Now let’s add JavaScript to make the toggle button functional.

// script.js
document.getElementById('toggle-button').addEventListener('change', function () {
    if (this.checked) {
        document.body.style.backgroundColor = '#4caf50';
    } else {
        document.body.style.backgroundColor = '#f0f0f0';
    }
});

Explanation:

  1. Event Listener: Listens for the change event on the checkbox.
  2. Condition: Changes the background color of the page based on the toggle state.

Creating a toggle button using HTML, CSS, and JavaScript is straightforward and highly customizable. With just a few lines of code, you can add an interactive and visually appealing element to your website.

Key Takeaways:

  • Use HTML for structure.
  • Use CSS for styling.
  • Use JavaScript for interactivity.

Experiment with the design and functionality to create a toggle button that suits your project needs.