Chat UI (Frontend Only) with HTML, CSS & JavaScript
40 DAYS 40 PROJECT CHALLENGE
Day #05
Project Overview
The Chat UI is a modern and responsive frontend project built using HTML, CSS, and JavaScript. It provides a simple chat interface where users can send messages and receive automated bot replies in a conversation-style layout.
This application includes user and bot message bubbles, automatic scrolling, and Enter key support for sending messages quickly. JavaScript handles message creation, reply generation, and dynamic updates without requiring a backend or external API.
This project is useful for beginners they will learn about DOM manipulation, event handling, conditional logic, delayed responses, and responsive interface design.
Key Features
- Chat Message Display: User and bot messages appear inside separate chat bubbles. Different alignment and background styles make the conversation easy to follow.
- Automated Bot Replies: It generates replies based on keywords in the user’s message. For example, messages containing “hello,” “price,” or “help” receive predefined responses.
- Message Validation: JavaScript checks whether the input contains meaningful text. Empty messages are ignored, preventing blank chat bubbles from appearing.
- User and Bot Layout: User messages appear on the right side, while bot messages appear on the left. This creates a familiar conversational interface.
- Enter Key Support: Users can press the Enter key to send a message instead of clicking the Send button. This makes the chat experience faster and more convenient.
- Delayed Bot Response: The bot replies after a 700-millisecond delay using
setTimeout(). This creates a simple simulated response experience. - Automatic Scrolling: Chat area scrolls to the latest message using the
scrollToBottom()function. As a result, users can easily follow the ongoing conversation. - Random Fallback Replies: When the message does not match a predefined keyword, JavaScript selects a random response from an array of general replies.
- Responsive Chat Interface: The chat container adapts to smaller screens. On mobile devices, it uses the full viewport height and removes the rounded corners.
- Clean User Interface: This project uses a minimal design with a dark header, light message area, rounded chat bubbles, and a simple input section.
What You'll Learn
- How to create a chat interface using HTML.
- How to style user and bot messages with CSS.
- How to dynamically create elements using JavaScript.
- How to handle button clicks and keyboard events.
- How to validate user input before displaying messages.
- How to use
setTimeout()for delayed responses. - How to apply conditional statements for chatbot logic.
- How to select random values from an array.
- How to automatically scroll an element using JavaScript.
- How to create responsive layouts using CSS media queries.
HTML Code
The HTML structure creates the main chat interface inside the .chat-container element. It contains three primary sections: the chat header, message area, input section and the .chat-header displays the application title, while .chat-messages contains the initial bot message and uses the chatMessages ID for dynamic message insertion. The input section includes a text field with the messageInput ID and a Send button that calls the sendMessage() function.
The page also connects the external CSS and JavaScript files. JavaScript later adds new user and bot messages inside the chat message container.
<!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
The CSS code creates a centered chat interface with a maximum width of 400px and a height of 600px. The body selector uses Flexbox to center the chat container vertically and horizontally against a dark blue background. This .chat-container class uses display: flex and flex-direction: column to organize the header, messages, and input sections. Its overflow: hidden property keeps the rounded container layout clean.
The .chat-messages selector uses flex: 1 so the message area occupies the available space. It also includes overflow-y: auto, allowing users to scroll through longer conversations. and the .message.user and .message.bot classes create different styles for user and bot messages. User messages align to the right with a dark background, while bot messages align to the left with a light gray background.
Alos the .chat-input section uses Flexbox to position the input and Send button side by side. Finally, the media query changes the chat container to full-screen height and removes its rounded corners on devices smaller than 500px.
* {
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
The JavaScript code manages message sending, bot replies, dynamic message creation, keyboard interaction, and automatic scrolling. First, the input and chatMessages variables select the message input field and chat message container using document.getElementById() and the sendMessage() function retrieves and trims the user’s input. If the input is empty, the function returns without adding a message. Otherwise, it calls addMessage() with the message text and the "user" type, clears the input field, and scrolls the chat area to the bottom.
Next, setTimeout() delays the bot’s response by 700 milliseconds. The generateReply() function converts the user’s message to lowercase and checks whether it contains keywords such as "hello", "price", "help", "bye", or "thank". It returns a matching response when a keyword is found. Otherwise, it selects a random response from the randomReplies array.
The addMessage() function creates a new div, assigns the message and message type classes, and inserts the message text inside a paragraph element. It then appends the new element to the chat container. Finally, the keypress event listener detects when the user presses Enter and calls sendMessage(). The scrollToBottom() function updates scrollTop using scrollHeight, ensuring that the latest message remains visible.
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;
} 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.