高级前端
css
【Q017】css 如何实现左侧固定300px,右侧自适应的布局

css 如何实现左侧固定300px,右侧自适应的布局

更多描述 问题追问:

  • 如果使用 flex 布局回答 flex: 1 时,则追问它是那几个属性的简写,最主要配置的是哪一个属性

Issue 欢迎在 Gtihub Issue 中回答此问题: Issue 18 (opens in a new tab)

Author 回答者: zhaochongzi (opens in a new tab)

css--- .box{ width:100%;; height:100%; display:flex; } .one{ width:300px; height:300px; background-color: #afa; } .two{ flex:1; height:300px; background-color: #ae5aca; } html----

12312313
123

Author 回答者: shfshanyue (opens in a new tab)

代码见 左侧固定,右侧自适应 - Codepen (opens in a new tab)

使用 flex 布局,左侧 300px,右侧 flex-grow: 1pug 代码及 css 代码示例如下

.container
  .left
  .main
.container {
  display: flex;
}
 
.left {
  flex-basis: 300px;
  flex-shrink: 0;
}
 
.main {
  flex-grow: 1;
}

如果只使用 Grid 布局,则代码会更加简单,只需要控制容器的 CSS 属性

.container {
  display: grid;
  grid-template-columns: 300px 1fr;
}

Author 回答者: Uwah (opens in a new tab)

左侧300px;右侧flex: 1; 采用flex的固比模型

Author 回答者: bohancen (opens in a new tab)

使用calc方法 .left{width:330px;} .right{width: calc(100% - 330px)}

Author 回答者: szc-sun (opens in a new tab)

圣杯布局吧, float也可以,不过很少用了
.container{padding-left: 300px;} .left,.main { float: left;position: relative;}
.left{width: 300px;right: 300px;margin-left: -100%}
.main{width: 100%;}

Author 回答者: wuzqZZZ (opens in a new tab)

浮动+BFC

<div class="box">
  <div class="box1"></div>
  <div class="box2"></div>
</div>
.box {
  height: 400px;
  background-color: skyblue;
}
.box1 {
  float: left;
  width: 300px;
  height: 200px;
  background-color: red;
}
.box2 {
  height: 200px;
  background-color: blue;
  overflow: hidden;
}