Adjusting the height of a div according to the changing heights of other divs

The sidebar contains a search field, two lists, and a text div. The height of the search field and text div always remains constant, while the heights of the two lists vary dynamically.

I am exploring the possibility of using only CSS to make the height of the first div responsive. This would involve adjusting its height based on the browser window size and the content in the second list. The second list can have anywhere from 0 to 20 items with a maximum height of 40% of the entire page.

<style>
   body, html {
       height: 100 % ;margin: 0;padding: 0;
   }

   ul {
       list - style: none;
       margin: 0;
       padding: 0;
   }

   #sidebar {
       max - height: 100 % ;
       overflow: hidden;
       width: 300 px;
   }

   #inputContainer {
       height: 50 px;
       max - height: 50 px;
       overflow: hidden;
       background: #eee;
   }

   #firstList 
   {
       background: #ddd
   }

   #secondList 
   {
       width: 100 % ;
       background: #bbb;
       position: absolute;
       bottom: 120 px;
       width: 300 px;
   }

   #firstList ul 
   {
       max - height: 230 px; /*THIS SHOULD NOT BE A STATIC NUMBER*/
       overflow - y: scroll;
   }

   #secondList ul 
   {
       overflow - y: scroll
       max - height: 40 % ;
       min - height: 200 px;
   }

   #otherBox 
   {
       position: absolute;
       bottom: 0 px;
       background: #333;
          color:# fff;
       height: 100 px;
       width: 300 px;
   }

For a demonstration, I have put together a plunkr showcasing this concept.

Answer №1

For this task, it would be more effective to utilize the flex model.

Relevant CSS:

#sidebar { 
    height:100%; overflow:hidden; width:300px; 
    display: flex;           /* Utilize flex box for container */
    flex-direction: column;  /* Arrange children in a column */
}
#inputContainer {
    flex: 1 1 50px;          /* Able to grow and shrink, fits in 50px */
    background:#eee;
}
#firstList{ 
    flex: 1 1 100%;          /* Can expand, can contract, fills up to 100% available space */
    background:#ddd; 
    overflow-y:scroll;
}
#secondList{
    flex: 0 0 20%;           /* Fixed at 20%, unable to grow or shrink */
    background:#bbb;
    overflow-y:scroll;
}      
#otherBox{
    flex: 0 0 100px;         /* Fixed at 100px, cannot grow or shrink */
    background:#333; color:#fff;
    overflow: hidden;
}

I have made adjustments to your Plunker here: http://plnkr.co/edit/oVB5Mbu4nU58UjYBFPpC?p=preview

You can also view a Fiddle where you can modify the height: http://jsfiddle.net/abhitalks/96h4wmkf/3/

Trust that this information proves useful.

.

Answer №2

To ensure proportional height for each element, assign specific heights to different elements as shown below:

#inputContainer{
    height:5%;
...
}

#firstList ul{
    height:40%; 
...
}

#secondList ul{
    max-height:40%;
...
}

#otherBox{
    ...
    height:15%;
}

UPDATE:

Set your div's height to auto and add overflow property like this:

#firstList ul{
    max-height:230px; /*AVOID USING STATIC NUMBERS*/
    overflow:auto;
    height:auto;
  }

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

Responsive Navigation Menu using Bootstrap Framework for mobile devices

I am encountering an issue with my bootstrap navbar where the button does not appear on the mobile version. This is happening while testing on my Windows Phone. Below is the code for my navbar: <nav class="navbar navbar-default navbar-fixed-top" ro ...

What is the best way to connect a CSS file with multiple pages located within a specific folder in HTML/CSS?

In my CS 205 class project, I am tasked with creating a website. To accomplish this, I used Notepad++ for the HTML files and Notepad for the CSS files. The site consists of an index.html page along with other content pages, each designed separately in Note ...

@page Css not displaying properly on the Mozilla Firefox browser

In order to fulfill my requirement, I need to ensure that there is a consistent 10cm margin on all pages when printing. Printing on my web page involves utilizing the window.print() function. However, due to the dynamic nature of the content, the number o ...

The JQuery Datepicker function consistently returns a null value when attempting to retrieve the selected date

I've encountered a challenge while working on a project recently. Currently, I'm utilizing Spring 3.0 with its integrated validation alongside Hibernate 3.6. My goal is to implement a JQuery datepicker for users to input dates. However, the dat ...

The function Mediarecorder.start() is experiencing issues on Firefox for Android and is not functioning

Recently, I've been facing a peculiar issue while developing a web application. The purpose of this app is to capture about 10 seconds of video intermittently from the device webcam and then upload it to a server. For this functionality, I utilized th ...

Focusing on the alignment of an article tag

I am currently facing a challenge in centering the content within an HTML article element. As of now, the content is aligned to the left margin of the page. Below is the snippet of HTML I am working with: <article> <h1>Sign In</h1> ...

Verifying username using an Ajax call

Currently, I am working on developing a signup form using HTML/CSS/JS that involves utilizing an AJAX request to interact with the server. Within my jQuery code, I have implemented a method for validating the form inputs, which also triggers a function (en ...

After the decimal point, there are two digits

My input field is disabled, where the result should be displayed as a product of "Quantity" and "Price" columns ("Quantity" * "Price" = "Total"). However, I am facing an issue where the result often displays more than 2 digits (for example: 3 * 2.93), desp ...

Creating Flexible Divs with Gradient Background for Older Versions of Internet Explorer - IE8 and IE7

I'm attempting to achieve the following. https://i.stack.imgur.com/YYOZv.jpg The issue I am encountering is that the elements in row1 have a different gradient background compared to those in row2. Additionally, row1 is populated dynamically with c ...

Maintain the height of the image equal to the height of the

I've been facing challenges with adjusting the height of this image to match the div container. I've experimented with various height properties such as max-height, min-height, normal height, auto, and 100%, but none seem to be providing the desi ...

I'm having trouble getting a CSS transition to work on a PNG sequence using jQuery in Mozilla

For my latest project, I decided to create an animation using a PNG sprite sequence. The idea was to add a smooth transition effect on hover and have the animation play in reverse when the hover state ends. If you want to take a look at the code yourself, ...

Issues with Bootstrap glyphicon not functioning correctly on mobile devices and Internet Explorer

Within my template, there is a navigation button positioned on the left side of the page that remains fixed as the user scrolls down. When clicked, this button triggers a menu to appear. Embedded within this button is a bootstrap glyphicon: <div class ...

Databinding with AngularJS inside pre tags

Attempting to demonstrate databinding in a code snippet, but it keeps evaluating within the pre tag! Even when using { and }, it still evaluates. It's almost comical how such a simple issue is proving difficult to find an answer for online. ...

Store text in a table format in your local storage

I need help figuring out how to save product and price information to local storage when an "add to cart" button is pressed. Can someone provide guidance on how to do this? Here is the code I currently have: body> <!-- Header--> ...

What is the best way to create a scrollable Material UI modal and dialog?

Having a problem with my mui modal where the top content is getting cut off and I can't scroll up. I've tried adding the {overflow:"scroll"} property to the modal but it's not working. Here's the code snippet I'm currentl ...

Issues with Bootstrap Carousel Fade Transition and Button Controls inoperable

I am currently exploring the creation of a carousel on bootstrap that transitions smoothly between three images (specifically 3 'gorilla' jpgs). However, I am encountering some challenges while learning HTML. Challenge 1: The carousel is not tra ...

When attempting to use the jsonify method on a variable within an html file, the output is not displayed

Is it possible to store "jsonify" in a variable? I find the pure json format of "return jsonify(data)" unappealing, so I am looking for a way to enhance it using an HTML template with proper indentation. Here's the python code I have: from flask impo ...

What is the best jQuery library to add to my website?

I have included several jQuery scripts on my website for various functionalities such as sticky header, anchors, and animations. I am wondering if it is necessary to include all of them or if I can just include one or two? Here are the jQuery scripts I ha ...

Python HTML parser with advanced CSS capabilities

Currently, I am in need of an HTML parser that is aware of CSS and functions similar to how a browser renders HTML. Specifically, I am searching for the equivalent of element.innerText in DOM-JS. For instance, let's consider the following HTML: < ...

What is the best way to merge 60 related jquery functions into a unified function?

I designed a webpage that serves as an extensive checklist. When you click on the edit button for a checklist item, a modal window opens up. This modal window contains a radio button to indicate whether any issues were found during the check. If "Yes" is s ...