兩欄布局

2019-11-29    seo達(dá)人

兩列布局的幾種方法

html結(jié)構(gòu)

 <div class="content">

      <div class="content-left">

        左側(cè)固定200px

      </div>

      <div class="content-right">

        右側(cè)自適應(yīng)

      </div>

 </div>



1.通過(guò)float和margin-left

 / 清除瀏覽器默認(rèn)邊距 /

      {

        margin: 0;

        padding: 0;

      }



      .content{

        overflow: hidden;

      }

      /
脫離文檔流 /

      .content-left {

        float: left;

        width: 200px;

        height: 200px;

        background: red;

      }



      .content-right {

        /
通過(guò)margin-left將左邊位置空出 /

        margin-left: 200px;

        background: blue;

        height: 200px;

      }



2.通過(guò) position: absolute;絕對(duì)定位

 /
清除瀏覽器默認(rèn)邊距 /

     
{

        margin: 0;

        padding: 0;

      }



      .content {

        overflow: hidden;

        position: relative;

      }

      / 脫離文檔流 /

      .content-left {

        position: absolute;

        top: 0;

        left: 0;

        width: 200px;

        height: 200px;

        background: red;

      }



      .content-right {

        / 通過(guò)margin-left將左邊位置空出 /

        margin-left: 200px;

        background: blue;

        height: 200px;

      }



3.通過(guò)flex彈性布局

/ 清除瀏覽器默認(rèn)邊距 /

      {

        margin: 0;

        padding: 0;

      }



      .content {

        overflow: hidden;

        display: flex;

      }

      .content-left {

          /
除了width: 200px;還可以flex-basis: 200px; /

        width: 200px;

        height: 200px;

        background: red;

      }



      .content-right {

          /
flex:1;將剩余空間分給它 /

        flex: 1;

        background: blue;

        height: 200px;

      }



4.通過(guò) display: table;表格布局

 /
清除瀏覽器默認(rèn)邊距 /

     
{

        margin: 0;

        padding: 0;

      }



      .content {

        overflow: hidden;

        display: table;

        / 必須給父級(jí)定寬不然自適應(yīng)盒子沒(méi)定寬只會(huì)由內(nèi)容撐開 /

        width: 100%;

      }

      .content-left {

        display: table-cell;

        width: 200px;

        height: 200px;

        background: red;

      }



      .content-right {

        display: table-cell;

        background: blue;

        height: 200px;

      }



5.通過(guò)inline-block和calc()函數(shù)

 / 清除瀏覽器默認(rèn)邊距 /

      {

        margin: 0;

        padding: 0;

      }



      .content {

        /
必須加font-size=0;把inline-block默認(rèn)間距去掉,

        不過(guò)設(shè)置后里面文字不顯示了可以給里面塊設(shè)置font-size:20px;

        或者把兩個(gè)塊之間的換行刪掉也能去掉間距/

        font-size: 0;

        overflow: hidden;

      }

      .content-left {

        font-size: 20px;

        display: inline-block;

        width: 200px;

        height: 200px;

        background: red;

      }



      .content-right {

        font-size: 20px;

        display: inline-block;

        background: blue;

        height: 200px;

        /
注意calc里的運(yùn)算符兩邊要有空格 /

        width: calc(100% - 200px);

      }



6.通過(guò)float和calc()函數(shù),左右兩塊都要浮動(dòng)

 /
清除瀏覽器默認(rèn)邊距 /

     
{

        margin: 0;

        padding: 0;

      }



      .content {

        overflow: hidden;

      }

      .content-left {

        float: left;

        width: 200px;

        height: 200px;

        background: red;

      }

      .content-right {

        float: left;

        background: blue;

        height: 200px;

        / 注意calc里的運(yùn)算符兩邊要有空格 /

        width: calc(100% - 200px);

      }



7.使用grid布局

 / 清除瀏覽器默認(rèn)邊距 /

      {

        margin: 0;

        padding: 0;

      }



      .content {

        overflow: hidden;

        display: grid;

        grid-template-columns: 200px 1fr;

        /
grid布局也有列等高的默認(rèn)效果。需要設(shè)置: align-items: start;。 /

        align-items: start;

      }

      .content-left {

        height: 200px;

        background: red;

        /
grid布局還有一個(gè)值得注意的小地方和flex不同:在使用margin-left的時(shí)候,

        grid布局默認(rèn)是box-sizing設(shè)置的盒寬度之間的位置。

        而flex則是使用兩個(gè)div的border或者padding外側(cè)之間的距離。 */

        box-sizing: border-box;

        grid-column: 1;

      }

      .content-right {

        background: blue;

        height: 200px;

        box-sizing: border-box;

        grid-column: 2;

      }



分享本文至:

日歷

鏈接

個(gè)人資料

存檔