Extending a division's height to match the remaining space within its parent division

There are 3 div elements in my code that I want to style like this:

Below is the HTML code snippet:

<section class="main-window">
    <div id="topdiv"></div>
    <div id="middiv"></div>
    <div id="botdiv"></div>
</section>

And here's the CSS code:

.main-window
{
  vertical-align: middle;
  border: 2px solid gray;
  border-radius: 5px;
  width: 90%;
  height: 70%;
  background-color: White;
  margin: auto;
  position: absolute;
  top: 0; left: 0; bottom: 0; right: 0;
}

#topdiv {
  background-color: beige;
  height: 40%;
  border: 2px solid black;
}

#middiv {
  background-color: lightblue;
  height: 40%;
  border: 2px solid black;
}

#botdiv {
  background-color: lightgreen;
  height: 20%;
  border: 2px solid black;
}

You can view the code on this fiddle link.

While setting the heights of the top two divs to 40% fills the parent div, adding borders to each div slightly exceeds the parent boundaries.

My query is: Is it possible to set the height of the first two divs to 40% and have the third div stretch until the bottom of its parent?

Answer №1

To ensure proper sizing of each child element, be sure to include the following CSS:

box-sizing: border-box;
-moz-box-sizing: border-box;
-ms-box-sizing: border-box;
-o-box-sizing: border-box;
-webkit-box-sizing: border-box;

Check out the live example here.

Additionally, making adjustments to the border can enhance the overall appearance.

View the updated code on Fiddle.

Answer №2

Make sure you have jQuery included in your code and add the following jQuery snippet for the third div:

 $(document).ready(function () {
     $("#botdiv").height($(".main-window").height() - $("#topdiv").height() - $("#middiv").height());
 });

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

Having trouble locating the source of the issue causing the product page to appear too wide on Shopify

Can someone assist me in identifying the issue that is causing the product page on this website () to be too wide? Here is an image highlighting the issue. I made some CSS customizations to make the product gallery full width and added padding to the pro ...

embedding HTML code directly into an iframe

Can HTML be directly inserted into an iframe within my HTML template? Would the following code be considered valid? <iframe> <html> <head> </head> <body> <p>Hello world</p> </body> ...

Displaying a loading spinner using JQuery while content is being loaded into a div

I have a navigation bar with links that load content from corresponding HTML pages into a div next to the navigation bar when clicked. To make it more user-friendly, I want to display a spinner while the content is loading. However, the current code I trie ...

Align the inner div in the center of the outer div, even when the outer div does not

I'm attempting to make sure the tail of the speechbubble aligns with the center of the image, regardless of their respective heights. Essentially, I want the image to always be centered within the current height of the wrapper, similar to how the spee ...

Troubleshooting issue with ng-click functionality in AngularJS within an <li> element

When creating a pagination list image with AngularJS, I encountered an issue where the ng-click directive was not working when used in either an <li> or <a> tag. However, it worked perfectly fine within a <button> tag. <div ng-control ...

Troubleshooting the challenges with jQuery's next().addClass() and prev().addClass() functions

I am attempting to create a slider feature. I have four different contents that I've positioned consecutively, but made them invisible initially. I defined a class called active-client with the attribute display: flex. Using jQuery's addClass() m ...

Unlocking the potential of converting YouTube videos into audio format within a custom HTML player

Hello everyone, I am looking to add an audio player in HTML for a YouTube video. When I click on the play button, I want the YouTube video to play in audio format. Can anyone please assist me with this? <video controls="true"> <sou ...

Tips for adjusting the position of an icon when encountering a line break using Javascript or CSS

After some trial and error, I managed to get it working, but only when the page is initially loaded and not in a responsive manner. Below is the JavaScript code I used. if ( $(".alert-box").height() >= 90 ) { $('.img').css(&apos ...

Include a "Click for more" and "Click to collapse" button on all thumbnails, not limited to just one

I am struggling to make the toggle script below work for all thumbnails in the gallery, not just the first thumbnail. Each card is filled with paragraphs, bullets, and links, making the page overwhelming. That's why I need the "Read More" and "Read Le ...

Align text vertically within Bootstrap carousel area

I'm struggling with centering text both horizontally and vertically in a Bootstrap 4 carousel. I've set up the bootply with a carousel, but the text is stuck in the upper left corner instead of being centered on the screen. <div class="carou ...

Guide to aligning a container to the left in Bootstrap 5

I have a grid container in Bootstrap 5 and I'd like to align it to the left on my webpage. <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3f5d50504b4c4b4b54e42f56b61a0acf40"&g ...

PHP SimpleXML: What is the best way to parse an HTML document?

As I attempt to use simplexml_load_string to load an HTML file as XML, numerous errors and warnings related to the HTML content arise leading to a failed process. Is there a method to effectively load an html file using SimpleXML? The HTML document in que ...

Can we not customize a nested component using the 'styled()' function in MUI?

Looking at the code snippet below, my attempt to style an MUI Stack component within an MUI Dialog component seems to be falling short. The Stack styles are not being applied as expected: const CustomDialog = styled(Dialog)(({ theme }) => ({ ' ...

Real-time webpage featuring dynamically updating text sourced from a file

After spending 5 hours attempting this on my own, I've decided to reach out for help. I am in need of creating a page that will automatically update itself and display the content of a file when it changes. For example, let's say we have a file ...

Creating a clickable link that dynamically updates based on the span and id values, and opens in a new browser tab

I am attempting to create a clickable URL that opens in a new window. The URL is being displayed and updated on my HTML page in the following manner: Python Code: def data_cb(): return jsonify(waiting=await_take_pic()) Javascript Code: jQuery(d ...

Button Element Not Appearing in JavaScript/HTML

My current project involves creating a replication of the Windows calculator to practice JQuery and JavaScript. I have already set up all the HTML buttons for this basic calculator, but I am facing an issue where only the number "1" is displayed when click ...

PHP contact form not properly sending complete submission data

I'm facing an issue with my HTML contact form that utilizes JavaScript for a change function. Essentially, I have a dropdown menu for different subjects, and based on the selected option, specific fields should appear. However, when a user fills out t ...

In Angular 4, the Bootstrap modal now only opens after a double click instead of opening on the first click

Working on an eCommerce application, there is a cart icon that triggers a modal screen displaying user-selected product data when clicked. However, the issue I'm facing is that upon initial page load, the modal screen opens only after double-clicking; ...

Looking for a never-ending whiteboard experience with React that includes zoom, pan, and pinch functionality

Recently, I put together a flow chart with the help of the npm package react-zoom-pan-pinch Issue : One problem I encountered is that the chart has boundaries on the left and top, while appearing infinite on the right and bottom. Is there a way to expan ...

Is tabIndex being used as a css property?

I'm trying to understand how to utilize the HTML attribute tabindex using CSS. Specifically, I want to assign tabIndex=0 to all div elements that have the className='my-checkbox'. This is my current approach: <div tabIndex="0" classNam ...