Skip to main content

实现左侧固定,右侧自适应两栏布局

<div class="parent">
<div class="left">左边内容</div>
<div class="right">右边内容</div>
</div>

左侧绝对定位 加 右侧 margin-left

.parent {
position: relative;
}
.left {
background: red;
width: 100px;
height: 100px;
position: absolute;
left: 0;
}
.right {
background: blue;
margin-left: 100px;
height: 100px;
}

左侧浮动,右侧 margin-left

.left {
background: red;
width: 100px;
height: 100px;
float: left;
}
.right {
background: blue;
height: 100px;
overflow: hidden;
}

左测浮动,右侧 overflow

跟方法 2 类似,只是将右侧 overflow 改为 margin-left

.left {
background: red;
width: 100px;
height: 100px;
float: left;
}
.right {
background: blue;
height: 100px;
margin-left: 100px;
}

左右两边 absolute

.parent {
position: relative;
}
.left {
background: red;
position: absolute;
width: 100px;
height: 100px;
left: 0;
}
.right {
background: blue;
position: absolute;
height: 100px;
left: 100px;
right: 0;
}

flex

利用 css3 的 flex 属性: 一个父元素包裹两个子元素,父元素设置 display: flex,左边设置固定宽度,右边设置 flex:1,自动填充满剩余空间

.parent {
display: flex;
}
.left {
background: red;
width: 100px;
height: 100px;
}
.right {
background: blue;
height: 100px;
flex: 1;
}

grid

利用 css3 的 grid 属性:

.wrap {
display: grid;
grid-template-columns: 10px auto;
}

参考文章:https://xianyulaodi.github.io/2017/05/25/%E8%80%81%E7%94%9F%E5%B8%B8%E8%B0%88css%E5%AE%9E%E7%8E%B0%E5%B7%A6%E4%BE%A7%E5%9B%BA%E5%AE%9A%EF%BC%8C%E5%8F%B3%E4%BE%A7%E8%87%AA%E9%80%82%E5%BA%94/