顶部和底部固定高度,流体高度中间

时间:2015-02-21 18:06:36

标签: html css css3 responsive-design

我要问的是我使用javascript做的事情,但我确实希望实现仅限css (如果可能)

以下是该方案:

我有一个DIV元素,其高度 h px。这个非常DIV元素有 3个孩子,它们也是DIV元素。他们的目的如下:

第一个DIV元素是 k px高度,并附加到父容器的顶部。它的高度是恒定的。

第二个DIV元素是 n px高度,并附加到父容器的底部。它的高度是恒定的。

第三个DIV元素是 h - (n + k) px。

当我调整父div (这是一个浮动框)以自动保持第三个DIV元素h - (n + k)px时,我想要的是

这是否可以通过 css 来实现?

6 个答案:

答案 0 :(得分:6)

是使用calc()功能:

表示您的第3个div集height CSS属性为:

height: calc(h - n - k);

答案 1 :(得分:6)

如果您计划支持browsers that don't support calc(),这是一种没有它的方法。

重点是使用绝对定位,填充(与附加的顶部和底部元素相同的高度)和box-sizing:border-box;

<强> DEMO

&#13;
&#13;
html,body,.wrap {
  height: 100%;
  margin: 0;
  padding: 0;
}
div {
  box-sizing: border-box;
}
.wrap {
  position: relative;
  width: 50%;
  padding: 50px 0 80px;
  border: 5px solid tomato;
}
.one,.two {
  position: absolute;
  left: 0;
  width: 100%;
  border: 5px solid teal;
}
.one {
  height: 50px;
  top: 0;
}
.two {
  height: 80px;
  bottom: 0;
}
.three {
  height: 100%;
  background: gold;
}
&#13;
<div class="wrap">
  <div class="one"></div>
  <div class="two"></div>
  <div class="three"></div>
</div>
&#13;
&#13;
&#13;


编辑:

这是一个奖金,没有box-sizing属性,只有绝对定位:

<强> DEMO

&#13;
&#13;
html, body, .wrap {
    height: 100%;
    margin: 0;
    padding: 0;
}
.wrap {
    position: relative;
    width: 50%;
}
.one, .two, .three {
    position: absolute;
    left: 0;
    width: 100%;
    background: teal;
}
.one {
    height: 50px;
    top: 0;
}
.two {
    height: 80px;
    bottom: 0;
}
.three {
    top: 50px;
    bottom: 80px;
    background: gold;
}
&#13;
<div class="wrap">
    <div class="one">... content 1 ...</div>
    <div class="two">... content 2 ...</div>
    <div class="three">...content 3 ...</div>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:2)

calc()的替代方法是使用flexboxes。这些不会将高度结合在一起,所以如果你改变了页眉和页脚的高度,你就不需要为你的其他元素更新CSS。

这是一个基本的例子:

&#13;
&#13;
$('button').on('click', function() {
    $('.container').height(function(i, v) {
        return v < 200 ? 250 : 150
    });
})
&#13;
.container {
    float:left;
    height:150px;
    width:300px;
    background-color:#000;
    /* set as a flex container laid out in a column (top to bottom) */
    display:flex;
    flex-direction:column;
}

/* content div, flex auto fills the remaining height */
.container div {
    background-color:#0f0;
    flex:1 1 auto;
}

/* top red div, fixed at 50px */
.container div:first-child {
    flex:0 0 50px;
    background-color:#f00;
}

/* bottom blue div, fixed at 50px */
.container div:last-child {
    flex:0 0 50px;
    background-color:#00f;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<div class="container">
    <div>Header</div>
    <div>Content</div>
    <div>Footer</div>
</div>
<div><button>Resize Container</button></div>
&#13;
&#13;
&#13;

答案 3 :(得分:1)

您可以使用calc来实现此目的。例如:

div {
 height: calc(h - n - k);
}

如需进一步阅读,请查看calc()上的精彩教程: http://webdesign.tutsplus.com/tutorials/calc-grids-are-the-best-grids--cms-22902

答案 4 :(得分:1)

以下是演示jsFiddle

HTML:

<div id="main">
    <div id="a"></div>
    <div id="b">Hello</div>
    <div id="c"></div>
</div>

CSS:

#main{height: 100%;background:red;}
#a{height: 100px;background:blue;}
#c{height: 50px;background:green;}
#b{height: calc(100% - 150px});

答案 5 :(得分:1)

您可以使用calc()。这是working demo

请记住将括号括在正确的位置(例如calc(200px - (50px + 50px))

#parent {
    width: 100px;
    height: 200px;
}

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

#div2 {
    height: 50px;
    background: blue;
}

#div3 {
    height: calc(200px - (50px + 50px));
    background: green;
}

还要确保check browser support