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:100px;
        border:1px solid #333;
        display:flex; 
      }
      .dwidth
      {
        
         width:150px;
      }
      .center
      { 
       flex:1; 
      }
    </style>
  </head>
  <body>
    <div id="boxs">
      <div class="dwidth" style="background-color:#ffc5c5;" >左边固定宽度</div>
       <div class="center" style="background-color:#b9f;" >中间自适应宽度</div>
      <div class="dwidth" style="background-color:#7171f7;" >右边固定宽度</div>  
    </div>
  </body>
</html>

例子