css基本布局的实现

先从一个题目出发:
假设高度已知,请写出三栏布局,其中左栏、右栏 宽度各为300px,中间自适应。

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

思考:一共有多少种实现方式?
不废话,直接上代码:

<style>
    * {
        margin: 0;
        padding: 0;
    }

    .layout article div {
        min-height: 100px;
    }
</style>
<body>
    <!-- 三栏布局:左右宽度固定,中间自适应  start -->
    <section class="layout float">
        <style>
            .layout.float .left {
                float: left;
                width: 300px;
                background: red;
            }

            .layout.float .right {
                float: right;
                width: 300px;
                background: blue;
            }

            .layout.float .center {
                background: pink;
            }
        </style>
        <article class="left-right-center">
            <div class="left"></div>
            <div class="right"></div>
            <div class="center">
                <h1>浮动解决方案</h1>
                1.中间部分
                2.中间部分
            </div>
        </article>
    </section>
    <section class="layout absolute">
        <style>
            .layout.absolute {
                margin-top: 20px;
            }

            .layout.absolute .left-right-center>div {
                position: absolute;
            }

            .layout.absolute .left {
                left: 0;
                width: 300px;
                background: red;
            }

            .layout.absolute .center {
                left: 300px;
                right: 300px;
                background: pink;
            }

            .layout.absolute .right {
                right: 0;
                width: 300px;
                background: blue;
            }
        </style>
        <article class="left-right-center">
            <div class="left"></div>
            <div class="center">
                <h1>绝对定位解决方案</h1>
                1.中间部分
                2.中间部分
            </div>
            <div class="right"></div>
        </article>
    </section>
    <section class="layout flex">
        <style>
            .layout.flex {
                margin-top: 140px;
            }

            .layout.flex .left-right-center {
                display: flex;

            }

            .layout.flex .left {

                width: 300px;
                background: red;
            }

            .layout.flex .center {
                flex-grow: 1;
                background: pink;
            }

            .layout.flex .right {

                width: 300px;
                background: blue;
            }
        </style>
        <article class="left-right-center">
            <div class="left"></div>
            <div class="center">
                <h1>flex定位解决方案</h1>
                1.中间部分
                2.中间部分
            </div>
            <div class="right"></div>
        </article>
    </section>
    <section class="layout table">
        <article class="left-center-right">
            <style>
                .layout.table {
                    margin-top: 20px;
                }

                .layout.table .left-center-right {
                    width: 100%;
                    display: table;
                    height: 100px;
                }

                .layout.table .left-center-right>div {
                    display: table-cell;
                }

                .layout.table .left {
                    width: 300px;
                    background: red;
                }

                .layout.table .center {
                    background: pink;
                }

                .layout.table .right {
                    width: 300px;
                    background: blue;
                }
            </style>
            <div class="left"></div>
            <div class="center">
                <h1>表格布局解决方案</h1>
                1.中间部分
                2.中间部分
            </div>
            <div class="right"></div>
        </article>
    </section>
    <section class="layout grid">
        <article class="left-center-right">
            <style>
                .layout.grid {
                    margin-top: 20px;
                }

                .layout.grid .left-center-right {
                    display: grid;
                    width: 100%;
                    grid-template-rows: 100px;
                    grid-template-columns: 300px auto 300px;
                }

                .layout.grid .left {
                    background: red;
                }

                .layout.grid .center {
                    background: pink;
                }

                .layout.grid .right {
                    background: blue;
                }
            </style>
            <div class="left"></div>
            <div class="center">
                <h1>grid解决方案</h1>
                1.中间部分
                2.中间部分
            </div>
            <div class="right"></div>
        </article>
    </section>
    <!-- 三栏布局:左右宽度固定,中间自适应  end -->
</body>

页面展示效果如图:


image.png

思考:这五中解决方案的优缺点各是什么,如果高度不已知,哪个方案不适用了,兼容性如何?
1.浮动:清除浮动处理的好的话,兼容性好。
2.绝对定位:快捷,但布局脱离文档流,下面所有子元素也都脱离文档流。
3.flex:比较完美的解决方案。
4.table:兼容性较好,但场景应用受限。
5.grid:简单的代码可实现复杂的场景,但兼容性不好。
超出高度的情况下,flex布局和table布局仍可使用,其他的不可。
由此问题引出:

三栏布局——上下高度固定,中间自适应如何实现?

 <!-- 三栏布局:上下高度固定,中间自适应  start -->
    <section class="container absolute">
        <style>
            .container.absolute .top-center-bottom>div {
                position: absolute;
                width: 100%;
            }

            .container.absolute .top {
                top: 0;
                height: 100px;
                background: blue;
            }

            .container.absolute .center {
                top: 100px;
            }

            .container.absolute .bottom {
                bottom: 0;
                height: 100px;
                background: gray;
            }
        </style>
        <article class="top-center-bottom">
            <div class="top">头部</div>
            <div class="center">
                <h1>内容</h1>
                <p>很多很多</p>
            </div>
            <div class="bottom">底部</div>
        </article>
    </section>

    <section class="container flex">
        <style>
            .container.flex .top-center-bottom {
                display: flex;
                flex-direction: column;
                height: 100vh;
            }

            .container.flex .top {
                height: 100px;
                background: blue;
            }

            .container.flex .center {
                flex-grow: 1;
            }

            .container.flex .bottom {
                height: 100px;
                background: gray;
            }
        </style>
        <article class="top-center-bottom">
            <div class="top">头部</div>
            <div class="center">
                <h1>内容</h1>
                <p>很多很多</p>
            </div>
            <div class="bottom">底部</div>
        </article>
    </section>
    <!-- 三栏布局:上下高度固定,中间自适应  end -->

两栏布局——左宽度固定,右自适应

<!-- 两栏布局:左宽度固定,右自适应 start -->
    <section class="layout float">
        <style>
            .layout.float .left {
                width: 300px;
                float: left;
                background: red;
            }

            .layout.float .right {
                margin-left:300px;
                background: blue;
            }
        </style>
        <article class="left-right">
            <div class="left">左侧</div>
            <div class="right">右侧</div>
        </article>
    </section>

    <section class="layout absolute">
        <style>
            .layout.absolute .left {
                position: absolute;
                width: 300px;
                left: 0;
                top: 0;
                background: red;
            }

            .layout.absolute .right {
                margin-left: 300px;
                background: blue;
            }
        </style>
        <article class="left-right">
            <div class="left">左侧</div>
            <div class="right">右侧</div>
        </article>
    </section>
    <!-- 两栏布局:左宽度固定,右自适应 end -->
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • CSS 是什么 css(Cascading Style Sheets),层叠样式表,选择器{属性:值;属性:值}h...
    崔敏嫣阅读 1,513评论 0 5
  • 为了提高工作效率,不在CSS布局上多次耗费时间,故记录此练习笔记。借鉴了大神【浪里行舟】的博客,文末有链接。 CS...
    月上秦少阅读 1,140评论 2 13
  • # 前言   布局方式是每个项目都必须使用到的,大部分新手看到设计稿的第一反应是: 从左到右,从上到下依次制作。对...
    果汁凉茶丶阅读 591评论 0 4
  • 转载自:https://segmentfault.com/a/1190000013565024 前端布局非常重要的...
    天字一等阅读 444评论 0 5
  • 环境要求: python3.5,tensorflow-GPU环境,依赖的库,根据需要安装 (环境配置建议:...
    ZeroZhou阅读 1,285评论 0 0