Bootstrap 5 Chat Example

Ruth Jul 09, 2026

Bootstrap, the popular CSS framework, has evolved significantly over the years, and its latest iteration, Bootstrap 5, brings a host of new features and improvements. One of the most engaging aspects of web development is creating interactive user interfaces, and chat functionality is a prime example of this. In this article, we'll explore how to create a simple chat example using Bootstrap 5.

ChatX bot Admin - Chat Software Template
ChatX bot Admin - Chat Software Template

Before we dive into the code, let's briefly discuss why Bootstrap is an excellent choice for building responsive chat interfaces. Bootstrap's grid system ensures that your chat layout adapts perfectly to different screen sizes, from desktops to mobile devices. Additionally, Bootstrap's pre-built components and utilities can speed up your development process.

a computer screen with chat bubbles on the left and right hand corner, in purple tones
a computer screen with chat bubbles on the left and right hand corner, in purple tones

Setting Up the Bootstrap 5 Environment

To get started with Bootstrap 5, you'll first need to include it in your project. You can either download the latest version from the official website or use a CDN. For this example, we'll use the CDN version.

Chat Site Page Design
Chat Site Page Design

Add the following line inside the section of your HTML file:

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet">

Creating the Chat Container

a computer screen with several chat bubbles on the bottom right corner and an emoticive message in the middle
a computer screen with several chat bubbles on the bottom right corner and an emoticive message in the middle

First, let's create a container for our chat application. We'll use Bootstrap's container class to center the chat on larger screens and make it full-width on smaller devices.

Add the following code inside the section of your HTML file:

<div class="container">
  <div class="chat-container">
    <div class="chat-messages">
      <div class="message">
        <p>Hello, World!</p>
      </div>
    </div>
    <form>
      <input type="text" class="form-control" placeholder="Type your message...">
      <button type="submit" class="btn btn-primary">Send</button>
    </form>
  </div>
</div>

Styling the Chat Messages

Chat Design
Chat Design

Now, let's style the chat messages to make them more visually appealing. We'll use Bootstrap's utility classes to add some padding, borders, and background colors.

Update the chat messages code as follows:

<div class="chat-messages">
  <div class="message p-3 mb-3 bg-light rounded-start">
    <p>Hello, World!</p>
  </div>
</div>

Implementing Real-time Chat Functionality

the relationship between me and chatgpt😭😭
the relationship between me and chatgpt😭😭

To create a real-time chat experience, we'll use WebSockets with the help of a library like Socket.IO. For this example, we'll assume you have already set up a server with Socket.IO and are familiar with its basic usage.

First, include the Socket.IO client library in your project:

Contact me on Chatbot marketing | Instagram Facebook and all social media’s platforms DM
Contact me on Chatbot marketing | Instagram Facebook and all social media’s platforms DM
GitHub - ntkhang03/Goat-Bot-V2: A simple Bot chat messenger using personal account. Made by NTKhang.
GitHub - ntkhang03/Goat-Bot-V2: A simple Bot chat messenger using personal account. Made by NTKhang.
an abstract background with blue and white lines on black paper, which are separated by circles
an abstract background with blue and white lines on black paper, which are separated by circles
He asked for it!!!
He asked for it!!!
two screens showing the same text messages as well as different people's avatars
two screens showing the same text messages as well as different people's avatars
the text messages are displayed in different languages
the text messages are displayed in different languages
an image of a computer screen with the texting section highlighted in purple and white
an image of a computer screen with the texting section highlighted in purple and white
ChatGPT rolled out new system shortcuts /picture and /search
ChatGPT rolled out new system shortcuts /picture and /search
💬chat.... Follow
💬chat.... Follow
💬chat... By 🤞friends
💬chat... By 🤞friends
Freshchat: Customer Messaging, Live Chat & Chatbots | Freshworks
Freshchat: Customer Messaging, Live Chat & Chatbots | Freshworks
an image of two people texting on the same phone screen while another person is looking at them
an image of two people texting on the same phone screen while another person is looking at them
the text messages are in different languages on the screen, and there is also an animated character
the text messages are in different languages on the screen, and there is also an animated character
Exploring the Power of Chat Application Dashboard with Chatbot Admin
Exploring the Power of Chat Application Dashboard with Chatbot Admin
AI Chatbot Software for Your Website | Get More Leads
AI Chatbot Software for Your Website | Get More Leads
Bootstrap 5.3 (2025) Cheat Sheet
Bootstrap 5.3 (2025) Cheat Sheet
Creating a Telegram chatbot with ChatGPT: A Step-by-Step Guide
Creating a Telegram chatbot with ChatGPT: A Step-by-Step Guide
an abstract black and purple wallpaper with lots of small dots in the middle of it
an abstract black and purple wallpaper with lots of small dots in the middle of it
୨ ˚ ₊ cute chat with boyfie, yujin ૮₍ ˃ ⤙
୨ ˚ ₊ cute chat with boyfie, yujin ૮₍ ˃ ⤙
a cat sitting on top of a white table next to a glass window sill
a cat sitting on top of a white table next to a glass window sill

<script src="/socket.io/socket.io.js"></script>

Sending Messages

Now, let's add event listeners to send messages when the form is submitted. We'll use the Socket.IO client to emit a 'chat-message' event with the message as data.

Add the following JavaScript code to your HTML file:

<script>
  const socket = io();

  document.querySelector('form').addEventListener('submit', (e) => {
    e.preventDefault();
    const input = document.querySelector('input');
    socket.emit('chat-message', input.value);
    input.value = '';
  });
</script>

Receiving Messages

Next, we'll listen for 'chat-message' events from the server and append new messages to the chat container.

Update the JavaScript code as follows:

<script>
  const socket = io();

  document.querySelector('form').addEventListener('submit', (e) => {
    e.preventDefault();
    const input = document.querySelector('input');
    socket.emit('chat-message', input.value);
    input.value = '';
  });

  socket.on('chat-message', (message) => {
    const chatMessages = document.querySelector('.chat-messages');
    const newMessage = document.createElement('div');
    newMessage.classList.add('message', 'p-3', 'mb-3', 'bg-light', 'rounded-start');
    newMessage.innerHTML = '<p>' + message + '</p>';
    chatMessages.appendChild(newMessage);
    chatMessages.scrollTop = chatMessages.scrollHeight;
  });
</script>

With these changes, you now have a simple, real-time chat example using Bootstrap 5 and Socket.IO. This example demonstrates the power of Bootstrap's responsive design and the ease of integrating real-time functionality with WebSockets.

To further enhance your chat application, consider adding features like user authentication, private messaging, and chat rooms. Don't forget to explore Bootstrap's official documentation for more components and utilities that can help you build engaging user interfaces.