【自适应两栏式布局】

两栏式布局

  • 左列定宽,右列自适应
  • 左列不定宽,右列自适应

1. 左列定宽,右列自适应

<div class="container">
    <div class="left">定宽左部分</div>
    <div class="right">自适应右部分</div>
</div>

方法一:浮动 + margin

.container{
    width: 70%;
    border: 2px solid red;
}
.left{
    width: 300px;
    background-color: rgba(217, 255, 0, 0.815);
    height: 300px;
    float: left;
}
.right{
    background-color: rgba(137, 43, 226, 0.952);
    height: 500px;
    /* 未加margin之前为假的两栏式布局 */
    margin-left: 300px;
}

方法二:浮动 + BFC

.container{
    width: 70%;
    border: 2px solid red;
}
.left{
    width: 300px;
    background-color: rgba(217, 255, 0, 0.815);
    height: 300px;
    float: left;
}
.right{
    background-color: rgba(137, 43, 226, 0.952);
    height: 500px;
    /* 触发BFC:环绕浮动元素! */
    overflow: hidden;
}

方法三:定位

.container{
    width: 70%;
    border: 2px solid red;
    /* 相对定位:相对自己原本位置! */
    position: relative;
}
.left{
    width: 300px;
    background-color: rgba(217, 255, 0, 0.815);
    height: 300px;
    /* 设置绝对定位 */
    position: absolute;
    left: 0px;
}
.right{
    background-color: rgba(137, 43, 226, 0.952);
    height: 500px;

    /* way1: */
    /* margin-left: 300px; */


    /* way2: [需要清除浮动]*/
    position: absolute;
    left: 300px;
    right: 0;
}

方法四:flex 布局

.container{
    width: 70%;
    border: 2px solid red;
    display: flex;
}
.left{
    width: 300px;
    background-color: rgba(217, 255, 0, 0.815);
    height: 300px;
}
.right{
    background-color: rgba(137, 43, 226, 0.952);
    height: 500px;
    /* flex:1; 填充容器剩余空间,达到自适应 */
    flex: 1;
}

方法五:浮动 + 父外边距

 .container{
    width: 70%;
    border: 2px solid red;
}
/* 清除浮动 */
.container::after{
    display: block;
    content: '';
    clear: both;
}
.left{
    width: 300px;
    height: 300px;
    background-color: yellow;
    float: left;
    margin-right: -100%;
}
.right{
    width: 100%;
    height: 500px;
    float: left;
}
.content{
    height: 100%;
    background-color: green;
    margin-left: 300px;
}

方法六:浮动布局

.container{
    width: 70%;
    border: 2px solid black;
    display: table;
}
.left{
    width: 300px;
    height: 300px;
    /* 平分整个父元素的width */
    display: table-cell;
    background-color: yellow;
}
.right{
    display: table-cell;
    height: 500px;
    background-color: blue;
}

2. 左列不定宽,右列自适应

方法一:flex

.container{
    width: 70%;
    border: 2px solid black;
    display: flex;
}
.left{
    width: 300px;
    height: 300px;
    background-color: yellow;
}
.right{
    flex: 1;
    height: 500px;
    background-color: blue;
}

方法二:浮动 + BFC

.container{
    width: 70%;
    border: 2px solid black;
}
.left{
    width: 300px;
    height: 300px;
    background-color: yellow;
    float: left;
}
.right{
    height: 500px;
    background-color: blue;
    overflow: hidden;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值