繁体   English   中英

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

[英]Top and bottom fixed height with fluid height middle

我要问的是我用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实现吗?

是的使用calc()函数:

为你的第3 div设置height CSS属性为:

height: calc(h - n - k);

如果您计划支持不支持calc()浏览器,那么这是一种没有它的方法。

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

DEMO

 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; } 
 <div class="wrap"> <div class="one"></div> <div class="two"></div> <div class="three"></div> </div> 


编辑:

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

DEMO

 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; } 
 <div class="wrap"> <div class="one">... content 1 ...</div> <div class="two">... content 2 ...</div> <div class="three">...content 3 ...</div> </div> 

一个替代calc()是使用flexboxes 这些不会将高度耦合在一起,因此如果您更改了页眉和页脚的高度,则无需更新其他元素的CSS。

这是一个基本的例子:

 $('button').on('click', function() { $('.container').height(function(i, v) { return v < 200 ? 250 : 150 }); }) 
 .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; } 
 <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> 

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

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

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

这是demo 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});

你可以使用calc() 这是一个工作演示

记得在正确的位置包装使用括号(例如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;
}

另外一定要检查浏览器支持

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM