CSS两列布局

CSS两列布局

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

(1)利用float+margin(fix)实现

<body>
    <div class="left">左列定宽</div>
    <div class="right-fix">
        <div class="right">右列自适应</div>
    </div>
</body>
.left{
    background-color: orange;
    float: left;
    width: 100px;
    height: 300px;
}

.right-fix{
    float: left;
    width: 100%;
    margin-left: -100px;
}

.right{
    background-color: #bbffaa;
    height: 300px;
    margin-left: 100px;
}

(2)使用float+overflow实现

<body>
    <div class="left">左列定宽</div>
    <div class="right">右列自适应</div>
</body>
.left{
    background-color: orange;
    float: left;
    width: 100px;
    height: 300px;
}

.right{
    background-color: #bbffaa;
    height: 300px;
    overflow: hidden;
}

(3)使用table实现
margin失效;设置间隔比较麻烦

<body>
    <div class="content">
        <div class="left">左列定宽</div>
        <div class="right">右列自适应</div>
    </div>
</body>
.content{
    width: 100%;
    display: table;
    height: 300px;
}

.left{
    background-color: orange;
    width: 100px;
}

.right{
    background-color: #bbffaa;
}

.left,.right{
    /*利用单元格自动分配宽度*/
    display: table-cell;
}

(4)使用绝对定位

.content{
    position: relative;
}

.left{
    position: absolute;
    top: 0;
    left: 0;
    width: 100px;
    height: 500px;
    background-color: orange;
}

.right{
    position: absolute;
    top: 0;
    left: 100px;  /*值大于等于.left的宽度*/
    right: 0;
    background-color: #0f0;
    height: 500px;
}

(5)使用flex实现。

.content{
    width: 500px;
    height: 500px;
    display: flex;
}

.left{
    width: 100px;
    background-color: orange;
}

.right{
    /* 均分父元素剩余空间 */
    flex: 1;
    background-color: palegreen;
}

(6)使用Grid实现

.content{
    width: 600px;
    height: 500px;
    display: grid;
    /*设定2列,auto换成1fr也行*/
    grid-template-columns: 100px auto;
}

.left{
    background-color: orange;
}

.right{
    background-color: palegreen;
}

2、左列自适应,右列定宽

(1)使用float+margin实现

.content{
    height: 300px;
    /*抵消#left的margin-left,
    达到#parent可在屏幕上显示完整,即水平居中*/
    padding-left: 100px;
}

.left{
    float: left;
    height: 300px;
    width: 100%;
    margin-left:-100px;
    background-color: orange;
}

.right{
    float: right;;
    width: 100px;
    height: 300px;
    background-color: palegreen;
}

(2)使用float+overflow实现

<div class="content">
        <!-- 顺序一定是right在前 -->
        <div class="right">右列定宽</div>
        <div class="left">左列自适应</div>
</div>
.left{
    overflow: hidden;
    height: 300px;
    background-color: orange;
}

.right{
    margin-left: 10px;
    float: right;
    width: 100px;
    height: 300px;
    background-color: palegreen;
}

(2)使用绝对定位实现

.content{
    position: relative;
}
.left{
    position: absolute;
    top: 0;
    left: 0;
    /*大于等于#rigth的宽度*/
    right: 100px;
    height: 300px;
    background-color: orange;
}

.right{
    position: absolute;
    top: 0;
    right: 0;
    height: 300px;
    width: 100px;
    background-color: palegreen;
}

3、一列不定,一列自适应

(盒子宽度随着内容增加或减少发生变化,另一个盒子自适应)
(1)使用float和overflow

.left{
    float: left;
    height: 300px;
    background-color: orange;
}

.right{
    /* 开启BFA,使元素不会被浮动元素覆盖 */
    overflow: hidden;
    height: 300px;
    background-color: palegreen;
}

(2)使用Flex实现

.content{
    display: flex;
}

.left{
    /* 不设置宽度 */
    height: 300px;
    background-color: orange;
}

.right{
    flex: 1;
    height: 300px;
    background-color: palegreen;
}

(3)使用Grid实现。

.content{
    display: grid;
    /* 左侧不定宽,右侧自适应 */
    grid-template-columns: auto 1fr;
}

.left{
    height: 300px;
    background-color: orange;
}

.right{
    height: 300px;
    background-color: palegreen;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值