What is the method for changing the anchoring in a `<div>` so that it is located at the bottom rather than the top?

I am currently working on a chat application and facing an issue with anchoring messages to the bottom of the message index while allowing them to grow upwards instead of hanging from the top. The project is developed using React, so the code provided below is in JSX.

Message Index:

<div className="chat-message-index">
  {this.state.activeChannel.messages.map(function(message{
    return <Message key={message.sid} message={message} />;
  })}
</div>

Messages (rendered within the index):

<div className="chat-message">
  <b className="name">{this.state.author}</b><br/>
  <span dangerouslySetInnerHTML={{__html: this.state.message.body}} />
</div>

CSS:

.chat-message-index {
  height: calc(~"100vh - 250px");
  position: absolute;
  overflow-y: scroll;
  width: 100%;
  background-color: silver;
  display: list-item;
}

.chat-message {
  position:relative;
  padding:10px;
  margin:22px 45px 22px 20px;
  color:#fff;
  background:#43464b;
  background:-webkit-gradient(linear, 0 0, 0 100%, from(#aeb5ba), to(#43464b));
  background:-moz-linear-gradient(#aeb5ba, #43464b);
  background:-o-linear-gradient(#aeb5ba, #43464b);
  background:linear-gradient(#aeb5ba, #43464b);
  -webkit-border-radius:10px;
  -moz-border-radius:10px;
  border-radius:10px;
}

I have exhausted all CSS techniques and ideas found through research without success. I attempted using transform:rotate(180deg); on both the message index and messages, but this caused issues with scrolling and positioning. While I may be able to make this approach work, it seems like a hacky solution that could lead to complications in the future...

Is there a simple way to invert the anchoring of a <div>? I simply want the first message displayed at the bottom of the message index <div>, with subsequent messages stacking on top of each other.

Answer №1

Utilizing Flexbox for the solution:

.chatbox {
  margin: 1em auto;
  width: 150px;
  height: 500px;
  border: 1px solid grey;
  display: flex;
  flex-direction: column-reverse;
  justify-content: flex-start;
  text-align: center;
}
.message {
  background: lightgreen;
  margin-bottom: .25em;
  padding: .25em 0 0 0;
  border-bottom: 2px solid green;
}
<div class="chatbox">
  <div class="message">MESSAGE 1</div>
  <div class="message">MESSAGE 2</div>
  <div class="message">MESSAGE 3</div>
  <div class="message">MESSAGE 4</div>
  <div class="message">MESSAGE 5</div>
  <div class="message">MESSAGE 6</div>
  <div class="message">MESSAGE 7</div>
  <div class="message">MESSAGE 8</div>
  <div class="message">MESSAGE 9</div>
  <div class="message">MESSAGE 10</div>
  <div class="message">MESSAGE 11</div>
  <div class="message">MESSAGE 12</div>
</div>

Answer №2

Here are a couple of options to consider:

  1. [css] Implement flexbox with flex-direction: column-reverse. Visit https://developer.mozilla.org/en-US/docs/Web/CSS/flex-direction for more information. Check out their sample which demonstrates the desired outcome.

  2. [js] Rearrange your messages list so that the newest item is at index 0 and the oldest item is at index last. Keep in mind this method may not be as elegant as the css solution mentioned above.

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

Why is the Clean up function being utilized in this particular scenario in React?

What happens when I use the clean-up function compared to when I do not? It seems that both options result in the same outcome in terms of program execution, so why bother using the clean-up function at all? function MyComponent() { const [text, setTex ...

Change the width of <p> element to control the number of lines visible on the screen

Is there a way to adjust the width of specific paragraph elements so they automatically have two lines with the same width? Even when the text is longer, the width should adjust to ensure both lines are equal in length (with the last line not wider than t ...

Storing the blob received from an AJAX call into Azure Blob Storage may result in a corrupted image

When I send a PDF to my vendor's API, they return a .png file as a blob (note: see update 2 regarding the data type being returned). I want to store this in Azure Blob Storage. However, when using the code below, the uploaded file becomes corrupted. ...

Menu bar link is unresponsive despite being active

I'm struggling to add "a:hover" to my home button in the menu bar and can't seem to remove the text-decoration. When I attempted to use media queries, things got confusing for me. **To post a question on Stack Overflow, it seems that I need to p ...

Ways to customize a bot's status based on the time of day

I'm currently working on enhancing my discord bot by having its status change every hour for a more dynamic user experience. However, I'm facing challenges with JavaScript dates. Can anyone provide some guidance? Below is the snippet of code I ha ...

Prevent animations on child elements with Vue.js

I am dealing with a scenario where I want to remove the fade transition on a child div within a <transition> component. The reason for nesting it is to prevent layout flickering, which can be demonstrated in a fiddle if necessary. In the fiddle belo ...

I need help figuring out how to increase the HTML h4 tag within an AJAX call. Whenever I try to debug, I keep getting values like h

<script> var pageUrl = "http://localhost:12152/Product.aspx"; window.onload = function () { var ncount = 0; ncount++; $("#callproceed").click(function () { alert("working"); $.ajax({ type: "POST", ...

Creating an invoice or money receipt using HTML is a straightforward process that involves using specific code and

Currently, I'm in the process of developing an application for a land developer company. The administrator of this application will be responsible for creating invoices, money receipts, and bills. I am looking to design these documents to resemble th ...

What is the best way to toggle the visibility of my menu using JavaScript?

I recently implemented a script to modify a CSS property in my nav bar as I scroll down, triggering the change after reaching 100px. $(window).scroll(function() { var scroll = $(window).scrollTop(); //console.log(scroll); if ...

"Despite encountering an error, the jQuery AJAX request remains active and continues to

Check out this code snippet I created. early_face_detect=$.ajax({ type: "POST", url: "earlydetect.py", timeout: 15000, success: function(respond) { var s=grab_early_details(respond); }, error: function(xmlhttprequest, textstatus, message) ...

Ways to display content alongside an image

Is there a way to verify if an application is running on another server by displaying an image it provides? The challenge is that altering the content of the image is not an option. If the image is not provided, can I show an alt attribute indicating that ...

Utilizing the button's id to display a partial view within Rails

I am working on a view that showcases a partial containing a list of events created by a user, each with an edit link alongside. My goal is to have a form partial appear below the event when the 'edit' link is clicked. To achieve this, I have su ...

Discrepancy in Rounding Methods: Android App vs. JavaScript Implementation

When working with numbers, I encountered a discrepancy in the results obtained from calculating and rounding. The value for today is 336887, and for yesterday it is 336582. I faced a similar issue with a different field, but after updating the Java code, t ...

Invoking a Components function from a Service in Angular may lead to a potential cyclic dependency issue

I am facing a challenge where I need to call a function from my filterComponent(component) within my engagementService(service). The function in my filterComponent accesses an array that is located within the engagementService. It uses the data from this ...

Avoiding state transitions within Angular's ui-router from within a directive

One of the challenges I am facing involves a directive called clickable-tag, where I pass my data as the tag's name (tag.tag): <a class="item item-avatar" ui-sref="nebula.questionData({questionId: question.id})" ng-repeat="question in questi ...

Dynamically remove a MongoDB entry with precision

I'm having trouble deleting an entry in MongoDB based on the id variable. Even when I pass the id as a parameter to the method, it doesn't seem to work. I've double-checked the variable inside the method and I'm confident that it has th ...

Issue with HTML and CSS - dropdown menu is not showing up

My dropdown menu is not appearing, can someone please help me? It should display under "Interno," but when I click on it, nothing shows up! I found this menu online and tried to implement it, but it seems like I am doing something wrong. For the full proje ...

Sending variables to another function in JavaScript

I am looking to transfer the variable "firstWord" along with its saved value from one method to another. var obj = { getWords: function () { var words = []; $('input').each(function () { words.push($(this).val( ...

Why is PHP unable to locate the index sent in the JSON data?

Why can't PHP seem to locate my index, myPostData? Utilizing jQuery/AJAX $('a').on("click", function(){ $.ajax({ type: "POST", url: "../image_view.php", data: {myPostData : {"lastName":"Sperrow", "firstName":"J ...

Include and remove JSON data items within ng-repeat loop

I am in the process of creating a dynamic page where users can add multiple locations to their contact information. Currently, my code looks like this: <div class="input-append" ng-repeat="location in newPartner.partner_location"> <input clas ...