Adjusting the height of one div to match the height of another div

I am facing a challenge in making a div (overlay) float over 3 other divs. It slightly overlaps the 1st (horsebanner) and 3rd (footer) divs while completely covering the 2nd (midbackground). My goal is to make the 2nd div automatically expand as the floating div grows, maintaining a consistent overlap with the 3rd div.

Below is the HTML code:

 <body>
    <div id="navigation"> 
    </div>
    <div id="main">
        <div class="overlay">
        </div>

        <div class="horsebanner">
        </div>

        <div class="midbackground">
        </div>

        <div class="footer">
        </div>
    </div>
 </body>

And here is the CSS code:

#main {
width: auto;
height: 650px;
background-color: #FF6;
margin-top: 0;
}

#main .horsebanner {
width: auto;
height: 150px;
background-color: #F90;
margin-top: 0;  
}

#main .midbackground {  
width: auto;
height: 450px;
background-color: #CCC;
margin-top: 0;
}

#main .footer { 
width: auto;
height: 50px;
background-color: #333;
margin-top: 0;
}

#main .overlay {
width: 300px;
height: 100px;
margin-left:100px;
margin-right:100px;
background-color:#0F0;
position: absolute;
}

Being new to the world of HTML, I would appreciate any guidance on how to achieve this. Essentially, my aim is for the midbackground DIV to expand proportionally to the overlay DIV for consistent overlapping with the third DIV.

Answer №1

To maintain a static size for the entire structure based on the viewer's window size, you need to set specific percentage heights for each section. For instance, in this example, I allocated 26% for the top, 48% for the middle, and 26% for the footer. Using jquery, I centered the overlay horizontally by adjusting its margin-top to 25% of the page when loading or resizing (you can modify these percentages as needed).

Below is the CSS code with the necessary adjustments:

#main {
width: auto;
background-color: #FF6;
margin-top: 0;   
}

#main .horsebanner {
width: auto;
height: 26%;
background-color: blue;
margin-top: 0;  
}

#main .midbackground {  
width: auto;
height: 48%;
background-color: #CCC;
margin-top: 0;
}

#main .footer { 
width: auto;
height: 26%;
background-color: red;
margin-top: 0;
}
.overlay {
width: 50%;
height: 50%;
margin-left: 25%;
background-color:#0F0;
position: absolute;
}

Here is the jQuery script to ensure consistency in sizes when resizing the window:

$(document).ready(function(){
$('#main').height($(this).height());
$('.overlay').css('margin-top',$('body').height()/4);
});

$(window).resize(function(){
$('#main').height($(this).height());
$('.overlay').css('margin-top',$('body').height()/4);
});

For better understanding, refer to the jsfiddle link provided: http://jsfiddle.net/pc8Rs/3/

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

Removing sort icon from the first column of a DataTable using jQuery

Is there a way to completely disable the sorting property in the dataTable API? I tried using bsort:false to disable sorting in all columns, which works fine, but the first column still displays the sort icon. Why is that? $("#reportgrid").DataTable({ ...

Submitting a form using ajax to send form data to php without reloading the page

Could anyone please help me figure out why my code is not functioning properly? I am trying to send form data to PHP using the post method, but my PHP script is not receiving any requests. Below is the code snippet for my signup form located in signupmodal ...

This piece of code is causing my browser to lag

I am encountering an issue with fetching and adding values from a weather API to my HTML elements. My goal is to display the hourly forecast for today, starting from the current hour until midnight of the next day. In order to optimize space, I implemente ...

Experimenting with media queries but struggling to make them function properly

I am relatively new to CSS and I am experimenting with media queries. Despite following advice on various Stackoverflow posts and other websites, I am unable to make them work. My goal is simple - to change the color of boxes when the screen width falls be ...

Errors in AJAX calls can sometimes result in an "ERR_EMPTY_RESPONSE" being

I'm currently working on a small CMS/Social network project for a school, which is quite complex and heavily relies on AJAX technology. Randomly, I encounter issues where calls are blocked and the browser displays an error message like net :: ERR_EMPT ...

Exploring the power of jQuery closures and handling events like mouseover and mouseout

I'm currently grappling with the concept of utilizing closures in conjunction with jQuery event functions. The challenge I am facing involves creating rounded shapes on the screen that stop and fade when hovered over, then resume fading when the mous ...

Has CSS3 been recognized as an official standard?

Can you confirm whether CSS3 has been officially recognized as a W3C standard or is it currently in the status of "CR" (Candidate Recommendation)? ...

In the event that the final calculated total is a negative number, reset it to zero. Inform the user of an error through the use of a prompt dialog box

I'm having trouble getting the grand total to display as 0 when I enter all amounts in positive values and then change the unit prices to negative values. However, it works fine when I only enter negative values throughout. Can someone please help me ...

How can we have a div stay at the top of the screen when scrolling down, but return to its original position when scrolling back up?

By utilizing this code, a div with position: absolute (top: 46px) on a page will become fixed to the top of the page (top: 0px) once the user scrolls to a certain point (the distance from the div to the top of the page). $(window).scroll(function (e) { ...

Ways to stop your browser from opening a new window when it is unable to recognize the address

When using a custom protocol to open a Windows application from my web application, everything works correctly without any issues. However, if the Windows application is not installed on the PC, a new window opens displaying "The address wasn't unders ...

The utilization of the jquery.form.js plugin is resulting in my form being submitted twice

In order to enable Ajax file uploads in my ASP.Net MVC project, I am utilizing the jquery plugin known as "jquery.form.js" (http://malsup.com/jquery/form/#getting-started). However, I am encountering an issue where the controller action responsible for han ...

Arranging information within the Ngb-Accordion

Welcome to the code snippet showcasing how to create an accordion in Angular: <ngb-accordion [closeOthers]="false" activeIds="0"> <ng-container class="card" *ngFor="let post of Posts"> <ngb-panel title="{{post.title}} - ...

Import CSS and JS files into a Node.js project

I've been diving into Node.js and I've hit a roadblock. I'm facing an issue with loading css and js files in my project. Despite successfully rendering Bootstrap and Fontawesome from a CDN, my custom css and js files refuse to load. This is ...

Favicon glitch occurred following the inclusion of Google Adsense

Is there anyone out there who can help me with this issue? I recently entered into a partnership with Google and, as part of the process, had to insert the Google AdSense code onto my website in order to be verified and show ads. Prior to this partnership ...

Creating dynamic HTML menus, including sub-menus, through PHP programming

Looking for HTML files in a specific folder to generate a dynamic menu and sub-menus. For instance, project files like 'project1_qa_qa2.html', 'project2_dev_dev1.html', etc. should be parsed using explode() to extract 'project1&ap ...

Display unique JQuery special characters

I'm looking to modify this specific text Hello Mr ABC so that it appears as: Hello "Mr ABC". The text Mr ABC includes a unique non-printable character " ". P.S: Where can I find more information on this topic? ...

Determining the equation from a string and converting it to an integer

I'm working with a table that contains formulas. For example, the formula is: (GP1/(GP1 + GP2))*100%. For my code using jQuery, I need to replace GP1 with the value from textbox1, GP2 with the value from textbox2, and remove the % symbol. My attemp ...

modifying input field with radio button selection in Jquery

Is there a way to set the input field to have a default value and then change it, or disable it with a different value? $("#tax").value(19).prop("disabled",false); $("#tax").value(0).prop("disabled",true); <script src="https://ajax.googleapis.com/aj ...

Navigating to a new page by clicking a button

I am trying to redirect to a different web page when a button is clicked. Below is the code snippet I am working with: JavaScript code snippet: app.controller('myCtrl', ['$scope', '$location', function($scope, $location) { ...

Ways to modify the data within a column for every row in a table without relying on props in Vue.js (specifically Element-ui)

I've been stuck on this issue for quite some time now. I've created a table with 3 columns, and for the first two columns, I can easily display the contents of each row using the prop property. However, for the third column, I need to display inf ...