ArticleMarch 24, 2025

How to Build a Simple To-Do App with HTML, CSS, and JavaScript

How to Build a Simple To-Do App with HTML, CSS, and JavaScript

How to Build a Simple To-Do App with HTML, CSS, and JavaScript

Creating a to-do app is one of the best beginner projects for learning web development. It gives you the chance to practice HTML for structure, CSS for styling, and JavaScript for functionality. In this tutorial, we’ll walk through how to build a basic to-do app that allows users to add tasks, mark them as completed, and remove them.

What You’ll Need:

  • Basic understanding of HTML, CSS, and JavaScript.
  • A text editor (e.g., Visual Studio Code).
  • A browser to view your project.

Step 1: Setting Up the HTML Structure

The first step is to create the basic structure of our app using HTML. We need a text input to add tasks, a button to submit them, and a list where the tasks will appear.

Here’s the HTML code:

To-Do App
<div class\="todo-container"\> <h1\>My To-Do List</h1\> <input type\="text" id\="todo-input" placeholder\="Add a new task..."\> <button id\="add-btn"\>Add Task</button\> <ul id\="todo-list"\></ul\> </div\> <script src\="script.js"\></script\>

</body> </html>

  • The todo-container class holds everything in the center.
  • The input allows the user to type a new task.
  • The button will add the task to the list.
  • The ul element (todo-list) will hold the tasks as li elements.

Step 2: Styling the App with CSS

Next, let’s add some styles to make our app look better. Create a style.css file and add the following styles:

* { margin: 0; padding: 0; box-sizing: border-box; }

body { font-family: Arial, sans-serif; background-color: #f4f4f9; display: flex; justify-content: center; align-items: center; height: 100vh; }

.todo-container { background-color: white; padding: 20px; border-radius: 8px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); width: 300px; }

h1 { text-align: center; font-size: 24px; color: #333; }

input[type="text"] { width: 100%; padding: 10px; margin-bottom: 10px; border: 2px solid #ddd; border-radius: 4px; }

button { width: 100%; padding: 10px; background-color: #4CAF50; color: white; border: none; border-radius: 4px; cursor: pointer; }

button:hover { background-color: #45a049; }

ul { list-style-type: none; }

li { padding: 10px; border-bottom: 1px solid #ddd; display: flex; justify-content: space-between; align-items: center; }

li.completed { text-decoration: line-through; color: #888; }

button.remove-btn { background-color: red; border-radius: 50%; width: 25px; height: 25px; color: white; border: none; cursor: pointer; }

  • The body is centered using flexbox, and the app container has a white background with rounded corners and a subtle shadow for aesthetics.
  • The input field and button are styled to match modern UI trends, and the list of tasks is styled to display each task clearly with a button to remove it.

Step 3: Adding JavaScript Functionality

Now, it’s time to make our app interactive. We will create a script.js file to handle adding tasks, marking tasks as complete, and removing tasks.

// Select DOM elements const todoInput = document.getElementById("todo-input"); const addBtn = document.getElementById("add-btn"); const todoList = document.getElementById("todo-list");

// Event listener to add tasks addBtn.addEventListener("click", function() { const taskText = todoInput.value.trim();

if (taskText !== "") { const li = document.createElement("li"); // Add task text const taskContent = document.createElement("span"); taskContent.textContent = taskText; li.appendChild(taskContent); // Add remove button const removeBtn = document.createElement("button"); removeBtn.textContent = "X"; removeBtn.classList.add("remove-btn"); li.appendChild(removeBtn); // Add event listener to mark task as completed taskContent.addEventListener("click", function() { li.classList.toggle("completed"); }); // Add event listener to remove task removeBtn.addEventListener("click", function() { li.remove(); }); // Append the new task to the list todoList.appendChild(li); // Clear input field todoInput.value = ""; } else { alert("Please enter a task."); }

});

// Optional: Add enter key functionality todoInput.addEventListener("keypress", function(event) { if (event.key === "Enter") { addBtn.click(); } });

Here’s how the JavaScript works:

  1. Adding Tasks: When the "Add Task" button is clicked, we create a new li element with the task text and a remove button. If the input field isn’t empty, the task is added to the list.
  2. Marking Tasks as Completed: When the user clicks on the task text, the completed class is toggled, which will strike through the task text to show it's completed.
  3. Removing Tasks: The remove button (styled as a small red circle) deletes the task when clicked.
  4. Keyboard Support: Pressing the Enter key will also add the task, making the app more user-friendly.

Step 4: Test and Improve

Now that everything is in place, open your index.html file in a browser to test your app. You can add tasks, mark them as completed, and remove them.

Additional Features You Could Add:

  • Persistent Storage: Use localStorage to store tasks so that they persist even when the page is refreshed.
  • Edit Tasks: Allow users to edit tasks by clicking on them.
  • Priority Levels: Add a feature to mark tasks as high or low priority.

Conclusion

Congratulations! You’ve successfully created a simple to-do app with HTML, CSS, and JavaScript. This is a great starting point for building more advanced apps as you continue learning web development. You can now customize and expand your app by adding more features, refining the user interface, and exploring new technologies. Keep building and experimenting to sharpen your skills!

#WebDevelopment, #ToDoApp, #JavaScript, #HTML, #CSS, #Coding, #FrontendDevelopment, #WebDesign, #TechProjects, #LearnToCode, #WebApp, #CodeNewbie, #DeveloperLife, #BuildInPublic, #JavaScriptDeveloper.

Comments (0)

No comments yet. Be the first to share your thoughts!

Suggested for you

Blog

From the Blog

S.O.L.I.D Principles ( System Design)
Quick Preview

S.O.L.I.D Principles ( System Design)

SOLID is an acronym for five design principles that make software designs more understandable, flexi...

Read Article
Interview Experience (Cognizant GenC)
Quick Preview

Interview Experience (Cognizant GenC)

My Cognizant GenC Next On-Campus Hiring Experience (2025–26 Batch) Recently, I took part in the Cogn...

Read Article
How to Build a Simple To-Do App with HTML, CSS, and JavaScript
Quick Preview

How to Build a Simple To-Do App with HTML, CSS, and JavaScript

How to Build a Simple To-Do App with HTML, CSS, and JavaScript Creating a to-do app is one of the be...

Read Article
Web Developer Roadmap: A Step-by-Step Guide (2025)
Quick Preview

Web Developer Roadmap: A Step-by-Step Guide (2025)

Web Developer Roadmap: A Step-by-Step Guide Web development is one of the most in-demand tech skills...

Read Article