I'm currently working on styling a simple chat application, nothing too extravagant, but I'm encountering a few obstacles. My styling goals include:
- Aligning blue messages (from you) to the right and gray messages (from other users) to the left.
- Displaying messages at the bottom of the div, with each new message pushing the older ones up.
- Implementing scroll functionality for the div once the messages exceed a certain height in pixels.
Here's the code I have so far:
const API_URL = "http://localhost:3000"
const form = document.querySelector("form")
const chatbox = document.querySelector(".chatbox")
const chatInput = document.querySelector(".message-input")
const socket = io.connect(API_URL)
socket.on("chat-message", (data) => {
console.log(data)
appendMessage("other-message", data)
})
socket.on("disconnect", (reason) => {
socket.emit("disconnect", reason)
});
form.addEventListener("submit", e => {
e.preventDefault()
console.log("message: ", chatInput.value)
const message = chatInput.value
appendMessage("message", message)
socket.emit("send-chat-message", message)
form.reset()
})
function appendMessage(className, message){
const div = document.createElement('div')
div.append(message)
div.className = className
chatbox.append(div)
}
.wrapper{
display: grid;
grid-template-columns: 1fr 2fr;
height: 100vh;
width: 100vw;
}
.side-bar{
background-color: blue;
color: gray;
text-align: left;
padding: 20px;
font-size: 50px;
}
.chat-room:hover{
cursor: pointer;
color: white;
}
.messages-container{
display: grid;
grid-template-columns: auto;
grid-template-rows: auto 40px;
}
.chatbox{
background-color: black;
border: 1px solid white;
overflow: scroll;
}
.chatbox > div{
border-radius: 15px;
padding: 10px;
margin: 20px;
color: white;
width: fit-content;
max-width: 50%;
}
.message{
background-color: blue;
}
.other-message{
background-color: gray;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="styles.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<title>chat</title>
</head>
<body>
<div class = "wrapper">
<div class="side-bar">
<div class="name">
</div>
<div class="chat-room">
<h3>#chat room 1</h3>
</div>
<div class="chat-room">
<h3>#chat room 2</h3>
</div>
<div class="chat-room">
<h3>#chat room 3</h3>
</div>
<div class="chat-room">
<h3>#lifetime chat</h3>
</div>
</div>
<div class="messages-container">
<!-- Chat messages will be appended here! -->
<div class="chatbox">
</div>
<form>
<input type="text" class="message-input form-control" required name="send-message" placeholder="Send message" aria-label="Recipient's username" aria-describedby="basic-addon2">
</form>
</div>
</div>
<script src="https://cdn.socket.io/socket.io-3.0.1.min.js"></script>
<script src="sendMessage.js"></script>
</body>
</html>
I'm currently facing some issues like the div not overflowing when the number of messages exceeds the height specified in the "chat" div in the HTML file. Despite adding the code for overflow: scroll;
in the CSS for that specific div, it continues to expand infinitely. Additionally, I'm unsure how to make the messages push to the bottom of the div rather than the top, with each new message displacing the previous one upwards.
Are there any CSS techniques or tricks that could help achieve these three objectives?