so I copied this video https://www.youtube.com/watch?v=_sxoqRIbW0c
this is what I got from it
for the html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Chat App</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="container">
<div class="chat-header">
<h3>Chat App</h3>
</div>
<div class="chat-body" id="chat-body"></div>
<div class="chat-input">
<input type="text" id="message-input" placeholder="Type a message..." />
<button id="send-btn">Send</button>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
i got this for the styles.css
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: Arial, sans-serif;
}
body {
background: #f5f5f5;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.container {
width: 400px;
background: #fff;
border-radius: 10px;
overflow: hidden;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
.chat-header {
background: #4a76fd;
color: #fff;
padding: 15px;
text-align: center;
}
.chat-body {
height: 400px;
padding: 15px;
overflow-y: auto;
}
.message {
margin-bottom: 15px;
padding: 10px;
background: #eaeaea;
border-radius: 5px;
width: fit-content;
max-width: 80%;
}
.message.you {
background: #4a76fd;
color: #fff;
margin-left: auto;
}
.chat-input {
display: flex;
border-top: 1px solid #ddd;
}
.chat-input input {
flex: 1;
padding: 15px;
border: none;
outline: none;
}
.chat-input button {
padding: 15px 20px;
background: #4a76fd;
color: #fff;
border: none;
cursor: pointer;
}
and this for js
const chatBody = document.getElementById("chat-body");
const messageInput = document.getElementById("message-input");
const sendBtn = document.getElementById("send-btn");
function addMessage(text, sender = "you") {
const message = document.createElement("div");
message.classList.add("message", sender);
message.textContent = text;
chatBody.appendChild(message);
chatBody.scrollTop = chatBody.scrollHeight;
}
sendBtn.addEventListener("click", () => {
const text = messageInput.value.trim();
if (text !== "") {
addMessage(text, "you");
messageInput.value = "";
}
});
messageInput.addEventListener("keypress", (e) => {
if (e.key === "Enter") {
sendBtn.click();
}
});