css两栏布局

实现两栏布局的方式

1.利用浮动

将左元素设置为固定宽度,并且向左浮动,右元素宽度设置为auto,margin-left为左元素宽度

<style>

        html {
            font-size: 33px;
        }
        #container {
            background-color: aquamarine;
            width: 600px;
            height: 400px;
            margin-top: 135px;
        }
        .left {
            background-color: rgb(36, 157, 48);
            width: 200px;
            float: left;
            height: 300px;
        }
        .right {
            background-color: yellow;
            width: auto;
            margin-left: 200px;
            height: 300px;
        }

        
    </style>
</head>

<body>
    <div id="container">
       <div class="left">
        L
       </div>
       <div class="right">
        R
       </div>
    </div>
    <script>
    </script>
</body>

2. 浮动+bfc

  • 右侧元素设置overflow: hidden; 这样右边就触发了BFCBFC的区域不会与浮动元素发生重叠,所以两侧就不会发生重叠。
 #container {
            background-color: aquamarine;
            width: 600px;
            height: 400px;
            margin-top: 135px;
        }
        .left {
            background-color: rgb(36, 157, 48);
            width: 200px;
            float: left;
            height: 300px;
        }
        .right {
            background-color: yellow;
            width: auto;
            overflow: hidden;
            height: 200px;
        }

3.flex布局

左侧固定宽,右侧flex:1

.left {
            background-color: rgb(36, 157, 48);
            width: 200px;
            float: left;
            height: 300px;
        }
        .right {
            background-color: yellow;
            width: auto;
            overflow: hidden;
            height: 200px;
            flex:1
        }

4.利用绝对定位

  • 利用绝对定位,将父级元素设置为相对定位
  • 左边元素设置为absolute定位,并且固定宽度设置为200px,left值设置为0。
  • 将右边元素的left值设置为左边固定宽度200px,right值设置为0。

.left {
            background-color: rgb(36, 157, 48);
            width: 200px;
            height: 300px;
            position: absolute;
            left: 0;
        }
        .right {
            background-color: yellow;
            width: auto;
            overflow: hidden;
            height: 200px;
            position: absolute;
            left: 200px;
            width: 100px;
        }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值