Responsive To-Do List using HTML, CSS and JavaScript

In this blog, we'll guide you through the process of creating a dynamic to-do list application using HTML, CSS, and JavaScript. Whether you're a coding novice or looking to refine your web development skills, this step-by-step tutorial will empower you to build a functional and visually appealing to-do list from scratch.

Watch on YouTube

HTML

                    
content_copy copy
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>To-do List</title> <link rel="stylesheet" href="../css/style.css"> <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@20,400,0,0" /> </head> <body> <div class="heading"> <h1>To Do List</h1> </div> <div class="add"> <textarea id="add-text" name="" placeholder="Add ToDo"></textarea> <button id="add-btn">Add</button> </div> <ul class="list"> </ul> </body> <script src="/js/script.js"></script> </html>

CSS

                    
content_copy copy
*{ margin: 0; padding: 0; box-sizing: border-box; } :root{ --color1: #222433; --color2: #b8a2c9; --color3: #4c3b71; --color4: #c5c3c4; } .heading{ height: 80px; display: flex; align-items: center; justify-content: center; color: var(--color4); } h1{ font-size: 40px; text-align: center; } .add{ margin-top: 20px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px; border-bottom: 1px solid var(--color2); padding-bottom: 20px; } .add textarea{ width: 80%; padding: 10px; font-size: 16px; border: 3px solid var(--color2); border-radius: 8px; outline: none; background-color: var(--color3); color: white; } .add button{ width: 100px; height: 30px; font-size: 16px; outline: none; border-radius: 5px; background-color: var(--color4); } .list{ list-style: none; margin: 30px 0; } .list-item{ width: 90%; height: 70px; border: 3px solid var(--color2); border-radius: 5px; margin: 20px auto; display: flex; align-items: center; gap: 5px; padding: 0 5px; } .list-item .checkbox{ width: 20px; height: 20px; border: 1px solid var(--color2); } .list-item textarea{ height: 40px; border: 2px solid var(--color2); border-radius: 5px; font-size: 16px; flex-grow: 1; background-color: var(--color3); color: white; } .edit, .delete{ color: var(--color4); } .edit:hover, .delete:hover, .checkbox:hover, #add-btn:hover{ cursor: pointer; } body{ background-color: var(--color1); } @media(min-width: 425px){ .heading{ height: 100px; } .heading h1{ font-size: 45px; } .add{ margin-top: 30px; gap: 20px; padding-bottom: 30px; } .add textarea{ width: 70%; font-size: 18px; } .add button{ height: 35px; font-size: 18px; } .list-item{ width: 80%; height: 80px; margin: 30px auto; padding: 0 10px; } .list-item textarea{ height: 50px; font-size: 18px; } } @media(min-width: 768px){ .add{ flex-direction: row; } .add textarea{ width: 50%; font-size: 20px; } .list-item{ width: 70%; height: 90px; margin: 40px auto; } .list-item textarea{ height: 60px; font-size: 20px; } }

JavaScript

                    
content_copy copy
// Get references to DOM elements let addBtn = document.getElementById("add-btn"); // Button to add a new to-do item let addText = document.getElementById("add-text"); // Input field for the to-do text let list = document.getElementsByClassName("list")[0]; // The unordered list to display to-do items let listItems = document.querySelectorAll(".list-item"); // Initially selected list items // Create action item when the 'add' button is clicked addBtn.addEventListener("click", function() { // Create a new list item element let listItem = document.createElement("li"); listItem.className = 'list-item'; // Set class for styling // Populate the list item with a checkbox, textarea, and action buttons listItem.innerHTML = ` <input type="checkbox" class="checkbox"> <!-- Checkbox to mark completion --> <textarea name="" id="" disabled>${addText.value}</textarea> <!-- Text area for task description --> <span class="material-symbols-outlined edit">edit</span> <!-- Edit button --> <span class="material-symbols-outlined delete">delete</span> <!-- Delete button --> `; // Append the new list item to the list list.appendChild(listItem); attachEventListeners(listItem); // Attach event listeners to the new list item }); // Attach event listeners to existing list items listItems.forEach(listItem => { attachEventListeners(listItem); // Ensure existing items have the same functionality }); // Function to attach event listeners to each list item function attachEventListeners(li) { const editBtn = li.querySelector('.edit'); // Select the edit button const deleteBtn = li.querySelector('.delete'); // Select the delete button const textarea = li.querySelector('textarea'); // Select the textarea const checkbox = li.querySelector('.checkbox'); // Select the checkbox // Event listener for the edit button editBtn.addEventListener('click', () => { textarea.disabled = false; // Enable editing of the textarea textarea.focus(); // Focus on the textarea for immediate typing }); // Event listener for when the textarea loses focus textarea.addEventListener('blur', () => { textarea.disabled = true; // Disable editing when focus is lost }); // Event listener for the delete button deleteBtn.addEventListener('click', () => { li.remove(); // Remove the list item from the DOM }); // Event listener for the checkbox to mark completion checkbox.addEventListener('change', () => { li.style.opacity = checkbox.checked ? '0.7' : '1'; // Adjust opacity based on completion status }); }