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

Troubleshooting the Problem with CSS Margin in HTML Footer

I'm encountering an issue with the footer on my website. The margin: auto; command doesn't seem to be working for the list items in the footer. I want the items in the footer to occupy one-third of the width, but no matter where I place the marg ...

Troubleshooting issue with ng-change function in AngularJS

I have been working on developing a web application that includes two dropdown lists. My goal is to update the data in dropdown list 2 whenever there is a change in dropdown list 1. <tr> <td> State </td> <td> ...

Looking to extract specific information from a webpage using VBA

I'm trying to extract the text 'Catalog Manager/ Sales' & 'EOL (product/component)' from the view source tab using VBA. Here is an excerpt of the view source code: <tr> <td nowrap="true" valign="top" width="165px" cl ...

Discover the best way to reference a JavaScript variable within an HTML form textfield

I'm having trouble with a script that is supposed to display the selected value from a drop down list in a text field on an HTML form. When I select an option, the value is not appearing in the text field. Can someone please assist me with this issue? ...

Extract information from various webpages with identical URLs

I am struggling to extract data from a specific webpage (). Even though I can successfully gather information from the initial page, whenever I attempt to navigate to subsequent pages, it continues to display the same dataset. It seems to retrieve the iden ...

What could be causing the HTML content to not display when the code is executed?

I've recently started using Visual Studio Code for my project. I am able to build my code without any errors, but when I run it, the HTML content does not display properly - only the CSS styling for the header and footer is visible. I have tried click ...

Why does the styling on my <a> tags in the footer only take effect when I apply padding to the form above?

It seems that when styling the <a> tag for the list in the footer, no changes occur even if correctly applying the style. However, adding padding to the form in the main part of the code causes the links in the footer to show the change. To see this ...

I am having trouble getting bootstrap-icons to work in my Angular project and I'm eager to figure it out

I am having trouble incorporating bootstrap-icons into my angular project. Despite running the npm i bootstrap-icons command, I am unable to successfully add icons using icon fonts on my webpage. As a temporary solution, I have added the bootstrap icon CD ...

After moving a JavaScript application to heroku, the script appears to be nonfunctional

I developed a basic javascript application using mojs, an animation library, and aimed to host it on heroku. To begin with, I attempted the "heroku create" command to deploy the original app on heroku - although the app was accessible, the script did not f ...

The date selection tool is failing to appear on the screen

I successfully created a date picker using Bootstrap. Here is the code: <div class="form-group"> <div class='input-group date' id='datetimepicker1'> <input type='text' class="form-control" /> <s ...

The icons from MaterializeCSS are not displaying correctly on the navbar within an Angular 7 project

Having an issue implementing MaterializeCSS Icons on the navbar. The arrow-drop_down icon is not displaying correctly, showing only text instead. Oddly enough, the icons render properly on other pages except for the app.component.html file. I attempted to ...

What is the best way to horizontally center a div while ensuring the content within the div stays aligned?

I'm attempting to align a div in the center while maintaining the position of its contents. Check out my code snippet: <style> .wrap{ position: relative; } .character{ position: absolute; bottom: -10%; left: 0; heigh ...

Ensure that when adjusting the height of a div, the content is always pushed down without affecting the overall layout of the page

My webpage contains a div element positioned in the middle of the content, with its height being adjustable through JavaScript code. I am seeking a way to manage the scrolling behavior when the height of the div changes. Specifically, I want the content t ...

Issue with Custom Dropdown input not triggering blur event

I've been working on a custom dropup component and have encountered some strange issues with it. The main problem is the blur event not firing when clicking outside of the input field. My goal is to hide the options elements on blur, reset the compon ...

Utilizing JQuery to differentiate a single element within a group of elements

As a beginner in the world of jquery, I am attempting to assign a font awesome icon star (fa-star) to differentiate between admins and regular members within the group members bar. The usernames have been blurred out for privacy reasons, but my goal is to ...

JavaScript program that continuously reads and retrieves the most recent data from a dynamically updating JSON file at regular intervals of every few seconds

I am a beginner in JavaScript and I'm facing an issue with displaying the most recent values from a .json file on an HTML page. The file is updated every 10 seconds, and I am also reading it every 10 seconds, but I'm not getting the latest data. ...

When using a routerlink in an a tag with Bootstrap 4, the navigation tab functionality may not work as expected

I've been working on bootstrap4 tabs and everything is working smoothly until I add a router link to the anchor tag. Strangely, only the hover effect works in this case. Can anyone help me troubleshoot this issue? Below is my code snippet: <link ...

Is it possible to modify the background color of the firefox address bar according to the type of protocol being used?

Is there a way to customize the background color of the address bar in Firefox according to different protocols? For example, using blue for https, orange for mixed content warning, green for ev-certificate, and red for invalid certificates. ...

Ensuring the image is properly sized to fit the screen and enabling the default zoom functionality

I am looking to achieve a specific behavior with an image, where it fits the viewport while maintaining its aspect ratio and allowing for zooming similar to how Chrome or Firefox handle local images. Here are the details of my project: The image I have is ...

Which element can be slotted?

Is it possible to use a tr (table row) for a named slot? What is the outcome when a tr is used as a slot's fallback content? For example: <x-table> <tr slot="x-row"> <td>data</td> <td>data</td> ...