Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS way to adjust a div's height based on another div

Tags:

html

css

I have a div that contains a flexible-area, popup-area and a footer.

  • Flexible-area and footer are always present.

  • Popup-area is 0, 40px, or 60px, since its contents are dynamically changed with js.

  • Flexible-area should fill all the space above the footer.

Is there a css-only method to allow the flexible-area to increase/decrease its height based on the size of the popup?

With the current css I have, when I delete the popup in the jsfiddle, flexible does not expand to fill the area.

I'm trying to see if I can do this without jquery or flexbox for max compatibility.

Thanks for your suggestions/comments!

https://jsfiddle.net/L8me7fLk/

div {
  color: #ffffff;
}
#container {
  height: 100vh;
  width: 100vw;
}
#flexible {
  background-color: red;
  width: 100vw;
  min-height: 68vh;
  max-height: 80vh;
}
#popup {
  width: 90%;
  max-height: 60px;
  min-height: 40px;
  line-height: 20px;
  overflow-y: scroll;
  position: absolute;
  bottom: 0;
  margin-bottom: 50px;
  background-color: #cccccc;
}
#footer {
  width: 90%;
  height: 50px;
  background-color: #666666;
  position: absolute;
  bottom: 0;
}
<div id="container">
  <div id="flexible">
    1content
    <br/>2content
    <br/>3content
    <br/>
  </div>
  <div id="popup">
    1popup
    <br/>2popup
    <br/>3popup
    <br/>4popup
    <br/>5popup
    <br/>
  </div>
  <div id="footer">
    footer
  </div>
</div>
like image 803
Tien Yuan Avatar asked Aug 28 '16 19:08

Tien Yuan


People also ask

How do I change the height of a div dynamically?

The content height of a div can dynamically set or change using height(), innerHeight(), and outerHeight() methods depending upon the user requirement.

How do you set the height of the element to match the height of another element?

Extending div to correct height, or lets say 100% height of the container, You have to chain height:100% up to the container with fixed height or to the body with another height: 100%; .

How do I make div height equal to another div?

The two or more different div of same height can be put side-by-side using CSS. Use CSS property to set the height and width of div and use display property to place div in side-by-side format. The used display property are listed below: display:table; This property is used for elements (div) which behaves like table.


2 Answers

The desired result can be achieved with flexbox.

Firstly, it is a good idea to eliminate <br/>s from the code. You can place these items in <div>s instead.

...
<div>1content</div>
<div>2content</div>
<div>3content</div>
...

On the container, you can use flexbox to tell it to behave as a column:

#container {
  display: flex;
  flex-flow: column wrap;
}

This will similarly be required for the children, because the layout of contents of flexible, popup, and footer are columns.

Now the most important part. The flexbile div should take up the available space, while the div underneath it should not. Therefore, we can apply the following:

#flexible {
  display: flex;
  flex: 1 0 auto;
}

#popup {
  display: flex;
  flex: 0 1 auto;
}

This tells flexible to use available space, and popup not to. For more information on how the flex selector works, see https://developer.mozilla.org/en/docs/Web/CSS/flex

JS Fiddle example

like image 88
alanbuchanan Avatar answered Oct 14 '22 10:10

alanbuchanan


    <html>
    <head>
        <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=2.5; user-scalable=YES" />
        <meta http-equiv="Content-Type" content="text/html; accept-charset=UTF-8">
    <style>
    .EqHeightDiv{
        float:left;
border:solid 1px #ccc;
background:#0CF;
margin:10px;
max-width:300px; 
padding:10px;
}
    </style>

    <script src="http://code.jquery.com/jquery-latest.js"></script> 
    <script type="text/javascript"> 
    function equalHeight(group) { 
        tallest = 0; 
        group.each(function() { 
            thisHeight = $(this).height(); 
            if(thisHeight > tallest) { 
                tallest = thisHeight; 
            } 
        }); 
        group.height(tallest); 
    } 

    $(document).ready(function(){ 
        equalHeight($(".EqHeightDiv")); 
    }); 
    </script>
    </head>

    <body>
    <div class="EqHeightDiv">Here is some stuff</div>
    <div class="EqHeightDiv">Here is some stuff Here is some stuff Here is some stuff Here is some stuff Here is some stuff Here is some stuff Here is some stuff Here is some stuff Here is some stuff Here is some stuff Here is some stuff Here is some stuff Here is some stuff Here is some stuff Here is some stuff Here is some stuff Here is some stuff Here is some stuff Here is some stuff Here is some stuff Here is some stuff Here is some stuff Here is some stuff Here is some stuff Here is some stuff Here is some stuff Here is some stuff Here is some stuff Here is some stuff Here is some stuff Here is some </div>
    <div class="EqHeightDiv">Here is some stuff</div>
    </body>

    </html>
like image 25
NK Chaudhary Avatar answered Oct 14 '22 12:10

NK Chaudhary