css常见双栏和三栏布局

左侧固定右侧自适应

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>左侧固定右侧自适应</title>
</head>
<style>
    /*方法1*/
    /* 1、将左侧div浮动,右侧div设置margin-left */
    .outer {
        overflow: hidden;
        border: 1px solid red;
    }

    .sidebar {
        float: left;
        width: 200px;
        height: 150px;
        background: #BCE8F1;
    }

    .content {
        margin-left: 200px;
        height: 100px;
        background: #F0AD4E;
    }

    /*方法2*/
    /* 2、固定区采用绝对定位,自适应区设置margin */
    .outer2 {
        position: relative;
        height: 150px;
        border: 1px solid red;
    }

    .sidebar2 {
        position: absolute;
        left: 0;
        top: 0;
        width: 200px;
        height: 100%;
        background: #BCE8F1;
    }

    .content2 {
        margin-left: 200px;
        height: 100px;
        background: #F0AD4E;
    }

    /*方法3*/
    /* table布局 */
    .outer3 {
        display: table;
        width: 100%;
        border: 1px solid red;
    }

    .sidebar3 {
        display: table-cell;
        width: 200px;
        height: 150px;
        background: #BCE8F1;
    }

    .content3 {
        display: table-cell;
        height: 100px;
        background: #F0AD4E;
    }

    /*方法4*/
    /* 双float + calc()计算属性 */
    .outer4 {
        overflow: hidden;
        border: 1px solid red;
    }

    .sidebar4 {
        float: left;
        width: 200px;
        height: 150px;
        background: #BCE8F1;
    }

    .content4 {
        float: left;
        width: calc(100% - 200px);
        height: 100px;
        background: #F0AD4E;
    }

    /*方法5*/
    /* float + BFC方法 */
    .outer6 {
        overflow: auto;
        border: 1px solid red;
    }

    .sidebar6 {
        float: left;
        width: 200px;
        height: 150px;
        background: #BCE8F1;
    }

    .content6 {
        overflow: auto;
        height: 100px;
        background: #F0AD4E;
    }

    /*方法6*/
    /* flex */
    .outer7 {
        display: flex;
        border: 1px solid red;
    }

    .sidebar7 {
        flex: 0 0 200px;
        /* width: 200px; */
        height: 150px;
        background: #BCE8F1;
    }

    .content7 {
        flex: 1;
        height: 100px;
        background: #F0AD4E;
    }
</style>

<body>
    <div class="outer6">
        <div class="sidebar6">固定宽度区(sideBar)</div>
        <div class="content6">自适应区(content)</div>
    </div>
    <div class="footer">footer</div>
</body>

</html>

三栏布局左右固定宽度中间自适应

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>三栏布局左右固定宽度中间自适应</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .layout article div{
            min-height: 100px;
        }
    </style>
</head>
<body>
    <section class="layout layout1">
        <style>
            .layout1 .left{
                float: left;
                width: 300px;
                background-color:red;
            }
            .layout1 .right{
                float: right;
                width: 300px;
                background-color:blue;
            }
            .layout1 .center{
                margin-right: 300px;
                margin-left: 300px;
                background-color: yellow;
            }
        </style>
        <article class="left-right-center">
            <div class="left"></div>
            <div class="right"></div>
            <div class="center">
                <h1>浮动解决方案</h1>
                1、这是三栏布局中间部分
                1、这是三栏布局中间部分
                <br>缺点:需要清除浮动
                <br>优点:兼容性好
            </div>
        </article>
    </section>
    <section class="layout layout2">
        <style>
            .layout2 .left-center-right>div{
                margin-top: 20px;
                position: absolute;
            }
            .layout2 .left{
                left: 0;
                width: 300px;
                background-color: red;
            }
            .layout2 .center{
                left: 300px;
                right: 300px;
                background-color: yellow;
            }
            .layout2 .right{
                right: 0;
                width: 300px;
                background-color: blue;
            }
        </style>
        <article class="left-center-right">
            <div class="left"></div>
            <div class="center">
                <h2>绝对定位解决方案</h2>
                1、这是三栏布局中间部分
                1、这是三栏布局中间部分
                <br>缺点:脱离文档流
                <br>优点:快捷
            </div>
            <div class="right"></div>
        </article>
    </section>

    <section class="layout layout3">
            <style>
                .layout3 .left-center-right{
                    display: flex;
                    margin-top: 200px;
                }
                .layout3 .left{
                    width: 300px;
                    background-color: red;
                }
                .layout3 .center{
                    flex:1;
                    background-color: yellow;
                }
                .layout3 .right{
                    width: 300px;
                    background-color: blue;
                }
            </style>
            <article class="left-center-right">
                <div class="left"></div>
                <div class="center">
                    <h2>flexbox解决方案</h2>
                    1、这是三栏布局中间部分
                    1、这是三栏布局中间部分
                    <br>完美
                </div>
                <div class="right"></div>
            </article>
        </section>

        <section class="layout layout4">
                <style>
                    .layout4 .left-center-right{
                        width: 100%;
                        display: table;
                        height: 100px;
                        margin-top: 50px;
                    }
                    .layout4 .left-center-right>div{
                        display: table-cell;
                    }
                    .layout4 .left{
                        width: 300px;
                        background-color: red;
                    }
                    .layout4 .center{
                        background-color: yellow;
                    }
                    .layout4 .right{
                        width: 300px;
                        background-color: blue;
                    }
                </style>
                <article class="left-center-right">
                    <div class="left"></div>
                    <div class="center">
                        <h2>表格布局解决方案</h2>
                        1、这是三栏布局中间部分
                        1、这是三栏布局中间部分
                        <br>兼容性好
                        <br>缺点:高度跟着变
                    </div>
                    <div class="right"></div>
                </article>
            </section>
            去掉高度已知哪个不适用:
            flex和table能用,会自动撑开
</body>
</html>
posted @ 2019-09-02 19:41  樱风凛  阅读(1600)  评论(0编辑  收藏  举报