Creating a sticky positioning effect in CSS

Can someone assist me in implementing the position: sticky as demonstrated below?

Currently, the upcoming date overlaps the current date.

This causes the opacity and shadow of the date to reach 100%, which can create a cluttered appearance if there are multiple dates present.

I would like the previous date to scroll up and make way for the next one.

All of this should be achieved using CSS only.

JS Fiddle Link

        * {
            margin: 0px;
            padding: 0px;
        }
        
        .chat {
            overflow: auto;
            border: solid 1px black;
            position: fixed;
            left: 50%;
            top: 50%;
            background-color: #e5ddd5;
            z-index: 100;
            height: 500px;
            margin-top: -200px;
            width: 500px;
            margin-left: -300px;
        }
        
        .box {
            width: 300px;
            margin: 30px auto;
            padding: 20px;
            text-align: center;
            font-weight: 400;
            color: black;
            font-family: arial;
            position: relative;
            border-radius: 20px;
        }
        
        .box.enviado {
            background: #dcf8c6;
        }
        
        .box.recebido {
           background: white;
        }
        
        .recebido:before {
            content: "";
            width: 0px;
            height: 0px;
            position: absolute;
            border-left: 10px solid white;
            border-right: 10px solid transparent;
            border-top: 10px solid white;
            border-bottom: 10px solid transparent;
            left: 19px;
            bottom: -19px;
        }
        
        .enviado:before {
            content: "";
            width: 0px;
            height: 0px;
            position: absolute;
            border-left: 10px solid transparent;
            border-right: 10px solid #dcf8c6;
            border-top: 10px solid #dcf8c6;
            border-bottom: 10px solid transparent;
            right: 19px;
            bottom: -19px;
        }
        
        .data {
            background-color: rgba(225, 245, 254, 0.92);
            color: rgba(69, 90, 100, 0.95)!important;
            padding: 5px 12px 6px 12px!important;
            border-radius: 7.5px!important;
            box-shadow: 0 1px 0.5px rgba(0, 0, 0, 0.13)!important;
            text-shadow: 0 1px 0 rgba(255, 255, 255, 0.4)!important;
            margin-bottom: 8px!important;
            margin-top: 8px!important;
            margin-right: auto!important;
            margin-left: auto!important;
            max-width: 75px;
            opacity: 0.8;
            z-index: 2;
          }

         .data {
             top: 10px;
             position: sticky;
         }
  <html> 
   <head>
       <meta charset="utf-8">
       <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
      <title></title>
      <meta name="description" content="sticky">
      <meta name="viewport" content="width=device-width">
    </head>
    
    <body>
        <div class="chat">
            <div class="data">05/03/2019</div>
            <div class="box recebido">Olá</div>
            <div class="box enviado">Oi, tudo bem ?</div>
            <div class="data">06/03/2019</div>
            <div class="box recebido">Tudo bem!</div>
            <div class="box recebido">e voce ?</div>
            <div class="box enviado">Tudo bem tambem</div>
            <div class="box recebido">preciso de ajuda</div>
            <div class="box recebido">Voce pode me ajudar</div>
            <div class="data">07/03/2019</div>
            <div class="box enviado">Talvez sim</div>
            <div class="box enviado">O que voce precisa</div>
            <div class="box recebido">Como posso utilizar o position:sticky ?</div>
            <div class="box enviado">Deixe-me ver</div>
            <div class="box enviado">Acho que posso te ajudar</div>
            <div class="box recebido">Certo</div>
        </div>
   </body>  
   </html>

Answer №1

To prevent overlapping, it is recommended to use multiple containers where each date and its corresponding messages are wrapped within the same container. By doing this, the previous day will scroll up before the next one becomes sticky.

* {
  margin: 0px;
  padding: 0px;
}

.chat {
  overflow: auto;
  border: solid 1px black;
  left: 50%;
  background-color: #e5ddd5;
  z-index: 100;
  height: 500px;
  width: 500px;
}

.box {
  width: 300px;
  margin: 30px auto;
  padding: 20px;
  text-align: center;
  font-weight: 400;
  color: black;
  font-family: arial;
  position: relative;
  border-radius: 20px;
}

.box.enviado {
  background: #dcf8c6;
}

.box.recebido {
  background: white;
}

.recebido:before {
  content: "";
  width: 0px;
  height: 0px;
  position: absolute;
  border-left: 10px solid white;
  border-right: 10px solid transparent;
  border-top: 10px solid white;
  border-bottom: 10px solid transparent;
  left: 19px;
  bottom: -19px;
}

.enviado:before {
  content: "";
  width: 0px;
  height: 0px;
  position: absolute;
  border-left: 10px solid transparent;
  border-right: 10px solid #dcf8c6;
  border-top: 10px solid #dcf8c6;
  border-bottom: 10px solid transparent;
  right: 19px;
  bottom: -19px;
}

.data {
  background-color: rgba(225, 245, 254, 0.92);
  color: rgba(69, 90, 100, 0.95)!important;
  padding: 5px 12px 6px 12px!important;
  border-radius: 7.5px!important;
  box-shadow: 0 1px 0.5px rgba(0, 0, 0, 0.13)!important;
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.4)!important;
  margin-bottom: 8px!important;
  margin-top: 8px!important;
  margin-right: auto!important;
  margin-left: auto!important;
  max-width: 75px;
  opacity: 0.8;
  z-index: 2;
}

.data {
  top: 10px;
  position: sticky;
}
<div class="chat">
  <div>
    <div class="data">05/03/2019</div>
    <div class="box recebido">Olá</div>
    <div class="box enviado">Oi, tudo bem ?</div>
  </div>
  <div>
    <div class="data">06/03/2019</div>
    <div class="box recebido">Tudo bem!</div>
    <div class="box recebido">e voce ?</div>
    <div class="box enviado">Tudo bem tambem</div>
    <div class="box recebido">preciso de ajuda</div>
    <div class="box recebido">Voce pode me ajudar</div>
  </div>
  <div>
    <div class="data">07/03/2019</div>
    <div class="box enviado">Talvez sim</div>
    <div class="box enviado">O que voce precisa</div>
    <div class="box recebido">Como posso utilizar o position:sticky ?</div>
    <div class="box enviado">Deixe-me ver</div>
    <div class="box enviado">Acho que posso te ajudar</div>
    <div class="box recebido">Certo</div>
  </div>
</div>

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

Moving from the end to the beginning with a jQuery slider transition

Instead of relying on external plugins, I built this slider from scratch: function customSlider(selector, interval, index) { var slider = this; this.ind = index; this.selector = selector; this.slides = []; this.activeSlide = 0; this.amount; ...

HTML script tag failing to load

As a beginner in using scripts in HTML, I'm currently following a tutorial that suggests loading the script after the body for certain reasons. In my own code, I found that I need to place the script both in the head section and after the body. Remov ...

How can I correct the code that is running both the if and else statements simultaneously?

In the code snippet below, a comparison is made between a username and password. If both are correct, the user is redirected to another page. However, there seems to be an issue where both the if and else statements are executing. Code: if (isset($_POST[ ...

Aligning a child element in the center along the horizontal axis within a parent container that is smaller in width than

I have been attempting to center an <img> element horizontally within a <div> that is narrower than the image itself. The <div> has overflow: hidden applied to it. For example, if the image is 500px wide and the DIV is 250px wide, is the ...

Unsuccessful translation of HTML symbol codes within a toggle function using jQuery

Is there a way to toggle between the symbols + and - using HTML entity codes &#43; and &#45;? I have attempted different solutions for toggling HTML content, but none seem to work correctly with these specific codes. It toggles once, but doesn&apo ...

Parallax scrolling in all directions

Is there a resource available for learning how to program a website similar to the one at ? I am familiar with parallax but can't seem to find any examples that resemble what they have done on that site. ...

How come inactive links are still able to be clicked on?

I have been experimenting with various methods to disable links, but I seem to be unable to prevent them from being clickable. While I was successful in changing the cursor to 'not-allowed' when hovering over the disabled link, it still remains c ...

Tips for minimizing padding and margin in a Bootstrap template

I recently downloaded the page from "https://getbootstrap.com/docs/5.3/examples/heroes/", and I've removed all sections in index.html except for the "hero" section and the "sign-up form". However, there is too much white space between these two elemen ...

Styling the body element in Material UI: A guide to customizing the

I'm having trouble figuring out how to target the root elements using the ThemeProvider in Material UI. const theme = createMuiTheme({ palette: { background: { default: "#00000", backgroundColor : 'black', b ...

Strange occurrences with the IMG tag

There seems to be an issue with my IMG element on the webpage. Even though I have set the height and width to 100%, there is some extra space at the end of the image. You can view the problem here: I have also included a screenshot from the developer too ...

Modifying the title of a webpage with a Bootstrap design

As a newcomer to HTML/CSS, I recently utilized a Bootstrap theme from Themezy to build my personal website. The template includes a navigation bar with tabs labeled Top, Work, Portfolio, and Contact. I felt the need to change Top to About, so I replaced ...

Does the Width Toggle Animation fail to function in FireFox when using jQuery?

Has anyone else encountered this issue with the jQuery animate function not working in Firefox but working fine in IE8, Opera, and Chrome? I'm experiencing this problem and wondering if there's a workaround to make it work in Firefox as well. ht ...

Troubleshooting a Dynamic Navigation Bar Problem with jQuery Integration

Having developed a responsive navbar, an issue arises during iPad breakpoints where attempting to close the active navbar by clicking the hamburger icon triggers the jQuery script to also perform .removeClass(".active").slideToggle() on two buttons, causin ...

What is the best way to create a seamless transition for background-image animations

I have a div where I am applying a class that alters the background image: $('div').addClass('specialBackground'); However, the transition between the background images is too abrupt. I want to achieve a smooth fade effect between the ...

A guide on seamlessly transitioning from a mobile website to the corresponding native app

I am currently working on a mobile website project. This website is built using basic HTML and is accessed through a URL on a web browser, not as a native app or through PhoneGap. The client has requested links to their Facebook, Pinterest, YouTube, Twitt ...

Align the center element vertically on a website with a Bootstrap 4 navigation bar

I am trying to vertically center an element on the page. It works fine until I add a Navbar, which causes the element to be centered with an offset from the height of the Navbar. How can I center the content container vertically while accounting for the Na ...

Enhancing security in client-server communication using HTML5 Ajax

Thank you for your assistance today. I am currently in the process of developing an HTML5 game and require guidance on the most effective method for Client Server communications. I am exploring alternatives to socket.io for undisclosed reasons. The key p ...

`I am facing issues with class binding in Vue 3 when using SwiperJS`

Currently, I am working with Vue 3 along with swiperjs. I have encountered a problem related to the v-bind:class behavior in my project. The issue arises when transitioning to the second slide, where the !h-auto class does not get applied as expected. St ...

Creating a series of scalable svgs of uniform size in a row, enabling me to resize them based on width without disrupting the overall design

I need a row of equally sized SVGs with some text below them, like a navigation bar but larger. I almost have it working, but there are some issues with my current setup. To clarify, this is what I am aiming for: The SVGs should be responsive and scale a ...

Angular 10 Reactive Form - Controlling character limit in user input field

I'm currently developing an Angular 10 reactive form and I am looking for a way to restrict the maximum number of characters that a user can input into a specific field. Using the maxLength Validator doesn't prevent users from entering more chara ...