四種方案實現CSS兩欄佈局

實現兩欄佈局:左側定寬,右側自適應

html:

<html>
  <head>
  </head>
  
  <body>
    <div class="container">
      <div class="left">LEFT</div>
      <div class="right">RIGHT</div>
    </div>
  </body>
</html>

最終效果圖:

方案一、float+margin

.container {
  width: 100%;
}
.left {
  background: #2196f3;
  width: 200px;
  height: 100px;
  float: left;
}
.right {
  background: #ff9800;
  height: 100px;
  margin-left: 200px;
}

方案二、float+overflow:hidden

.container {
  width: 100%;
}
.left {
  background: #2196f3;
  width: 200px;
  height: 100px;
  float: left;
}
.right {
  background: #ff9800;
  height: 100px;
  overflow: hidden;
}

方案三、CSS3:float+calc

.container {
  width: 100%;
}
.left {
  background: #2196f3;
  width: 200px;
  height: 100px;
  float: left;
}
.right {
  background: #ff9800;
  height: 100px;
  float: left;
  width: calc(100% - 200px);
}

方案四、flex佈局

.container {
  width: 100%;
  display: flex;
}
.left {
  background: #2196f3;
  width: 200px;
  height: 100px;
}
.right {
  background: #ff9800;
  height: 100px;
  flex: 1;
}

 

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章