When the browser window is resized, the footer starts to cover the content

To prevent the divs from overlapping when the browser window is resized vertically, take a look at the code snippet below:

<html>
    <body>
        <style>
            #box {
                display: flex;
                flex-direction: column;
                align-items: center;
            }
            #top {
                background-color: red;
                height: 560px;
                width: 400px;
            }
            #bottom {
                background-color: green;
                height: 100px;
                width: 400px;
                position: absolute;
                bottom: 0
            }
        </style>
        <div id="box">
          <div id="top">
          </div>
          <div id="bottom">
          </div>
        </div>
    <body>
</html>

If you're experiencing overlapping divs, there are ways to avoid it while maintaining the structure. The bottom div typically serves as a footer in this scenario. Thank you for your assistance!

Answer №1

To ensure both div elements maintain their heights, apply the min-height property to the parent box element and remove absolute positioning from the bottom element.

Setting margin-top: auto on a flex column item will align it to the bottom of its parent container, especially visible on larger screens.

body {
  margin: 0;
  display: flex;                  /*  Fix for IE bug  */
}

#box {
  flex-grow: 1;                   /*  Expand to fill body width  */

  display: flex;
  flex-direction: column;
  align-items: center;
  min-height: 100vh;
}

#top {
  background-color: red;
  height: 560px;
  width: 400px;
  border: 1px solid black;
}

#bottom {
  margin-top: auto;               /*  Align to bottom on larger screens  */
  background-color: green;
  height: 100px;
  width: 400px;
  border: 1px solid black;  
}
<div id="box">
  <div id="top">
  </div>
  <div id="bottom">
  </div>
</div>


If the containers need to shrink at any point, for example when the red div does, which has a fixed height equal to the viewport height.

This is achieved by setting flex-shrink: 0 on the green div to prevent it from shrinking and maintaining its set height.

html, body {
  margin: 0;
}
#box {
  display: flex;
  flex-direction: column;
  align-items: center;
  height: 100vh;
}

#top {
  background-color: red;
  height: 560px;
  width: 400px;
  border: 1px solid black;
}

#bottom {
  margin-top: auto;
  flex-shrink: 0;
  background-color: green;
  height: 100px;
  width: 400px;
  border: 1px solid black;  
}
<div id="box">
  <div id="top">
  </div>
  <div id="bottom">
  </div>
</div>

Answer №2

To resolve the issue, it is important to ensure that the parent element has a position: relative; set. In this particular scenario, setting it on the body element will address the problem. When the parent's position is specified as relative and the child's position is set to absolute, the child element will be positioned in relation to its parent:

body {
  position: relative;
}

#box {
    display: flex;
    flex-direction: column;
    align-items: center;
}
#top {
    background-color: red;
    height: 560px;
    width: 400px;
}
#bottom {
    background-color: green;
    height: 100px;
    width: 400px;
    position: absolute;
    bottom: 0
}
<div id="box">
  <div id="top">
  </div>
  <div id="bottom">
  </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

Clever method for enabling image uploads upon image selection without needing to click an upload button using JQuery

Is there a way to automatically upload the file without having to click the upload button? Detail : The code below shows an input field for uploading an image, where you have to select the file and then click the "Upload" button to actually upload it: & ...

Prevent automatic scrolling by clicking when you reach the bottom

In order to replicate the issue I am experiencing, please click on the down button four times, and then click on the up button. You will observe that you need to click the up button one extra time for it to function properly. How can I detect when it has r ...

Integrating individual script into HTML

Imagine you have a file named public/index.html. Additionally, there is another html file called otherScript, which contains only <script> tags with a script inside. How can you include these scripts into your public/index.html file? You intend to ...

At times, PHP mail usage may result in certain $_POST arrays being unexpectedly empty

I've encountered an issue while using the php mail function to send data from a html form. The mail function is functioning correctly and I receive the email upon form submission, but occasionally I receive empty arrays in the email content. Here is ...

Identify Pressed Shift Key Direction - Leveraging JQuery

I am currently working on a web application that requires users to enter capital letters. However, I want to restrict them from using the right shift key for certain keys like a, s, d, f and the left shift key for h, j, k, l in order to detect whether they ...

Using Bootstrap to align items to the right in a navigation bar

I've been attempting to align certain elements to the right in the navbar using Bootstrap by utilizing mr-auto, but it doesn't seem to be functioning properly. What steps should I take to ensure it works correctly? Below is a snippet of my index. ...

Convert JSON data into an HTML table using JavaScript without displaying the final result

I need help troubleshooting my JavaScript code that is supposed to populate an HTML table with data from a JSON file. However, the table remains empty and I can't figure out why. Sample JSON Data [{"User_Name":"John Doe","score":"10","team":"1"}, { ...

React Native tutorial: Changing button color on press

When a user clicks on the TouchableOpacity element, I want to change the color of the button from the default gray to blue. Initially, the 'checked={this.state.newProjects}' newProjects variable is not present in the state, so when clicked it sho ...

Angular: monitoring changes in HTML content within a Component or Directive

I have a situation where I am retrieving HTML content from a REST endpoint using a directive and inserting it into a div element using [innerHTML]. Once this HTML content is rendered, I would like to manipulate it by calling a global function. My approach ...

Struggling to create a SVG Line with DOM Manipulation in Typescript

I'm having trouble adding an SVG element to my div using the appendChild function in TypeScript. I want to add a line inside the SVG, but for some reason, I can't see the line output on my browser. There are no errors showing up either. Please he ...

Tips for ensuring a checkbox is ticked before submitting in Bootstrap

I am in the process of creating a form using Bootstrap. I want to ensure that users can only submit the form once they have checked the checkbox.https://i.sstatic.net/piBRA.png <form> <div class="form-group"> <label for=&qu ...

Is it more efficient to employ jQuery with the form submission button?

Would using JQuery for form submission be the optimal choice? For instance: <input type="text" id="username" name="username" maxlength="30" /><br /> <input type="password" id="password" name="password" maxlength="30" /><br /> <i ...

Ensure that the navbar remains within the container as you scroll down the page

With the implementation of bootstrap 4, I have successfully integrated a fixed navbar inside a container. Initially, at the top of the page, it occupies the full width, but as soon as I start scrolling, it smoothly transitions into the confines of the cont ...

Tips for containing `overflow` within a flexbox container

I've organized my body space into three sections: header, content, and footer. To achieve a sticky footer effect at the bottom, I used the flex property in my layout. However, I'm struggling to add a scrollbar to my main container box. I've ...

Show data based on the chosen destination

Recently, I've been working on creating a simple printer manager to monitor the status of all my printers. Although I have successfully displayed all the data, I'm facing an issue while trying to organize it by location. The error message I keep ...

How can I retrieve the width of a responsive React element during its initial rendering phase?

In my React project, there is a component called ResultList which is used to display products in a gallery format. The challenge I'm facing is determining the appropriate number of products to show per row based on the available width for the ResultL ...

Conceal the server's internal file structure to prevent it from being displayed in sources

When I visit my website and inspect the elements in Google Chrome, I am able to see a list of all the folders on my site under the sources tab. This reveals their original names and allows for the downloading of the entire website code. I am concerned abo ...

Display a JSON object on a web browser

I am trying to display a JSON object on a web browser using HTML. The object is already in a text file and has been properly formatted for readability. My goal is to maintain the same formatting when displaying it on the browser. ...

Picture is not displaying properly post-rotation

Is there a way to rotate an image by 90 degrees and display it in a Card Component without it covering the text? Currently, I am utilizing Element.io, but encountering issues with the rotated image overlapping the text. The image currently has the followi ...

Modifying website elements with JavaScript

Can someone assist me with getting a script to work on my website that will allow me to switch between four different sets of content using buttons labeled 1, 2, 3, and 4? I have tried using addClass and removeClass but cannot seem to get it right. Here i ...