三栏布局:左右固定,中间自适应的几种方式

1、flex布局(强烈推荐)

实现方式:左右两栏设置宽度,中间栏设置 flex:1,占满余下部分

<!DOCTYPE html>
<html lang="en">

<head>
    <title>flex布局</title>
    <style>
        .main{
            height: 60px;
            display: flex;
        }

        .left,
        .right{
            height: 100%;
            width: 200px;
            background-color: #ccc;
        }

        .content{
            flex: 1;
            background-color: #eee;
        }
    </style>
</head>

<body>
    <div class="main">
        <div class="left"></div>
        <div class="content"></div>
        <div class="right"></div>
    </div>
</body>

</html>

2、grid布局

实现方式:左右两栏设置宽度,中间栏宽度auto

<!DOCTYPE html>
<html lang="en">

<head>
    <title>grid布局</title>
    <style>
        body {
            display: grid;
            grid-template-columns: 200px auto 200px;
            grid-template-rows: 60px;
        }

        .left,
        .right {
            background-color: #ccc;
        }

        .content {
            background-color: #eee;
        }
    </style>
</head>

<body>
    <div class="left"></div>
    <div class="content"></div>
    <div class="right"></div>
</body>

</html>

3、magin负值法

实现方式:左右两栏均左浮动,中间栏外层盒子设置浮动,中间栏设置左右两栏宽度的margin值,左栏设置margin -100%,右栏设置 margin值为负的盒子宽度。

<!DOCTYPE html>
<html lang="en">

<head>
    <title>margin负值</title>

    <style>
        .left,
        .right {
            float: left;
            width: 200px;
            height: 60px;
            background-color: #eee;
        }

        .left {
             margin-left: -100%;
        }

        .right {
            margin-left: -200px;
        }

        .main {
            width: 100%;
            float: left;
            height: 60px;
        }

        .content {
            height: 60px;
            margin: 0 200px;
            background-color: #ccc;
        }
    </style>
</head>

<body>
    <div class="main">
        <div class="content"></div>
    </div>
    <div class="left"></div>
    <div class="right"></div>
</body>

</html>

4、自身浮动法

实现方式:左栏左浮动,右栏右浮动,中间栏放最后不浮动,设置左右margin值

<!DOCTYPE html>
<html lang="en">

<head>
    <title>自身浮动法</title>
    <style>
        .left,
        .right {
            height: 60px;
            width: 200px;
            background-color: #eee;
        }

        .left {
            float: left;
        }

        .right {
            float: right;
        }

        .content{
            height: 60px;
            background-color: #ccc;
            margin: 0 200px;
        }
    </style>
</head>

<body>
    <div class="left"></div>
    <div class="right"></div>
    <div class="content"></div>
</body>

</html>

5、绝对定位法

实现方式:左右两栏绝对定位,分别定位到盒子的两侧,中间栏采用margin值撑开盒子

注意:采用定位时,浏览器默认的padding或margin值会影响布局,需要初始化样式 margin:0;padding:0;

<!DOCTYPE html>
<html lang="en">

<head>
    <title>绝对定位法</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .left,
        .right {
            position: absolute;
            height: 60px;
            width: 200px;
            background-color: #ccc;
            top: 0;
        }

        .left {
            left: 0;
        }

        .right {
            right: 0;
        }

        .content {
            height: 60px;
            margin: 0 200px;
            background-color: #eee;
        }
    </style>
</head>

<body>
    <div class="left"></div>
    <div class="content"></div>
    <div class="right"></div>
</body>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浅墨、离殇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值