Html布局左右寬度固定中間自適應(yīng)解決方案

2018-12-03 14:13:22 來源:互聯(lián)網(wǎng)作者:someone 人氣: 次閱讀 391 條評(píng)論

文章主要介紹了詳解左右寬度固定中間自適應(yīng)html布局解決方案的相關(guān)資料,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧 a.使用浮動(dòng)布局html結(jié)構(gòu)...

文章主要介紹了詳解左右寬度固定中間自適應(yīng)html布局解決方案的相關(guān)資料,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

 

a.使用浮動(dòng)布局

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

  1. <div class="box">
  2.     <div class="left">left</div>
  3.     <div class="right">right</div>
  4.     <div class="center">center</div>
  5. </div>  
  6. //此處注意要先渲染左、右浮動(dòng)的元素才到中間的元素。元素浮動(dòng)后剩余兄弟塊級(jí)元素會(huì)占滿父元素的寬度
  7. <style>
  8.    .box{
  9.         height:200px;
  10.     }   
  11.     .left{
  12.         float:left;
  13.         width:300px;
  14.     }
  15.     .right{
  16.         float:right;
  17.         width:300px;
  18.     }
  19. </style>

b.使用固定定位

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

  1. <div class="box">
  2.     <div class="left">left</div>
  3.     <div class="right">right</div>
  4.      <div class="center">center</div>
  5. </div>
  6. //和浮動(dòng)布局同理,先渲染左右元素,使其定位在父元素的左右兩端,剩余的中間元素占滿父元素剩余寬度。
  7. <style>
  8.     .box{
  9.         position: relative;
  10.       }
  11.       .left{
  12.         position: absolute;
  13.         width: 100px;
  14.         left: 0;
  15.       }
  16.       .right{
  17.         width:100px;
  18.         position: absolute;
  19.         right: 0;
  20.       }
  21.       .center{
  22.         margin: 0 100px;
  23.         background: red;
  24.       }
  25. </style>

c.表格布局

將父元素display:table,子元素display:table-cell,會(huì)將它變?yōu)樾袃?nèi)塊。

這種布局方式的優(yōu)點(diǎn)是兼容性好。

  1. <div class="box">
  2.   <div class="left">
  3.     left
  4.   </div>
  5.   <div class="center">
  6.     center
  7.   </div>
  8.   <div class="right">
  9.     right
  10.   </div>
  11. </div>
  12. <style>
  13.     .box{
  14.         display: table;
  15.         width: 100%;
  16.       }
  17.       .left{
  18.         display: table-cell;
  19.         width: 100px;
  20.         left: 0;
  21.       }
  22.       .right{
  23.         width:100px;
  24.         display: table-cell;
  25.       }
  26.       .center{
  27.         width: 100%;
  28.         background: red;
  29.       }
  30. </style>

d.彈性布局

父元素display:flex子元素會(huì)全部并列在一排。

子元素中flex:n的寬度會(huì)將父元素的寬度/n

如flex:1,寬度就等于父元素高度。

彈性布局的缺點(diǎn)是兼容性不高,目前IE瀏覽器無法使用彈性布局

  1. <div class="box">
  2.   <div class="left">
  3.     left
  4.   </div>
  5.   <div class="center">
  6.     center
  7.   </div>
  8.   <div class="right">
  9.     right
  10.   </div>
  11. </div>
  12. <style>
  13.     .box{
  14.         display: flex;
  15.         width: 100%;
  16.       }
  17.       .left{
  18.        
  19.         width: 100px;
  20.         left: 0;
  21.       }
  22.       .right{
  23.         width:100px;
  24.       }
  25.       .center{
  26.         flex:1;
  27.       }
  28. </style>

e.網(wǎng)格布局

父元素display:grid;

grid-templatecolumns:100px auto 100px;

依次為第一個(gè)子元素寬100px 第二個(gè)自適應(yīng) 第三個(gè)100px;

網(wǎng)格布局的優(yōu)點(diǎn)是極為簡(jiǎn)便,直接通過父元素樣式?jīng)Q定,缺點(diǎn)是兼容性不高。

  1. <div class="box">
  2.   <div class="left">
  3.     left
  4.   </div>
  5.   <div class="center">
  6.     center
  7.   </div>
  8.   <div class="right">
  9.     right
  10.   </div>
  11. </div>
  12. <style>
  13.   .box{
  14.         display: grid;
  15.         grid-template-columns: 100px auto 100px;
  16.         width: 100%;
  17.       }
  18. </style>

以上就是本文Html布局左右寬度固定中間自適應(yīng)解決方案的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。