Daily-Question icon indicating copy to clipboard operation
Daily-Question copied to clipboard

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

Open shfshanyue opened this issue 5 years ago • 6 comments

shfshanyue avatar Nov 08 '19 11:11 shfshanyue

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

zhaochongzi avatar Dec 06 '19 03:12 zhaochongzi

代码见 左侧固定,右侧自适应 - Codepen

使用 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;
}

shfshanyue avatar Dec 24 '19 05:12 shfshanyue

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

Uwah avatar Mar 03 '20 01:03 Uwah

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

bohancen avatar Mar 25 '20 02:03 bohancen

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

szc-sun avatar Sep 08 '21 09:09 szc-sun

浮动+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;
}

wuzqZZZ avatar Aug 28 '22 07:08 wuzqZZZ