Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically change div's height to parent

How can I achieve, that the blue box fills the div's remaining place and dynamically decreases his height when the green box gets bigger?

http://jsfiddle.net/trek711/ncrqb/4/

HTML

<div id="wrap">
    <div id="left"></div>
    <div id="righttop">
        <div id="click">Click!</div>
        <div id="box"></div>
        <div id="place"></div>
    </div>
    <div id="rightbot"></div>
</div>

CSS

#wrap {
    padding: 5px;
    width: 700px;
    height: 500px;
    background-color: yellow;
    border: 1px solid black;
}
#left {
    float: left;
    margin: 0 5px 0 0;
    width: 395px;
    height: inherit;
    background-color: red;
}
#righttop {
    float: left;
    padding: 5px;
    width: 290px;
    background-color: green;
}
#rightbot {
    float: left;
    margin: 5px 0 0 0;
    width: 300px;
    height: 60%;
    background-color: blue;
}
#click {
    width: 50px;
    height: 50px;
    background-color: red;
}
#box {
    display: none;
    width: 200px;
    height: 100px;
    background-color: white;
}
#place {
    width: 100px;
    height: 120px;
    background-color: lightgrey;
}

JS

$("#click").on("click", function (e) {
    $("#box").slideToggle("fast");
});

Thank you very much!

like image 717
trek711 Avatar asked Aug 16 '13 11:08

trek711


People also ask

How do I make my div take 100% height of parent div?

Answer: Set the 100% height for parents too And we all know that the default value of the height property is auto , so if we also set the height of <body> and <html> elements to 100%, the resulting height of the container div becomes equal the 100% height of the browser window.

How do I get dynamic height in CSS?

We use css property height: calc( 100% - div_height ); Here, Calc is a function. It uses mathematical expression by this property we can set the height content div area dynamically.

How do you make div width and height auto adjust to content?

Syntax: height: length|percentage|auto|initial|inherit; Property Values: height: auto; It is used to set height property to its default value.


1 Answers

Unfortunately, there's no way you can accomplish this with CSS. However, the slideToggle does have a callback that you can use to resize things as it progresses

$("#box").slideToggle(
        { duration: "fast", 
         progress: function() {
            $('#rightbot').height(
               $('#wrap').height()
               - $('#righttop').height()
               - 15 /* margins */)
       }
  });

And in JSFiddle

The only ugly thing there is the margins. It's probably possible to find those programatically as well, though.

This is the only way to do it that allows for smooth resizing of the blue box

like image 71
Richard Avatar answered Sep 28 '22 01:09

Richard