Tips for avoiding overflow while utilizing animations

In my current project, I am facing an issue with a container div that has the CSS property overflow: auto which cannot be removed. Inside this container, there is another div that toggles between showing and hiding using an animation triggered by a button click. However, the problem arises when this inner div moves down, causing an overflow within the container. It is important to note that the inner div must remain within the container.

To better illustrate the problem, you can view an example at the following link: https://jsfiddle.net/wg3jzkd6/1/

The desired outcome is as follows:

  • The inner div should move down smoothly without causing any overflow within the container.

Answer №1

@manuel, could you please consider relocating the absolute div within the content div? If possible, try incorporating the overflow: hidden; property on the content div

document.getElementById('button').addEventListener('click', () => {
    const absolute = document.getElementById('absolute-div')
  const absoluteClass = absolute.getAttribute('class')
  
  if(absoluteClass.includes('hidden'))
        absolute.setAttribute('class', 'absolute-div shown')
  else
    absolute.setAttribute('class', 'absolute-div hidden')
})
@keyframes hide {
    from {
        transform: translateY(0);
    }
    
    to { 
        transform: translateY(100%);
    }
}

@keyframes show {

    from {
        transform: translateY(100%);
    }
    
    to { 
        transform: translateY(0);
    }
}

.container {
  position: relative;
  overflow: auto;
  border: 1px solid black;
  height: 80vh;
  width: 40vw;
  margin: 0 auto;
  padding: 1rem;
}

.content {
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  min-height: 100%;
  background-color: green;
  overflow:hidden;
}

.absolute-div {
  background-color: blue;
  min-height: 20%;
  width: 100%;
  position: absolute;
  left: 0;
  bottom:0;
  display:inline;
}

.hidden {
  animation: hide ease forwards 300ms;
}

.shown {
  animation: show ease forwards 300ms;
}
<html>
<body>
  <div id='container' class='container'>
    
    <div class='content'>
    
       <button id='button'>
       SHOW/HIDE
       </button>
       
       <div id='absolute-div' class='absolute-div'>
        Hello world
      </div>
      
    </div>
    
  </div>
</body>
</html>

Hopefully, implementing this adjustment will help address the issue you are encountering...

Answer №2

From my perspective, it seems like you are dealing with a situation where your container has excess overflow. One potential solution could be applying the CSS property overflow: hidden; to your container div.

Answer №3

Simply update the overflow property to hidden

document.getElementById('button').addEventListener('click', () => {
    const absolute = document.getElementById('absolute-div')
  const absoluteClass = absolute.getAttribute('class')
  
  if(absoluteClass.includes('hidden'))
        absolute.setAttribute('class', 'absolute-div shown')
  else
    absolute.setAttribute('class', 'absolute-div hidden')
})
@keyframes hide {
    from {
        transform: translateY(0);
    }
    
    to { 
        transform: translateY(100%);
    }
}

@keyframes show {

    from {
        transform: translateY(100%);
    }
    
    to { 
        transform: translateY(0);
    }
}

.container {
  position: relative;
  overflow: hidden;
  border: 1px solid black;
  height: 80vh;
  width: 40vw;
  margin: 0 auto;
  padding: 1rem;
}

.content {
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  min-height: 100%;
  background-color: green;
}

.absolute-div {
  background-color: blue;
  min-height: 20%;
  width: 100%;
  position: absolute;
  left: 0;
  bottom:0;
}

.hidden {
  animation: hide ease forwards 300ms;
}

.shown {
  animation: show ease forwards 300ms;
}
<html>
<body>
  <div id='container' class='container'>
    <div class='content'>
       <button id='button'>
       SHOW/HIDE
       </button>
    </div>
    
    <div id='absolute-div' class='absolute-div'>
      Hello world
    </div>
  </div>
</body>
</html>

Answer №4

I am puzzled as to why I cannot change the value of overflow: auto. Surprisingly, it seems to work fine when the overflow property is set to hidden.

If altering from auto triggers some unknown effect, I suggest providing a clear explanation for it. However, if there are any issues, they could potentially be resolved by enclosing the entire structure within another div element and isolating it to prevent any unforeseen complications.

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

Understanding the separation and communication techniques in Vue.js components

I recently developed a small vuejs application and I find myself getting confused with the functioning of components. Here is the code snippet that I have been working on: <div id="app"> <div v-if="loggedIn"> <nav> <r ...

Retrieving the following element

I am trying to retrieve the next sibling element of one that contains the text "Yes". Unfortunately, I can only use the text "Yes" and part of the id attribute, as the number (e.g.. 106) is not available. Due to this limitation, I am unable to directly acc ...

Unexpected vertical line in MUI5 Stepper appears when invoking from a function

Greetings! I'm currently working on developing a MUI5 vertical stepper, but I've encountered an issue when rendering steps from a function. Specifically, there is an extra connector line appearing on top of the first step. If you'd like to ...

Implementing pagination within an Angular 11 Mat-table with grouping feature

Encountering an interesting issue with MatTable pagination and grouping simultaneously. I have two components each with a Mat-table featuring Pagination+Grouping. ComponentOne functions smoothly without any issues. When choosing to display 5 elements pe ...

Having an issue with my code in angular 12 where I am unable to successfully call an API to retrieve a token, and then pass that token to another API for further processing

Here is the code snippet containing two methods: getToken and validateuser. I am fetching the token from getToken and passing it as a parameter to validateuser. However, before retrieving the token, my second API call is being executed. ...

The hover effect on the menu button is not covering the entire button when the screen is resized

It appears that I am encountering a problem with the hover functionality. When my screen is at full size, hovering over a menu option causes the entire background color to change. However, upon resizing the screen, only a part of the background color chang ...

Issues are arising with the .mouseover functionality within this particular code snippet

Learning Javascript has been a challenge for me so far. I tried following a tutorial, but the result I got wasn't what I expected based on the video. I'm wondering why that is and how I can fix it. I'm aiming to make a box appear suddenly w ...

How to Keep Bootstrap 3 Accordion Open Even When Collapsed

I am in the process of building an accordion group with bootstrap 3. Here is the code I have so far: <div id="accordion" class="panel-group"> <div class="panel panel-default"> <div class="panel-heading"> <div class ...

AngularJS filter that retrieves only values that have an exact match

In my AngularJS application, I have a checkbox that acts as a filter: Here is the data being used: app.controller('listdata', function($scope, $http) { $scope.users = [{ "name": "pravin", "queue": [{ "number": "456", "st ...

@keyframes shimmering-fade

I'm attempting to create a text animation effect (please see video) but I'm struggling to find the solution!! Can someone assist me with this? Should I use JavaScript for a better result? h1.fadeinone { animation: fadeinone 10s;} h1.fadeintwo ...

Maintain line breaks in the scanner input within an HTML element

Currently, I am utilizing a Zebra DS9908 scanner to scan a barcode and transfer the data onto an HTML page. However, I am encountering an issue with preserving all input characters. Despite attempting both a <div> and <textarea>, the line feed ...

How is the height and width of the header determined?

When examining the theme by clicking "inspect element" on the green portion (<header> ), you will notice that the height and width properties have pixel values that change dynamically as the window is resized. So, where do these values originate fr ...

The hierarchy of CSS in Vue components

I've developed a customized CSS framework to streamline the design process across all my internal projects. The central file is structured as shown below: @import "~normalize.css/normalize.css"; @import "_variables.scss"; @import "_mixins.scss" ...

"Ensuring a DIV element has a height of 100% within

Is there a way to set and enforce a div's height to be 100% of the browser window, another div, or a table cell without resorting to complicated hacks found on Stack Overflow or other websites? ...

creating and managing multiple redirect routes in vue.js application

const router = new VueRouter({ routes: [ {path: '*', redirect: '/'} } )} I have configured this as the default redirect route in my routes.js file. Now, I am wondering how I can set multiple redirect paths and trigger them ...

Designing tables and image galleries using HTML, CSS, and jQuery: A guide

How can I easily transform tabular data into thumbnails with the click of a button? I need a 2-view system that switches between table and thumbnail views without including image thumbnails. I welcome any creative suggestions! :) Examples: ...

The UI does not display the html code (specifically svg) added through jquery appending

I am working with the following HTML code: <svg> <g> <rect></rect> <text> abc </text> </g> <g> <rect></rect> <text> def </text> </g& ...

How can I switch the visibility of two A HREF elements by clicking on one of them?

Let me break it down for you in the simplest way possible. First off, there's this <a href="#" id="PAUSE" class="tubular-pause">Pause</a> and then we have a second one <a href="#" id="PLAY" class="tubular-play">Play</a> Al ...

Challenges arising from the offset in an overflowing Div

Encountering issues with JQuery Offset when utilized within a div that has a fixed height and overflow. This particular div contains two columns, a main column and a sidebar. The objective is to have one of the sidebar divs scroll within its container unt ...

Difficulty in vertical alignment of inline-block elements

Greetings, I am currently working on creating a Resume in HTML. However, I seem to be facing an issue with the inline-block property as the two div elements that should be placed next to each other are not displaying as expected - one of them appears sligh ...