Jak sprawić, by div zajmował pozostałą wysokość?

Mam taki problem, mam dwa divy:

<div style="width:100%; height:50px;" id="div1"></div>
<div style="width:100%;" id="div2"></div>

Jak sprawić, by div2 zajmował pozostałą wysokość strony?

Author: thirtydot, 2011-08-25

11 answers

Użyj absolutnego pozycjonowania:

#div1{
    width: 100%;
    height: 50px;
    background-color:red;/*Development Only*/
}
#div2{
    width: 100%;
    position: absolute;
    top: 50px;
    bottom: 0;
    background-color:blue;/*Development Only*/
}
<div id="div1"></div>
<div id="div2"></div>
 57
Author: Alexander Rafferty,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-03 23:15:27

Możesz użyć tego http://jsfiddle.net/Victornpb/S8g4E/783/

#container {
    display: table;
    width: 400px;
    height: 400px;
}
#container > div{
    display: table-row;
    height: 0;
}
#container > div.fill{
    height: auto;
}

Po prostu zastosuj klasę .fill do dowolnego z dzieci, aby następnie zająć pozostałą wysokość.

<div id="container">
    <div>
        Lorem ipsum
    </div>
    <div>
        Lorem ipsum
    </div>
    <div class="fill">   <!-- this will fill the remaining height-->
        Lorem ipsum
    </div>
</div>

To działa z liczbą dzieci, które chcesz, nie jest wymagane dodatkowe znaczniki.

 21
Author: Vitim.us,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-12-03 17:52:55

Demo

Jednym ze sposobów jest ustawienie div na {[1] } i nadanie mu góry 50px i dołu 0px;

#div2
{
    position:absolute;
    bottom:0px;
    top:50px
}
 13
Author: Joseph Marikle,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-06-20 09:12:55

Ponieważ wiesz, ile pikseli zajmuje poprzednia zawartość, możesz użyć funkcji calc():

height: calc(100% - 50px);
 8
Author: herman,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-02 18:39:37

Z tabelami CSS, możesz zawinąć div wokół dwóch, które tam masz i użyć tej struktury css / html:

<style type="text/css">
.container { display:table; width:100%; height:100%;  }
#div1 { display:table-row; height:50px; background-color:red; }
#div2 { display:table-row; background-color:blue; }
</style>

<div class="container">
    <div id="div1"></div>
    <div id="div2"></div>
</div>

Zależy jednak od tego, jakie przeglądarki obsługują te typy wyświetlania. CHYBA NIE IE8 i niżej. EDIT: Scratch that-- IE8 does support CSS tables.

 5
Author: Brendan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-08-25 23:40:57

Możesz użyć

display: flex;

Właściwość CSS, jak wspomniano wcześniej przez @ Ayan, ale stworzyłem działający przykład: https://jsfiddle.net/d2kjxd51/

 4
Author: Cezary Tomczyk,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-03 13:43:14

Sam stanąłem przed tym samym wyzwaniem i znalazłem te 2 Odpowiedzi za pomocą właściwości flex.

CSS

.container {
  display: flex;
  flex-direction: column;
}

.dynamic-element{
  flex: 1;
}
 4
Author: Muhammad Omar ElShourbagy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-01-04 09:51:01

Próbowałem z CSS, i lub trzeba użyć display: table lub trzeba użyć nowego css, który nie jest jeszcze obsługiwany w większości przeglądarek (2016).

Więc napisałem wtyczkę jquery, aby zrobić to za nas, cieszę się, że mogę się nią podzielić:

 
//Credit Efy Teicher
$(document).ready(function () {
            $(".fillHight").fillHeight();
            $(".fillWidth").fillWidth();
        });

        window.onresize = function (event) {
            $(".fillHight").fillHeight();
            $(".fillWidth").fillWidth();
        }

        $.fn.fillHeight = function () {
            var siblingsHeight = 0;
            this.siblings("div").each(function () {
                siblingsHeight = siblingsHeight + $(this).height();
            });

            var height = this.parent().height() - siblingsHeight;
            this.height(height);
        };

 
        $.fn.fillWidth = function (){
            var siblingsWidth = 0;
            this.siblings("div").each(function () {
                siblingsWidth  += $(this).width();
            });

            var width =this.parent().width() - siblingsWidth;
            this.width(width);
        }
      * {
            box-sizing: border-box;
        }

        html {
        }

        html, body, .fillParent {
            height: 100%;
            margin: 0;
            padding: 0;
        }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<div class="fillParent" style="background-color:antiquewhite">
        <div>
            no1
        </div>
        <div class="fillHight">
            no2 fill
        </div>
        <div class="deb">
            no3
        </div>
    </div>
 1
Author: user1911353,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-04-01 02:06:31

Możesz użyć funkcji calc do obliczenia pozostałej wysokości dla 2. div.

*{
  box-sizing: border-box;
}

#div1{
  height: 50px;
  background: skyblue;
}

#div2{
  height: calc(100vh - 50px);
  background: blue;
}
<div id="div1"></div>
<div id="div2"></div>
 1
Author: Abhishek Tewari,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-05-16 14:30:07
<div>
  <div id="header">header</div>
  <div id="content">content</div>
  <div id="footer">footer</div>
</div>

#header {
  height: 200px;
}

#content {
  height: 100%;
  margin-bottom: -200px;
  padding-bottom: 200px;
  margin-top: -200px;
  padding-top: 200px;
}

#footer {
  height: 200px;
}
 0
Author: wmzy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-01-13 10:43:51

Dlaczego nie użyć padding z ujemnymi marginesami? Coś takiego:

<div class="parent">
  <div class="child1">
  </div>
  <div class="child2">
  </div>
</div>

A następnie

.parent {
  padding-top: 1em;
}
.child1 {
  margin-top: -1em;
  height: 1em;
}
.child2 {
  margin-top: 0;
  height: 100%;
}
 0
Author: kalu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-19 05:18:31