Chat UI (Frontend Only) with HTML, CSS & JavaScript

40 DAYS 40 PROJECT CHALLENGE

Day #05

Project Overview

A modern Chat UI (Frontend Only) built using HTML, CSS, and JavaScript with a clean and responsive interface. The application allows users to send messages and receive automated bot replies in a chat-style layout. It demonstrates frontend concepts such as dynamic message rendering, DOM manipulation, user interaction handling, and responsive UI design, making it a great beginner project for learning how chat interfaces work.

Key Features

  • Real-time message display in chat bubbles
  • User and bot message layout for clear conversation flow
  • Auto reply system using simple JavaScript logic
  • Enter key support for sending messages quickly
  • Automatic scroll to the latest message
  • Responsive chat interface for mobile and desktop devices
  • Clean and minimal chat design
  • Interactive UI powered by JavaScript DOM manipulation

HTML Code

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chat UI</title>
<link rel="stylesheet" href="style.css">
</head>

<body>

<div class="chat-container">

  <div class="chat-header">
    <h3>Chat App</h3>
  </div>

  <div class="chat-messages" id="chatMessages">
    <div class="message bot">
      <p>Hello 👋 How can I help you today?</p>
    </div>
  </div>

  <div class="chat-input">
    <input type="text" id="messageInput" placeholder="Type a message...">
    <button onclick="sendMessage()">Send</button>
  </div>

</div>

<script src="script.js"></script>
</body>
</html>

CSS Code

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: 'Segoe UI', sans-serif;
  background: #042354;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

.chat-container {
  width: 100%;
  max-width: 400px;
  height: 600px;
  background: #fff;
  display: flex;
  flex-direction: column;
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.1);
  overflow: hidden;
}

.chat-header {
  padding: 15px;
  background: #111;
  color: #fff;
  text-align: center;
}

.chat-messages {
  flex: 1;
  padding: 15px;
  overflow-y: auto;
  background: #f9fafb;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.message {
  max-width: 75%;
  padding: 10px 14px;
  border-radius: 18px;
  font-size: 14px;
}

.message.user {
  align-self: flex-end;
  background: #111;
  color: #fff;
  border-bottom-right-radius: 4px;
}

.message.bot {
  align-self: flex-start;
  background: #e5e7eb;
  border-bottom-left-radius: 4px;
}

.chat-input {
  display: flex;
  padding: 10px;
  border-top: 1px solid #eee;
}

.chat-input input {
  flex: 1;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 6px;
  outline: none;
}

.chat-input button {
  margin-left: 8px;
  padding: 10px 15px;
  background: #111;
  color: #fff;
  border: none;
  border-radius: 6px;
  cursor: pointer;
}

.chat-input button:hover {
  background: #333;
}

/* Mobile Responsive */

@media(max-width:500px){
  .chat-container{
    height:100vh;
    border-radius:0;
  }
}

Javascript Code

const input = document.getElementById("messageInput");
const chatMessages = document.getElementById("chatMessages");

function sendMessage() {

  const messageText = input.value.trim();

  if (messageText === "") return;

  addMessage(messageText, "user");

  input.value = "";

  scrollToBottom();

  setTimeout(() => {

    const botReply = generateReply(messageText);

    addMessage(botReply, "bot");

    scrollToBottom();

  }, 700);
}

function addMessage(text, type) {

  const message = document.createElement("div");

  message.classList.add("message", type);

  message.innerHTML = `<p>${text}</p>`;

  chatMessages.appendChild(message);
}

/* Bot reply logic */

function generateReply(userText) {

  const text = userText.toLowerCase();

  if (text.includes("hello") || text.includes("hi")) {
    return "Hello there 👋 How can I assist you?";
  }

  if (text.includes("price")) {
    return "Our pricing depends on the product. Which one are you interested in?";
  }

  if (text.includes("help")) {
    return "Sure 😊 Tell me what you need help with.";
  }

  if (text.includes("bye")) {
    return "Goodbye 👋 Have a great day!";
  }

  if (text.includes("thank")) {
    return "You're welcome 😊";
  }

  const randomReplies = [
    "That sounds interesting!",
    "Can you tell me more?",
    "I'm here to help 😊",
    "Got it 👍",
    "Let me think about that..."
  ];

  return randomReplies[Math.floor(Math.random() * randomReplies.length)];
}

/* Enter key send */

input.addEventListener("keypress", function(e){

  if(e.key === "Enter"){
    sendMessage();
  }

});

function scrollToBottom(){

  chatMessages.scrollTop = chatMessages.scrollHeight;

}
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Related Projects

Day 2 : Kanban Board (Drag & Drop) 

A drag-and-drop task board to manage tasks across different stages.

Concepts: DOM manipulation, drag & drop API, event handling.

Day 3 : E-commerce Product Page

Generates an OTP via alert and verifies it through a popup-based input UI.

Concepts: DOM manipulation, modal UI, random OTP generation, input handling.

Day 7 : Survey / Poll App

Switches content dynamically using a tab-based navigation UI.

Concepts: DOM manipulation, event handling, UI state management.