CSS左側(cè)固定右側(cè)自適應(yīng)的五種布局方法


在頁(yè)面或者布局列表中,常常有左側(cè)固定,右側(cè)自使用的情況,接下來(lái),這五種方法滿足這個(gè)需求。

一、左邊浮動(dòng),右邊margin

<!--html部分-->

 <div class="box">
    <div class="left"></div>
    <div class="right"></div>
</div>

<!--CSS部分-->
  .box{
     height: 200px;
        background-color: skyblue;
     }
    .left{
        float:left;
        width:100px; 
        height:200px; 
    }
    .right{
        margin-left:100px; 
        height:200px; 
        background:yellowgreen; 
    }

二、左邊浮動(dòng),右邊hidden。

這樣右邊的盒子就變成了一個(gè)絕緣的盒子。(所謂絕緣,就是和其他盒子不會(huì)有交集)

<!--html部分-->
<div class="box">
    <div class="left"></div>
    <div class="right"></div>
</div>
<!--CSS部分-->
.box{
    height: 200px;
    background-color: skyblue;
 }
 .left{
    width:100px; 
    height:200px; 
    background:yellowgreen; 
    float:left;
  }
  .right{
    overflow:hidden;
    height:200px;           
  }

三、父盒子設(shè)置padding,左邊盒子定位

<!--html部分-->
<div class="box">
    <div class="left"></div>
    <div class="right"></div>
</div>
<!--CSS部分-->
.box{
    height: 200px;
    background-color: skyblue;
    padding-left: 100px;
    position: relative;
}
.left{
    width: 100px;
    height: 200px;
    background-color: yellowgreen;
    position: absolute;
    top:0;
    left:0;
}
.right{
    width: 100%;
}

四、table實(shí)現(xiàn)

<!--html部分-->
<table border="0" cellpadding="0" cellspacing="0" class="box">
      <tr>
       <td class="left"></td>
       <td class="right"></td>
      </tr>
</table>

<!--CSS部分-->

.box{
    width:100%;
    }
.left{
    width:100px; 
    height:200px; 
    background:skyblue; 
}
.right{
    background:yellowgreen;
}

五、flex實(shí)現(xiàn)

<!--html部分-->
<div class="box">
    <div class="left"></div>
    <div class="right"></div>
</div>

<!--CSS部分-->

.box{
    height: 200px;
    background-color: skyblue;
    display: flex;   /* 設(shè)置為flex容器 */
}
.left{
    width: 100px;
    height: 200px;
}
.right{
    height:200px; 
    background:yellowgreen;
    flex:1; /* 比例設(shè)置為1,撐滿剩余空間 */
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容