leftfixed.png日常工作里,想要实现这种常见的:左侧固定,右侧自适应的两列布局,这里记录一下几种常用写法
共用html和css
<style>
.left {
border:1px solid blue;
}
.right{
border:1px solid red;
}
</style>
...
<div class="wrap">
<div class="left">left</div>
<div class="right">right</div>
</div>
flex布局
📕:ie低版本不支持
.wrap {
height: 50px;
display: flex;
}
.wrap > .left {
width: 100px;
}
.wrap >.right {
flex-grow: 1;
}
grid布局
grid-template-columns: 100px 1fr;
baidu browser 不兼容
.wrap {
display: grid;
grid-template-columns: 100px 1fr;
}
.wrap > .left {
width: 100px;
position: absolute;
}
.wrap > .right {
margin-left: 100px;
}
float & marginLeft
absolute、 marginLeft子元素高度溢出需要配合js配置父高
.wrap::after{
content: '';
display: block;
clear: both;
}
.wrap > .left {
width: 100px;
float: left;
}
.wrap > .right {
margin-left: 102px; /** border 占位置**/
}
calc计算宽度
calc(100% - 104px),记得减去border
.wrap {
font-size: 0;
/* border: 1px solid pink; */
}
.wrap > .left {
vertical-align: top;
font-size: 16px;
width: 100px;
display: inline-block;
}
.wrap > .right {
vertical-align: top;
font-size: 16px;
width: calc(100% - 104px);
display: inline-block;
}
table-cell
.wrap {
display: table;
}
.wrap > .left {
width: 100px;
}
.wrap > .right{
display: table-cell;
width: 100%
}
网友评论