Подтвердить что ты не робот

Как заставить div занимать оставшуюся высоту?

У меня есть эта проблема, у меня два div:

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

Как заставить div2 занимать оставшуюся высоту страницы?

4b9b3361

Ответ 1

Используйте абсолютное позиционирование:

#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>

Ответ 2

Вы можете использовать этот 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;
}

Просто примените класс .fill к любому из дочерних элементов, чтобы сделать, затем занять оставшуюся высоту.

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

Он работает с тем, сколько детей вы хотите, никакой дополнительной разметки не требуется.

Ответ 3

Демо

Один из способов - установить div на position:absolute и дать ему верх 50px и внизу 0px;

#div2
{
    position:absolute;
    bottom:0px;
    top:50px
}

Ответ 4

Поскольку вы знаете, сколько пикселей занято предыдущим контентом, вы можете использовать функцию calc():

height: calc(100% - 50px);

Ответ 5

С таблицами CSS вы можете обернуть div вокруг двух, которые у вас есть, и использовать эту структуру 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>

В зависимости от того, какие браузеры поддерживают эти типы отображения. Я не думаю, что IE8 и ниже. EDIT: Scratch, что... IE8 поддерживает таблицы CSS.

Ответ 6

Вы можете использовать

display: flex;

Свойство CSS, как упоминалось ранее, @Ayan, но я создал рабочий пример: https://jsfiddle.net/d2kjxd51/

Ответ 7

Я попытался использовать CSS, и вам нужно использовать display: table или вам нужно использовать новый CSS, который еще не поддерживается в большинстве браузеров (2016).

Итак, я написал плагин jquery, чтобы сделать это для нас, я рад поделиться им:

 
//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>

Ответ 8

<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;
}

Ответ 9

Почему бы не использовать прописку с отрицательными полями? Что-то вроде этого:

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

И затем

.parent {
  padding-top: 1em;
}
.child1 {
  margin-top: -1em;
  height: 1em;
}
.child2 {
  margin-top: 0;
  height: 100%;
}