Ensure that the text input box is positioned at the bottom of the chatbox and enable scrolling

Is there a way to make the textbox stay fixed at the bottom of the chatbox, regardless of how many messages are present? Currently, it appears after the last message. Additionally, I would appreciate assistance in implementing a scroll feature that automatically moves the view down to the latest message. At the moment, new messages exceed the desired height of the box.

.chatbox {
  position: relative;
  background-color: #fff;
  max-width: 350px;
  height: 400px;
  margin: 20px;
  border-radius: 10px;
  font-family: Avenir;
  font-size: 14px;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}

.nav {
  background-color: #0077fb;
  height: 30px;
  padding: 10px;
  color: #ffff;
  border-top-left-radius: 10px;
  border-top-right-radius: 10px;
  display: flex;
  align-items: center;
}

.chatbody {
  padding: 10px 10px;
}

#bot {
  padding: 10px;
  float: left;
  margin: 5px;
  max-width: 90%;
  display: table;
  clear: both;
  margin-left: 15px;
  background-color: #edefed;
  border-top-left-radius: 20px;
  border-top-right-radius: 20px;
  border-bottom-right-radius: 20px;
}

#user {
  padding: 10px;
  float: right;
  max-width: 70%;
  background-color: #0077fb;
  margin: 5px;
  color: white;
  margin-right: 15px;
  display: table;
  clear: both;
  border-top-left-radius: 20px;
  border-top-right-radius: 20px;
  border-bottom-left-radius: 20px;
}

#text {
  width: 90%;
  outline: none;
  border: none;
  padding: 10px;
  border-radius: 50px;
  margin-top: 7px;
  margin-left: 10px;
}

.icon {
  padding: 15px;
  color: #0077fb;
  right: -30px;
  min-width: 50px;
  position: absolute;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

<container>
  <div class="chatbox">
    <div class="nav">
      <span>Alice</span>
    </div>
    <div class="chatbody">
      <div id="bot">Hi there</div>
      <div id="bot">How are you doing ?</div>
      <div id="user">I am doing good</div>
      <div id="bot">Awesome. So how can I help you today ?</div>
    </div>
    <div>
      <input type="text" id="text" name="" placeholder="Message" autocomplete="off">
      <i class="fa fa-send icon"></i>
    </div>
  </div>
</container>

Answer №1

I've made changes to one of your div elements and added a new class called .message. Here is the code I used:

.message {
  position: absolute;
  bottom: 0;
  width: 100%;
}

View the demo below:

.chatbox {
  position: relative;
  background-color: #fff;
  max-width: 350px;
  height: 400px;
  margin: 20px;
  border-radius: 10px;
  font-family: Avenir;
  font-size: 14px;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}

.nav {
  background-color: #0077fb;
  height: 30px;
  padding: 10px;
  color: #ffff;
  border-top-left-radius: 10px;
  border-top-right-radius: 10px;
  display: flex;
  align-items: center;
}

.chatbody {
  padding: 10px 10px;
}

#bot {
  padding: 10px;
  float: left;
  margin: 5px;
  max-width: 90%;
  display: table;
  clear: both;
  margin-left: 15px;
  background-color: #edefed;
  border-top-left-radius: 20px;
  border-top-right-radius: 20px;
  border-bottom-right-radius: 20px;
}

#user {
  padding: 10px;
  float: right;
  max-width: 70%;
  background-color: #0077fb;
  margin: 5px;
  color: white;
  margin-right: 15px;
  display: table;
  clear: both;
  border-top-left-radius: 20px;
  border-top-right-radius: 20px;
  border-bottom-left-radius: 20px;
}

#text {
  width: 90%;
  outline: none;
  border: none;
  padding: 10px;
  border-radius: 50px;
  margin-top: 7px;
  margin-left: 10px;
}

.icon {
  padding: 15px;
  color: #0077fb;
  right: -30px;
  min-width: 50px;
  position: absolute;
}

.message {
  position: absolute;
  bottom: 0;
  width: 100%;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

<container>
  <div class="chatbox">
    <div class="nav">
      <span>Alice</span>
    </div>
    <div class="chatbody">
      <div id="bot">Hi there</div>
      <div id="bot">How are you doing ?</div>
      <div id="user">I am doing good</div>
      <div id="bot">Awesome. So how can I help you today ?</div>
    </div>
    <div class="message">
      <input type="text" id="text" name="" placeholder="Message" autocomplete="off">
      <i class="fa fa-send icon"></i>
    </div>
  </div>
</container>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Managing Session Cookies in ExpressJS

Hey there! I've been dealing with a problem while using Express-Session to manage my server-side sessions. When I set user data to session variables in one file, it works perfectly. However, when I try to access those variables in another file, all I ...

CSS: Issue with rounding errors in Em units

Recently, I decided to update the CSS file for a website I'm working on by making most elements and fonts dynamic in size using the em unit instead of px. The new sizes seem to be working, but there's an issue that's been bothering me. It s ...

The function get() in p5.js will provide you with an array of pixels that is empty

I am currently working on a project that is inspired by this particular video. Within p5.js, I have been utilizing the get() function. My goal is to divide a large tileset into smaller images and store them in an array. However, I have encountered an issue ...

Run JavaScript when ColdFusion page is being loaded

Within my ColdFusion page, I have incorporated multiple cfinclude template calls to bring in separate files. Before each cfinclude template call, I am seeking a way to update a javascript variable. I have attempted to achieve this by using: <script typ ...

Saving images to your local folder with Angular.js and PHP: A step-by-step guide

I am looking to upload and store an image in a local folder using Angular.js and PHP. Below is the code snippet I have: <div class="col-md-6 bannerimagefile" ng-controller="imgController"> <form name="form"> <label for="banneri ...

Deactivate the date when it reaches 8 in the current date count using ajax, php, and mysql

I want to prevent users from selecting a specific date after it has been chosen 8 times. Currently, when the date is selected for the 9th time, an alert box pops up. Instead of the alert box, I would like to disable that particular date selection altogethe ...

The background-color of a single child within an absolutely positioned element in Chrome may not be displayed if the element has a z-index and overflow scrolling

This test page has been created to showcase the issue: <html> <head> <style> .panel { position: absolute; width: 326px; max-height: 200px; overflow: scroll; z-index: 1000; } .item-container { width: 100%; list-style: none; ...

Breaking down setInterval IDs for their corresponding function parameters

I plan on running multiple setIntervals, and though there may be a more efficient way to do it, that's something I'll consider later down the line. Is there a method for achieving this? var a = setInterval(function(a){ console.log(a); c ...

Customize each Picker.Item element in React Native with unique styles

How can I style individual Picker.Items beyond just changing the text color? Additionally, what other props can be used for a Picker.Item? I am aware of "key", "value", "label", and "color". Are there any additional props available? I want to customize o ...

Vue-bootstrap spinbutton form with customizable parameters

I am in need of a custom formatter function for the b-form-spinbutton component that depends on my data. I want to pass an extra argument to the :formatter-fn property in the code snippet below, but it is not working as expected. <b-form-spinbutton :for ...

Tips for utilizing the TinyMCE special button and personalized dialog box for inserting content into your website

By utilizing the TinyMCE editor, I successfully added a custom button to its toolbar and connected it to a Flickr account. This allows a specialized dialog box to display with various image options. The goal is for users to click on an image within the di ...

Pug Template Not Displaying Emojis or Special Characters in Sendgrid Email Subject Line

There seems to be an issue with rendering emojis or special characters in the subject header of the pug file, although they work perfectly in the body. I have compiled both the body and subject header separately using pug. const compileHtmlFunction = pug.c ...

What could be causing my canvas element to only display a blank black screen?

Setting up a 3d asset viewer in Three.js can be quite challenging, especially for someone who is new to JavaScript like myself. After following the advice to wrap my code within an 'init();' function, I encountered a new issue - a black screen in ...

invoking a parent function from a cellRenderer in Vue.js Ag-Grid through emit function

I integrated the ag-grid-vue into my project and added a separate component to one of the columns for user actions, such as Edit, View, or Delete. Editing and deleting records work fine, but when a record is deleted, I want the table to re-render by fetchi ...

Having trouble with the `npm run dev` command in a Laravel project? You may be encountering a `TypeError: program.parseAsync is not

Recently, I integrated Vue.js into my ongoing Laravel project using npm to delve into front-end development with the framework. Following the installation, the instructions guided me to execute npm run dev in order to visualize the modifications made in . ...

Scrolling without limits - cylindrical webpage (CSS/JS)

Is it possible to create a page that infinitely scrolls from top to top without going straight back to the top, but instead showing the bottom content below after reaching it? Imagine being able to scroll up and see the bottom above the top like a 3D cylin ...

Repair the voting system

In my Ruby on Rails app, I have successfully set up an up/down voting system that utilizes ajax. When a user clicks the buttons, it triggers the create method to insert a vote into the database and calculate the total sum of votes. Currently, users can fr ...

Having trouble with images not showing up on React applications built with webpack?

Hey there! I decided not to use the create react app command and instead built everything from scratch. Below is my webpack configuration: const path = require("path"); module.exports = { mode: "development", entry: "./index.js", output: { pa ...

Suggestions for arranging nested list items securely?

Hello, I'm new to web design and would greatly appreciate any assistance. I've been learning HTML and CSS through a live project but have run into trouble with positioning nested list items. This is how my web browser renders the page: Here&a ...

What could be causing variations in the performance of my Speech Recognition code each time I run it?

Check out my code snippet: export class voiceRecognition { constructor() { } public startVoiceRecognition() { const recognition = new webkitSpeechRecognition(); recognition.continuous = false; recognition.interimresults = false; recogn ...