1. 程式人生 > >前端經典三欄佈局,中間自適應。

前端經典三欄佈局,中間自適應。

  1. 首先第一種我們常用的float方法
    需要注意的是該方法的中間部分center要放在right盒子之後。
  html *{
            padding:0;
            margin: 0;
        }
        .layout article div{
            min-height: 100px;
        }
        .layout.float .left{
            float: left;
            width: 300px;
            background: red;
        }
        .layout.float .right{
            float: right;
            width: 300px;
            background: blue;
        }
        .layout.float .center{
            background: yellow;
        }
html部分:
 <section
class="layout float">
<article class="left-right-center"> <div class="left"></div> <div class="right"></div> <div class="center"> <h1>浮動解決方案</h1> <h2>注意內容主體放在最後</h2> </div
>
</article> </section>

2.絕對定位法,使用position來實現:

html *{
            padding: 0;
            margin: 0;
        }

        .layout article div{
               min-height: 300px;
        }

        .layout.absolute .left-center-right>div{
              position: absolute;
        }

        .layout
.absolute .left{ left: 0; width: 300px; background: red; } .layout.absolute .center{ left: 300px; right:300px; background: yellow; } .layout.absolute .right{ right:0; width: 300px; background: blue; } html部分: <section class="layout absolute"> <article class="left-center-right"> <div class="left"></div> <div class="center"> <h2>絕對定位解決方案</h2> 1、這是三欄佈局絕對定位中間部分 </div> <div class="right"></div> </article> </section>

3.第三種彈性佈局flex


 html *{
           margin: 0;
           padding: 0;
       }
        .layout article div{
            min-height: 100px;
        }
        .layout.flexbox .left-center-right{
            display: flex;
        }
        .layout.flexbox .left{
            width: 300px;
            background: red;
        }
        .layout.flexbox .center{
            flex: 1;
            background: yellow;
        }
        .layout.flexbox .right{
            width: 300px;
            background: blue;
        }
   html部分:
   <section class="layout flexbox">
    <article class="left-center-right">
        <div class="left"></div>
        <div class="center">
            <h1>這是flexbox的解決方案</h1>
            1.這是三欄佈局的中間部分
        </div>
        <div class="right"></div>
    </article>
</section>

4.第四種table佈局

html *{
            margin: 0;
            padding: 0;
        }
        .layout article div{
            min-height: 300px;
        }
        .layout.table .left-center-right{
            width: 100%;
            display: table;
            height: 100px;
        }
        .layout.table .left-center-right>div{
            display: table-cell;
        }
        .layout.table .left{
            width: 300px;
            background: red;
        }
        .layout.table .center{
            background: yellow;
        }
        .layout.table .right{
            width: 300px;
            background: blue;
        }
   html部分:
   <section class="layout table">
        <article class="left-center-right">
            <div class="left"></div>
            <div class="center">
                <h1>表格佈局</h1>
                1.這是三欄佈局表格佈局中間部分
            </div>
            <div class="right"></div>
        </article>
    </section>

5.網格佈局grid是html5的新屬性


 html *{
            padding: 0;
            margin: 0;
        }

      .layout.grid .left-center-right{
          display: grid;
          width: 100%;
          grid-template-rows: 100px;
          grid-template-columns: 300px auto 300px;
      }
        .layout.grid .left{
            background: red;
        }
        .layout.grid .center{
            background: yellow;
        }
        .layout.grid .right{
            background: blue;
        }
  html部分:
  <section class="layout grid">
       <div class="left-center-right">
           <div class="left"></div>
           <div class="center">
               <h1>網格佈局</h1>
           </div>
           <div class="right"></div>
       </div>
   </section>

這五種方案通常pc中可以使用float和table佈局來實現,移動端可以使用flex來實現grid也可以,絕對定位的方案脫離文件流可用性很差,而且專案中也不這麼寫代買,浮動佈局需要處理好BFC的問題就可以了。