css flex 上下固定中间自适应

css flex 上下固定中间自适应也是开发中常见的一种布局,本章节将来演示如何用css flex来实现上下固定中间自适应的布局。

代码例子

只需要把中间设置成flex:1即可,这等同于flex-grow:1;flex-shrink:1;(flex-grow:1空间足够填充剩余空间,flex-shrink:1空间不足等比例缩小)

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>css flex 上下固定中间自适应例子</title>
    <style>
      body {
        padding: 5px
      }
      #boxs
      {
        width:90%;
        height:300px;
        border:1px solid #333;
        display:flex; 
        flex-direction:column
      }
      .dheight
      {
        
        height:50px;
      }
      .center
      { 
       flex:1; 
      
      }
    </style>
  </head>
  <body>
    <div id="boxs">
      <div class="dheight" style="background-color:#ffc5c5;" >上边固定宽度</div>
       <div class="center" style="background-color:#b9f;" >中间自适应宽度</div>
      <div class="dheight" style="background-color:#7171f7;" >下边固定宽度</div>  
    </div>
  </body>
</html>

例子