用css创建两栏布局

1、左侧元素设置固定大小,并左浮动,右侧元素设置overflow: hidden; 这样右边就触发BFC,BFC的区域不会与浮动元素发生重叠,所以两侧就不会发生重叠。

.left

            width: 100px;

            background: red;

            float: left;

        }

        .right {

            background: blue;

            overflow: hidden;

        }

2、将左边元素宽度设置为200px,并且设置向左浮动。将右边元素的margin-left设置为200px,宽度设置为auto(默认为auto,撑满整个父元素)

.outer {  height: 100px;}

.left {

  float: left;

  width: 200px;

  background: tomato;

}

.right {

  margin-left: 200px;

  width: auto;

  background: gold;

}

3、将左边元素设置为固定宽度200px,将右边的元素设置为flex:1。

.outer {

  display: flex;

  }

.left {

  width: 200px;

  background: tomato;

}

.right {

  flex: 1;

  background: gold;}

4、将父级元素设置为相对定位。左边元素设置为absolute定位,并且宽度设置为200px。将右边元素的margin-left的值设置为200px

.outer {

  position: relative;

}

.left {

  position: absolute;

  width: 200px;

  background: tomato;

}

.right {

  margin-left: 200px;

  background: gold;

}

5、利用绝对定位,将父级元素设置为相对定位。左边元素宽度设置为200px,右边元素设置为绝对定位,左边定位为200px,其余方向定位为0。

.outer {

  position: relative;

  }

.left {

  width: 200px;

  background: tomato;

}

.right {

  position: absolute;

  top: 0;

  right: 0;

  bottom: 0;

  left: 200px;

  background: gold;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值