Troubleshooting problems with Flexbox layout and scrollbars

My goal for the chatbox was to align multiple divs to the bottom of the parent. I referred to this link on Stack Overflow: Align multiple divs to bottom of parent

Unfortunately, I encountered an issue where it wouldn't create a scrollbar after overflow.

Here is how it looks without using Flex: https://i.sstatic.net/IsZpz.gif

And here is the result after applying Flex to align messages to the bottom, but still facing scrolling problems: https://i.sstatic.net/kEmIA.gif

 let chatText = document.getElementById('chat-text');
 let chatText2 = document.getElementById('chat-text-with-flex');
     let chatInput = document.getElementById('chat-input');
     let chatForm = document.getElementById('chat-form');
   chatForm.onsubmit = (e) => {
          e.preventDefault(); 
                chatText.innerHTML += '<div>' + chatInput.value + '</div>';
                 chatText2.innerHTML += '<div>' + chatInput.value + '</div>';
     }
  
    #chat-bg {
          position: relative;
          height: 100%;
          width: 519px;
          height: 141px;
          overflow: auto;
     }
     
     #chat-text {
          position: absolute;
          margin-top: 5;
          padding-left: 10;
          border: solid 1px black;
          background-color: red;
          height: 112px;
          width: 502px;
          overflow-y: auto;
          color: black;
     }
  #chat-text-with-flex {
          position: absolute;
          margin-top: 5;
          padding-left: 10;
          border: solid 1px black;
          background-color: red;
          height: 112px;
          width: 502px;
          overflow-y: auto;
          color: black;
          display: flex;
          flex-direction: column;
          justify-content: flex-end;
     }

     #chat-text div,#chat-text-with-flex div {
          color: white;
          border: solid 1px black;
          background-color: blue;
          word-wrap: break-word;
          width: 482;
     }
<div id="chat-bg">
     <div id="chat-text">

          <div>Normal chatbox, scrollbar working after overflow</div>
          <div>Normal chatbox, scrollbar working</div>
          <div>Normal chatbox, scrollbar working</div>
     </div>
</div>

<div id="chat-bg">
     <div id="chat-text-with-flex">
          <div>Chat box with flex (to align message to bottom) but no scroll bar (problem)</div>
           <div>Chat box with flex (to align message to bottom) but no scroll bar (problem)</div>
            <div>Chat box with flex (to align message to bottom) but no scroll bar (problem)</div>
             <div>Chat box with flex (to align message to bottom) but no scroll bar (problem)</div>
     </div>
</div>

<form id="chat-form">
     <input id="chat-input" type="text" style="width:500px" autocomplete="off">
</form>

I am wondering if there is a way to enable scrolling with the Flexbox solution or if there are alternative solutions that can be explored. Any suggestions would be greatly appreciated.

Answer №1

To ensure proper functionality, the min-height property must be included.

let chatText2 = document.getElementById("chat-text-with-flex");
let chatInput = document.getElementById("chat-input");
let chatForm = document.getElementById("chat-form");

chatForm.onsubmit = (e) => {
  e.preventDefault();

  chatText2.innerHTML += "<div>" + chatInput.value + "</div>";
 $("#chat-bg").scrollTop($("#chat-bg")[0].scrollHeight);
};
#chat-bg {
  position: relative;
  height: 100%;
  width: 519px;
  height: 141px;
  overflow: auto;
}

#chat-text-with-flex {
  margin-top: 5;
  padding-left: 10;
  border: solid 1px black;
  background-color: red;
  width: 502px;
  height: auto;
  overflow: scroll;
  color: black;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
}

#chat-text div,
#chat-text-with-flex div {
  color: white;
  border: solid 1px black;
  background-color: blue;
  word-wrap: break-word;
  width: 482;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="chat-bg">
  <div id="chat-text-with-flex">

   <div> the issue has been resolved 1</div>
   <div> the issue has been resolved 2</div>
   <div> the issue has been resolved 3</div>
   <div> the issue has been resolved 4</div>
   <div> the issue has been resolved 5</div>

  </div>
</div>

<form id="chat-form">
  <input id="chat-input" type="text" style="width:500px" autocomplete="off">
</form>

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

Transforming each link into a formatted navigation bar in HTML

I added a logo to the top of my page but it ended up looking like my navigation bar. I suspect it's because of how I styled my nav bar. Even when I tried to adjust the logo's formatting using inline CSS, it didn't solve the issue. My Custom ...

Incorporating URL-linked images within Django

Is there an easy method to insert images into URLs in Django similar to this example? <img alt="Embedded Image" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIA..." /> I need to display multiple small images and I think this approach could b ...

Improved AJAX Dependency

A few days ago, a question was posted with messy code and other issues due to my lack of experience (please forgive the form handling as well). However, I have made some improvements and added context. The main problem now lies in the second AJAX call. Ch ...

Steps to create an automatic submission feature using a combobox in HTML5 and then sending the retrieved data back to the HTML file

Here is the code snippet I've been working on: <strong>Station Name</strong> <!--This portion includes a combobox using HTML5 --> <input type=text list=Stations> <datalist id=Stations> <option>Station1</opt ...

Display or conceal a div element depending on the user's choice

I am looking to hide inactive divs, but whenever I reload the page, all tab contents merge into one. The screenshot below shows the issue I'm facing. Screenshot Below is the code snippet: $('.tab a').on('click', function ...

Using `overflow: hidden` on a table will result in the bottom and right borders disappearing

I am facing an issue where the border of my table is being cut off when using overflow: hidden. Below is an example code snippet illustrating this problem. <style> table { border-collapse: collapse; } table { ...

Is there a similar feature to Bootstrap's img-responsive in Zurb Foundation?

Foundation's interchange feature allows you to replace individual images with smaller versions at various screen sizes, but I'm in need of a solution that resizes the images like Bootstrap's img-responsive class used to do. ...

Tips for styling an array of objects using mapping techniques

I have an array of messages and I am currently using the map() function. Each message in the array has two keys - one for the author and another for the message content. What I want to achieve is to change the styles of the div tag when displaying the last ...

How to bring in external HTML inner content using Javascript AJAX instead of relying on Jquery

Can someone assist me with a coding issue I'm facing? Here is the HTML code snippet: <ul class="menu-list"> <li class="active"><a href="cont1.html" title="Title 1">Link 1</a></li> <li><a href="cont2.html" ti ...

Establishing a secondary setTimeout function does not trigger the execution of JQUERY and AJAX

// Custom Cart Code for Database Quantity Update $('.input-text').on('keydown ' , function(){ var tr_parent = $(this).closest("tr"); setTimeout(function () { $(tr_parent).css('opacity', '0.3'); }, 4000); var i ...

What is the best way to customize the color scheme of a Bootstrap 4 range slider?

https://i.sstatic.net/MBona.png Looking for suggestions on how to customize the colors of ranges in Bootstrap 4 and change the blue thumb color to 'gray'. Below is the example HTML input code: <p id="slider" class="range-field"> <in ...

The Angular currency filter is displaying a strange blank space despite the correct syntax being used. Is there anyone who can assist me with this issue?

Hey everyone, I'm having some trouble with the currency filter in Angular. It's not working and just displaying a blank space. I'm new to learning Angular so any help would be greatly appreciated! Here is the code I'm using: <!DOCT ...

Can CSS3 animations and @keyframes be utilized within a MailChimp template?

After coming across an inspiring HTML newsletter, I was motivated to add some keyframes animations to a MailChimp template. To my disappointment, the animation works perfectly on local setup, but once uploaded to MailChimp, the @keyframe rules along with t ...

Advanced CSS Techniques for Customizing UI Elements

Currently, I am incorporating Material UI Components into my react-app. When it comes to customizing the styles of these components, such as adding margin-top and font-weight to a button text, I encountered a challenge due to using CSS Modules. I resorted ...

Steps to include a checkbox and button within a jQuery datatable column

Currently, I am attempting to insert a checkbox and button into all rows of a jQuery data table. The checkbox should be positioned in the first column while the button should appear in the final column. I am following the MVC format for this implementati ...

Include a div with absolute positioning that fits within the visible browser window

My current project involves creating a graphical canvas map with tooltips, but I am facing a challenge. I need to ensure that the tooltips, which are absolute positioned divs, remain within the viewport. Unfortunately, I am having trouble achieving this. ...

Verify if data already exists in an HTML table column using JavaScript

As a beginner in web programming, I am currently trying to dynamically add data to a table. But before inserting the data into the table, my requirement is to first verify if the data already exists in a specific column, such as the name column. function ...

Having trouble with Jquery not applying or removing the selected class in the navigation menu

My website is built using PHP, with the index file dynamically loading pages from a folder named "pages." Additionally, I have the navigation menu stored in a separate file. I am attempting to highlight the text color when a link is clicked, but I am faci ...

I'm curious as to why these two divs are positioned side by side. Can anyone shed some light on why this footer isn't functioning properly

My goal is to showcase the logo along with 3 buttons containing all the social media links underneath it. However, for some reason, my 2 divs are displaying inline instead of flex or block. Can anyone shed light on why this isn't functioning as expect ...

The property cannot be set for an undefined element in Jquery UI slider tabs

There seems to be some extra space at the bottom of the slider tabs between the navigators and content. Additionally, I am facing a challenge in making the page responsive as the content size does not adjust when I resize the window. To see the changes, I ...