两列布局,三列布局的实现

本文详细介绍了如何使用CSS实现两列和三列布局。对于两列布局,探讨了利用BFC和绝对定位的方法;对于三列布局,分别讲解了两边固定中间自适应的两种实现方式,包括绝对定位和浮动的运用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、两列布局

1、利用BFC

<div class="left">
    left
</div>
<div class="right">
    right
</div>
* { padding: 0; margin: 0;}
.left{
    float: left;
    width: 200px;
    height: 800px;
    background-color: blueviolet;
}
.right{
    overflow: hidden;
    height: 800px;
    background-color: blue;
}

2、利用绝对定位

* { padding: 0; margin: 0;}
.left{
    position: absolute;
    top: 0;
    left: 0;
    width: 200px;
    height: 800px;
    background-color: blueviolet;
}
.right{
    height: 800px;
    background-color: blue;
    margin-left: 200px;
}

二、三列布局

1、两边固定,中间自适应

(1)使用绝对定位

<div class="left">
    left
</div>
<div class="center">center</div>
<div class="right">
    right
</div>
* { padding: 0; margin: 0;}
.left{
    position: absolute;
    left: 0;
    top: 0;
    width: 200px;
    height: 800px;
    background-color: blueviolet;
}
.center{
    width:auto;
    height: 800px;
    background-color: green;
}
.right{
    position: absolute;
    right: 0;
    top: 0;
    width: 200px;
    height: 800px;
    background-color: blue;
 }

(2)利用浮动

<div class="left">
    left
</div>
<div class="right">
    right
</div>
<div class="center">center</div>
* { padding: 0; margin: 0;}
.left{
    float: left;
    width: 200px;
    height: 800px;
    background-color: blueviolet;
}
.center{
    width: auto;
    height: 800px;
    background-color: green;
    margin-left: 200px;
    margin-right: 200px;
}
.right{
    float: right;
    width: 200px;
    height: 800px;
    background-color: blue;
}

2、中间固定,两边宽度自适应

<div id="left">
    <div>left</div>
</div>
<div id="center">center</div>
<div id="right">
    <div>right</div>
</div>
* { padding: 0; margin: 0;}
#center { width: 600px; background: red; margin: 0 auto;height: 800px}
#left { position: absolute; top: 0; left: 0;width: 50%;}
#right { position: absolute; top: 0; right: 0; width: 50%;}
#left div { margin-right: 300px; position: relative; background: lime;height: 800px}
#right div { margin-left: 300px; position: relative; background: lime;height: 800px}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值