What is the best way to position text messages at the bottom of a chat window?

I am currently working on a chat window, and while setting the height to 100% works perfectly, I am facing difficulty in making the messages appear at the bottom.

HTML:

<div class="d-flex justify-content-center">
  <div class="container container-child">
    <div class="card">
      <div class="card-body">
        <div class="text-left"><span class="badge badge-pill badge-secondary" title="2020-01-01 09:00:00">Hello</span></div>
        <div class="text-right"><span class="badge badge-pill badge-primary" title="2020-01-01 09:00:00">World</span></div>
      </div>
      <div class="card-footer">
        <div class="input-group input-group-sm">
          <input type="text" class="form-control" placeholder="Your message..." autofocus />
          <div class="input-group-append">
            <button type="button" class="btn btn-primary"><i class="fa fa-paper-plane-o fa-fw"></i><span class="d-none d-sm-inline"> Send</span></button>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

CSS:

  .d-flex {
    height: 100%;
  }

  .card {
    height: 100%;
  }

  .card-body {
    overflow-y: auto;
  }

  .badge {
    font-weight: 400;
  }

Screenshot:

https://i.sstatic.net/L5J2U.png

Answer â„–1

To align your text to the bottom, you can convert .card-body into a flex element using display: flex, and then apply margin-top: auto to both .text-left and .text-right.

However, this may cause the right-hand text to move to the left and be on the same line. To fix this, include justify-content: space-between in .card-body and add a margin-bottom of around 20px to .text-left.

You can see this implementation in the code snippet below:

.d-flex {
  height: 100vh; /* Adjusted for Stack example */
}

.card {
  height: 100%;
}

.card-body {
  overflow-y: auto;
}

.badge {
  font-weight: 400;
}

/* NEW */
.card-body {
  display: flex;
  justify-content: space-between;
}
.text-left, .text-right {
  margin-top: auto;
}
.text-left {
  margin-bottom: 20px;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

<div class="d-flex justify-content-center">
  <div class="container container-child">
    <div class="card">
      <div class="card-body">
        <div class="text-left"><span class="badge badge-pill badge-secondary" title="2020-01-01 09:00:00">Hello</span></div>
        <div class="text-right"><span class="badge badge-pill badge-primary" title="2020-01-01 09:00:00">World</span></div>
      </div>
      <div class="card-footer">
        <div class="input-group input-group-sm">
          <input type="text" class="form-control" placeholder="Your message..." autofocus />
          <div class="input-group-append">
            <button type="button" class="btn btn-primary"><i class="fa fa-paper-plane-o fa-fw"></i><span class="d-none d-sm-inline"> Send</span></button>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

Keep in mind that if this is a chat application where messages appear sequentially, you should adjust the margin-bottom of each element by the same value (like 20px in the example) for every new message generated.

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

The fonts appear differently when working on a Mac but show up as Times New Roman when displayed on Windows in development

Currently, I am utilizing react combined with nextjs for server-side rendering and bootstrap. Within my component, I have integrated bootstrap via a CDN: <Head> <title>{title}</title> <meta charSet="utf-8" /> < ...

Set Sprite Canvas Material to match the color of a different element

Can someone please guide me on changing the color of the Sprite Canvas Material? var material = new THREE.SpriteCanvasMaterial( { color: 0xffffff, I am trying to make it match the color of another element based on a specific ID or class. However, I' ...

Instructions on creating a countdown timer that reveals a hidden div once it reaches zero, remains visible for a set period, and then resets

I created a countdown timer that displays a div when the timer reaches zero. However, I am struggling with getting the timer to reset and start counting down again after displaying the div. For instance, if I set the timer for 7 days and it reaches the de ...

Creating a div that becomes fixed at the top of the page after scrolling down a certain distance is a great way to improve user experience on a

I am struggling to create a fixed navigation bar that sticks to the top of the page after scrolling 500px, but without using position: fixed. Despite trying various solutions, none seem to work due to the unique layout of my navigation bar. Strangely enoug ...

Tips for applying styles to elements that are not directly related when the parent element has a specific class using CSS

I have some HTML code structured as shown below. <div class='A'> <div class='B' /> <div class='C' /> </div class='A'> <div class='D' /> My Objective: If A contains C: ...

What causes Firefox (Mobile) to zoom out of my webpage?

After launching my webpage on Google Chrome mobile (android), everything loads perfectly. However, when trying to access the site using Firefox mobile (android), most pages load in a zoomed-out view. The issue is resolved by opening the drop-down menu, but ...

.NET Core global layout using Bootstrap Datepicker

(Currently coding in .NET Core 3.1). I've integrated a bootstrap Datepicker into my _layout. <form class="form-inline ml-3"> <div class="input-group mb-3"> <div class="input-group date" id="datepicker"> ...

What are some ways I can ensure my webpage remains consistent and user-friendly when scrolling, rather than appearing distorted and jumbled?

Is there a way to automatically scroll my page when the window is at a small height, instead of adjusting it to the height by squeezing it? I've tried using overflow in different ways, but haven't been successful in getting the entire page to scr ...

Is it possible to animate captions and slides individually within a Bootstrap 5 carousel?

Beginner coder here, testing my skills with a self-assigned task. I've created a carousel using Bootstrap 5 with three slides, each containing two lines of captions. As the slides transition, the captions move in opposite directions—up and down. Ho ...

How to Perfectly Position Content with Bootstrap 3's Full Width Image Carousel

Here's my query: Is there a way to adjust the position of my content (such as text) inside the slider responsively? The text can be positioned anywhere within the Div Slider, for example, at 25% from the left and 50% from the top within the Image Di ...

What is the best way to navigate my Page while my Dialog Component is displayed?

Is there a way to enable scrolling on the background when a dialog component opens up? By default, scrolling is disabled when the dialog appears. How can we allow scrolling in the background? Here is the code snippet: import React, { useState } from &apos ...

Purifying potentially harmful style value for background-image when using ngStyle with a conditional statement

I'm attempting to dynamically display a background image of a div using ngStyle. I have also set a fallback image in case the default value is null. <div class="card-img-top" [ngStyle]="{'background-image': process.thumbna ...

Tips for creating responsive elements with a z-index of 1 in CSS

I have implemented the code provided in a codepen tutorial, but I replaced the world map image with a USA map. However, the dots are not responsive due to the z-index=1 specified on them. I experimented with vh, vw, and percentages, yet when I resize my s ...

issue with jquery fade toggle not working properly on Bootstrap website

My website is built using Bootstrap and jQuery. I am facing an issue where the fadeToggle() function on a div element is not displaying properly - it remains hidden all the time. Can anyone provide insight into what might be causing this problem, or sugg ...

Creating responsive divs on my webpage

Although I have browsed through various posts with similar questions, none of the solutions seem to work for me. I will make sure to include all relevant information related to my issue and explain why I am unable to resolve it on my own. Currently, I am ...

What causes the behavior of <body> backgrounds to differ from that of other HTML elements?

After examining the HTML code provided: <p style="margin:100px;border:1px solid red;background-color:#dff"> Hello </p> We can observe that when rendered by the browser, it looks like this: https://i.sstatic.net/rADHC.png If we were to mod ...

Setting boundaries for <td> widths: min-width and max-width

Similar Question: Min-width and max-height for table atrributes The <tables> and <td> elements do not recognize the percentage-based attributes min-width or max-width as per specification. How can this be simulated? ...

Styling a PrimeFaces panelGrid within a data table using CSS

I am struggling to add a background color to a panelgrid within a datatable when a hover event occurs. Despite writing the necessary code and CSS, nothing seems to work. Any suggestions on how to address this issue? <p:dataTable id="movementsTable" var ...

Is it feasible to preserve the HTML form content data even after refreshing the page?

Can someone offer a suggestion that doesn't involve using Ajax? I'm wondering if there is a way to achieve this using JavaScript/jQuery or some other method. Here's my issue: When submitting a page, the action class redirects back to the sa ...

Uploading files with Ajax in PHP

Illustration of my Ajax script: Script <script type="text/javascript> $(document).ready(function(){ $('.costExcel').on("submit",function(event){ event.preventDefault() var url = "ajax.php"; ...