常见三栏布局方法总结

1.float

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>三栏布局</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .clearfix {
            zoom: 1;
        }

        .clearfix::after {
            display: block;
            content: ' ';
            clear: both
        }

        html,
        body {
            width: 100%;
            height: 100%
        }

        .container {
            width: 100%;
            height: 100%;
        }

        .container header {
            height: 80px;
            background: rgba(0, 0, 0, 0.5)
        }

        .container footer {
            height: 80px;
            background: rgba(0, 0, 0, 0.5)
        }

        .container .left {
            float: left;
        }

        .container .right {
            float: right;
        }

        .container .middle {
            height: calc(100% - 80px - 80px);
        }

        .container .middle aside {
            height: 100%;
            width: 300px;
            background: rgba(156, 154, 249, 1);
        }
    </style>
</head>

<body>
    <div class="container">
        <!-- 顶部Header -->
        <header>这里是头部</header>
        <!-- 中间aside及content -->
        <div class="middle clearfix">
            <aside class="left"></aside>
            <aside class="right"></aside>
            <!-- 中间content显示内容 为了防止将右侧挤下去故放置在右侧栏下方 -->
            <div class="content">这里是内容</div>
        </div>
        <!-- 底部Footer -->
        <footer></footer>
    </div>
</body>

</html>

2.绝对定位

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>三栏布局</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        html,
        body {
            width: 100%;
            height: 100%
        }

        .container {
            width: 100%;
            height: 100%;
        }

        .container header {
            height: 80px;
            background: rgba(0, 0, 0, 0.5)
        }

        .container footer {
            height: 80px;
            background: rgba(0, 0, 0, 0.5)
        }

        .container .middle {
            position: relative;
            height: calc(100% - 80px - 80px);
        }

        .container .content, .container aside {
            position: absolute;
        }

        .container .left {
            width: 300px;
            background: rgba(156, 154, 249, 1);
            left: 0;
            height: 100%;
        }

        .container .right {
            width: 300px;
            background: rgba(156, 154, 249, 1);
            right: 0;
            height: 100%;
        }
        .content {
            left: 300px;
            right: 300px;
        }
    </style>
</head>

<body>
    <div class="container">
        <!-- 顶部Header -->
        <header>这里是头部</header>
        <!-- 中间aside及content -->
        <div class="middle">
            <aside class="left"></aside>
            <div class="content">这里是内容</div>
            <aside class="right"></aside>
        </div>
        <!-- 底部Footer -->
        <footer></footer>
    </div>
</body>

</html>

3. flex

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>三栏布局</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        html,
        body {
            width: 100%;
            height: 100%
        }

        .container {
            width: 100%;
            height: 100%;
        }

        .container header {
            height: 80px;
            background: rgba(0, 0, 0, 0.5)
        }

        .container footer {
            height: 80px;
            background: rgba(0, 0, 0, 0.5)
        }

        .container .middle {
            display: flex;
            height: calc(100% - 80px - 80px);
        }

        .container aside {
            width: 300px;
            background: rgba(156, 154, 249, 1);
        }

        .content {
            flex: 1;
        }
    </style>
</head>

<body>
    <div class="container">
        <!-- 顶部Header -->
        <header>这里是头部</header>
        <!-- 中间aside及content -->
        <div class="middle">
            <aside class="left"></aside>
            <div class="content">这里是内容</div>
            <aside class="right"></aside>
        </div>
        <!-- 底部Footer -->
        <footer></footer>
    </div>
</body>

</html>

4.display:table

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>三栏布局</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        html,
        body {
            width: 100%;
            height: 100%
        }

        .container {
            width: 100%;
            height: 100%;
        }

        .container header {
            height: 80px;
            background: rgba(0, 0, 0, 0.5)
        }

        .container footer {
            height: 80px;
            background: rgba(0, 0, 0, 0.5)
        }

        .container .middle {
            display: table;
            width: 100%;
            height: calc(100% - 80px - 80px);
        }

        .container aside {
            width: 300px;
            display: table-cell;
            background: rgba(156, 154, 249, 1);
        }

        .content {
            display: table-cell;
        }
    </style>
</head>

<body>
    <div class="container">
        <!-- 顶部Header -->
        <header>这里是头部</header>
        <!-- 中间aside及content -->
        <div class="middle">
            <aside class="left"></aside>
            <div class="content">这里是内容</div>
            <aside class="right"></aside>
        </div>
        <!-- 底部Footer -->
        <footer></footer>
    </div>
</body>

</html>

5.display:grid

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>三栏布局</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        html,
        body {
            width: 100%;
            height: 100%
        }

        .container {
            width: 100%;
            height: 100%;
        }

        .container header {
            height: 80px;
            background: rgba(0, 0, 0, 0.5)
        }

        .container footer {
            height: 80px;
            background: rgba(0, 0, 0, 0.5)
        }

        .container .middle {
            display: grid;
            grid-template-columns: 300px auto 300px;
            height: calc(100% - 80px - 80px);
        }

        .container aside {
            background: rgba(156, 154, 249, 1);
        }
    </style>
</head>

<body>
    <div class="container">
        <!-- 顶部Header -->
        <header>这里是头部</header>
        <!-- 中间aside及content -->
        <div class="middle">
            <aside class="left"></aside>
            <div class="content">这里是内容</div>
            <aside class="right"></aside>
        </div>
        <!-- 底部Footer -->
        <footer></footer>
    </div>
</body>

</html>

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值