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

Run JavaScript code last before page unloads

Two pages are involved in this process. On the first page, there is a form that the user completes and then clicks on the submit button. After that, the code for the first page unloads, and the user is redirected to the second page. Actual Issue A loadin ...

Design a food selection with only CSS and HTML

I am working with a menu structure that looks like this: <ul class"menu"> <li> <a>item1</a> <ul> <li><a>subitem1</a></li> <li><a>subitem2</a></li> &l ...

How can you gather user data on a website without relying on a database and making it voluntary?

In order to streamline the process, the user should be able to send a preformatted email with the click of a button or have their data stored securely without leaving the site. The goal is to avoid the extra step of using a mailto: link, unless the email c ...

Navigating through pages in React using Pagination Animations

Attempting to style an active button with a small transition when it receives that attribute has been a challenge. The same issue occurs with the paragraph rendered from the data file. function Pagination() { const [selected, setSelected] = useState(0) ...

Creating a seamless and interactive online platform

I am in the process of designing a website that has a sleek and dynamic layout, rather than just a static homepage. Let me explain further: Here is my current setup so you can understand what I am trying to achieve. By dynamic, I mean that when the page ...

Error message in JS/Ajax alert box

I keep receiving an alert box saying "Image uploaded" even when the variable $imagename is empty. Below is the script in question: <script> function ajax_post1(ca){ var cat = ca; var name = document.getElementById("name").value; var ...

Learning the ins and outs of HTML5 and CSS3

Does anyone have recommendations for high-quality HTML5 and CSS3 tutorials? ...

What is the best way to reference a JavaScript or jQuery variable within a PHP variable?

Is it possible to read a javascript or jquery variable through php code? For example: <script> var num = 3; </script> <php? $a = 20; $b = num*$a; // Is this valid? ?> Any thoughts on this? ...

do not display bullet points

The bullets from the list-style type are not appearing. Despite the CSS code never declaring list-style-type: none, I have specifically declared list-style-type: disc for the section in question. I even checked with Firebug and other tools, and it is evide ...

The timer is malfunctioning

I am new to JavaScript and looking to create a simple countdown. I came across this script: http://codepen.io/scottobrien/pen/Fvawk However, when I try to customize it with my own settings, nothing seems to happen. Thank you for any assistance! Below is ...

Trouble with background image displaying on meteor platform

Hey there! I'm in the process of replicating the HBO login page without functional links, but for some reason, the background image isn't loading properly. I suspect it might be an issue with resizing the image, but I can't pinpoint the exac ...

The styled-components in CSS are causing some issues with the color themes

Link to Image Showing the Issue. I have implemented themes and colors in my React application successfully, but I am encountering a peculiar problem with the labels. Whenever I switch the theme from green to blue and then back to green, focusing on the inp ...

Omit child DIV element in JavaScript and the Document Object Model

I have a situation with two div elements. One is <div class="card" id="openWebsite"> and the other is a sub-division <div class="card__btn"> Here's the issue: When someone clicks on the main div, they get red ...

tips for generating a random number for direct display

Can anyone help me figure out how to automatically display the total of numbers from this script on my blog post without having to click anything? Additionally, I need it to update if the browser is refreshed. ` http://jsfiddle.net/BenedictLewis/xmPgR/ ...

The display of the AngularJS {{variable}} is not supported within a script tag

<script type="text/template"> <div class="abcd"> <div class="efg" data-price="{{::displayPrice}}"></div> </div> </script> I am facing an issue with a template that contains a div element with the da ...

Error: Property 'onclick' cannot be set on a null object

JavaScript isn't my strong suit, so I'm struggling to solve this issue. The console is showing me the following error message: Uncaught TypeError: Cannot set property 'onclick' of null <script> var modal = document.getE ...

Basic Chinese HTML Markup

As a college student, I find myself in the unique situation of taking two seemingly unrelated classes that have unexpectedly merged together - programming and Chinese language. In my C++ class, while learning to code in HTML, I took the opportunity to crea ...

Can we find a method to incorporate multicolored borders?

I currently have a td element with the following CSS styling: td { border-bottom: 3px solid aqua; } I want to be able to click on these cells and change their color, similar to the images linked below: Is there a way to achieve multi-color borders fo ...

Text within the footer section

When displaying the name of a subcategory, I want it in one line. However, if the subcategory name contains two words, it should be displayed in two lines. https://i.stack.imgur.com/CcDeL.png Code: .Footer { width: 100%; display ...

Is it possible to protect assets aside from JavaScript in Nodewebkit?

After coming across a post about securing the source code in a node-webkit desktop application on Stack Overflow, I began contemplating ways to safeguard my font files. Would using a snapshot approach, similar to the one mentioned in the post, be a viable ...