Angular div with scroll functionality

I am currently working on developing an Angular application that will showcase a JSON data structure in a layout similar to a chatbox. To achieve this, I have created a component specifically for the "chatbox".

Here is an example of the data structure within my chatbox.component.ts file:

  test = [
    { "date":"1","sender":"x","answer":"hello"},
    { "date": "2", "sender": "y", "answer": "hello my friend" },
    { "date": "3", "sender": "z", "answer": "bye" }
  ];

In the chatbox.component.html file:

<div class="inboxContainer">
        <tr *ngFor="let msg of test">
            <div class="wrapper">
                <div class="sender">Sender: {{msg.sender}}</div>
                <div class="date">Date : {{msg.date}}</div>
            </div>
            <div class="answer">Message: {{msg.answer}}<br></div>
        </tr>
</div>
<div class="newMessageContainer">
    <mat-form-field>
        <mat-label>New Message</mat-label>
        <textarea matInput cdkTextareaAutosize #autosize="cdkTextareaAutosize" cdkAutosizeMinRows="1"
            cdkAutosizeMaxRows="3"></textarea>
    </mat-form-field>

</div>
<div class ="sendMessageContainer">
<button mat-button>
    Send
</button>
</div>

Within the chatbox.component.css file:

.inboxContainer {
    width: 40vw;
    height: 60vh;
    border: 1px solid black;
     margin: auto;
     position: absolute;
     top: 0;
     left: 0;
     bottom: 0;
     right: 0;
}
.newMessageContainer {
     margin: auto;
     position: absolute;
     top: 80vh;
     left: 30vw;
     bottom: 0;
     right: 0;
}
.mat-form-field {
  width: 30vw;
  top:0.1vh;
}
.mat-button {
    left: 4vw;
    top:1.4vh;
    background-color: #B29B59;
}
.sendMessageContainer {
        margin: auto;
        position: absolute;
        top: 80vh;
        left: 60vw;
        bottom: 0;
        right: 0;
}
.wrapper {
  display:flex;
}
.date {
  border:1px solid black;
  width: 20vw;
  border-right:none;
  border-left:none;
}
.sender {
border:1px solid black;
width: 20vw;
border-left:none;
}
.answer {
width: 38.8vw;
height: 18vh;
}

If you view the external links provided, you can see that currently, the messages overflow beyond the fixed border when more than three messages are displayed. My query is centered around how to implement scrollbars within the div to address this issue.

Answer №1

Initially, I observed an error in the use of the tr tag.

<tr *ngFor="let msg of test">
  <div class="wrapper">
      <div class="sender">Sender: {{msg.sender}}</div>
      <div class="date">Date : {{msg.date}}</div>
  </div>
  <div class="answer">Message: {{msg.answer}}<br></div>
</tr>

It seems unclear why the tr is being used at this point. The correct usage for tr is within a table tag exclusively. You should replace it with div. Additionally, you must include overflow: auto; for .inboxContainer as shown below.

.inboxContainer {
    width: 40vw;
    height: 60vh;
    border: 1px solid black;
    margin: auto;
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    overflow: auto;
}

Answer №2

If you want to ensure a scrollbar appears in the .inboxContainer, simply add the CSS property overflow: auto. Here is an example of how it can be implemented:

.inboxContainer {
  overflow: auto; // You can also opt for 'overflow: scroll;' to always reserve space for the scrollbar
  width: 40vw;
  height: 60vh;
  border: 1px solid black;
  margin: auto;
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
}

Answer №3

everything is running smoothly

   .scrollable-div {
      // width: auto;
      // height: auto;
      // height: 1000px; /* set a fixed height for the div */
      // overflow-y: scroll; /* enable vertical scrollbar */
      // margin-bottom: 200px;
    
      overflow: auto; // you can use 'overflow: scroll;' and always have reserved space for scrollbar
      width: auto;
      // height: 100vh;
       border: 2px solid red;
      // margin: auto;
      position: absolute;
      top: 0;
      left: 0;
      bottom: 0;
      right: 0;
    }

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

How can I programmatically control the scrollbar of an iframe displaying a PDF using JavaScript?

As I explore ways to display PDF documents on a Smart TV, I have decided to implement invisible buttons for scrolling up and down the document. This functionality needs to be integrated into a web environment, so this is what I have attempted: Here is the ...

Fast CSS dilemma

Here is a fiddle I created: http://jsfiddle.net/ozzy/WEGMh/ This is how it is supposed to look: The issue is that the left and right hand divs are not displaying. I have attempted to use z-index, but there must be something obvious that I am missing. Pe ...

Displaying divs depending on dropdown selection - Troubleshooting script issue

When I try to display certain divs based on dropdown selection using the script below, it works fine on a simple page. However, when I implement it on my current development page, it completely messes up the layout, turning everything black and adding stra ...

Is it possible to include a scroll bar at the top of an overflow-x <div> in addition to or instead of the bottom?

Can the scroll bar be positioned at the top of a table instead of just the bottom? <div style="overflow-x: auto;"> <table> ... ... </table> </div> I am looking for a way to have the scroll bar appear at the top of my t ...

Transferring Information Across Angular Components via a Service Utilizing an RxJS Subject

The Goal I'm Pursuing In my Angular client app, I have two separate components. One component handles data fetching and processing from the server. My objective is to utilize the subscribe callback function to navigate to a different page where I can ...

Launching Your Ionic App as a Website

Is it possible to deploy an Ionic app as a website? If so, what are the potential security concerns or obstacles that may arise? Please provide guidance or resources on how to accomplish this. Thank you, ...

Vue: The enigmatic world of ghost properties?

During my project work, I encountered the following code snippet: Main component - <ParameterModal>: <template> <modal-wrapper props="..."> <!-- ... other similar templates are present... --> <template v-else-if="moda ...

Best practices for working with Angular: How to handle nested subscribe calls effectively?

My dashboard component has an extended ngOninit function where I need to perform some verifications before the component loads. this.startupSubscription = this.accountService.getInfo().pipe( switchMap(account => { this.account = account; retur ...

Enhance the speed of webview on Android devices

I have been working on an application that utilizes a webview to load both HTML and JavaScript files. Unfortunately, I have noticed a significant decrease in performance when displaying the content. Despite trying various solutions such as: webVi ...

Click on the div to automatically insert its text into the textarea

I am looking for a way to enable users to edit their posts easily. My idea is to have them click on a link, which will then hide the original div containing their post and reveal a new div with the old text inside a textarea for editing. Despite searching ...

Issue encountered: Cannot locate module: Error message - Unable to find 'stream' in 'C:devjszip-test ode_modulesjsziplib' folder

I am encountering an issue in my angular 7 application while using jszip v3.2.1. During the project build process (e.g., running npm start), I receive the following error message: ERROR in ./node_modules/jszip/lib/readable-stream-browser.js Module not fo ...

Modifying class selectors does not have any effect on newly added items

Imagine you have a ragged array structured as shown below: var myarray = [ [ ['k1', [] ], ['k2', ['l1', 'l2'] ], ['k3', [] ], ], [ ['k1', [] ], ['k2', ['l1', 'l2&ap ...

Issue with Javascript Date formatting as "dd-MMM-yyyy" in Internet Explorer

When retrieving data from the server side, I am encountering an issue with the date format dd-MMM-yyyy. if (new Date("02-MAY-2018").toDateString() == new Date(this.dob).toDateString()) { alert("No error"); } else { alert("error"); } The convers ...

There seems to be a problem with the while loop in the PHP code while creating a forum

Encountered a new error that says: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') AND type='o'' at line 1. Can someone assist in fixing this issu ...

Executing a filter operation on objects that contain optional variables

I'm currently working on filtering an object that includes a non-mandatory attribute. How can I handle ignoring the attribute if it doesn't exist? events:IEvent[]; filteredEvents:IEvent[]; performFilter(filterBy:string){ filterBy = fil ...

Collapse division in Twitter Bootstrap

Currently struggling to incorporate a Google Map into a 2 column grid using Bootstrap. The issue at hand is that the column containing the map has somehow collapsed, resulting in only the top portion of the map being visible when it should be fully embed ...

Looking to clean up unnecessary <b></b> tags from my Wordpress website - how can I do this?

One of my sites is located at . When inspecting the source code through the browser, I noticed the presence of empty tags. Does anyone know why these empty tags are being generated and how they can be removed? I have thoroughly searched through all files ...

NavigatedRoute and Auth-protect - problem retrieving ID from paramMap

Currently working on a website for my exam project, but encountering an issue with the AuthGuard not returning the correct ID in my code. event-details.component.ts getEvent(): void { const id = +this.route.snapshot.paramMap.get('id'); ...

JavaScript Reference for the HTML DOM Style's height property and revert back to the initial style height

Hey there! I have a simple fix for those who know JavaScript. I have some code that changes the height style on the first click, but I'm looking for a way to revert back to the original height style on the second click. Here's the code snippet: ...

Is it possible for me to alter the script for my button's onclick

Is there a way to replicate all properties of a div when creating a clone using JavaScript code? I have an existing script that successfully creates a clone of a div when a button is pressed, but it does not copy the CSS properties. How can I ensure that t ...