页面布局

高度已知,三栏布局,左右各300px,中间自适应,能够实现的方法

1.float方法

     <section class="layout float">
        <style>
            .left-right-center>div{
                height:100px;
            }
            .layout.float .left{
                width: 300px;
                float:left;
                background:red;
            }
            .layout.float .right{
                width: 300px;
                float: right;
                background:yellow;
            }
            .layout.float .center{
                background: skyblue;
            }
        </style>
        <article class="left-right-center">
            <div class="left"></div>
            <div class="right"></div>
            <div class="center">float方法</div>
        </article>
    </section>

2.绝对定位

  <section class="layout position">
        <style>
            .layout.position .left-right-center>div{
                height: 100px;
                position: absolute;
            }
            .layout.position .left{
                left:0px;
                width:300px;
                background: red;
            }
            .layout.position .right{
                right:0;
                width: 300px;
                background:yellow;
            }
            .layout.position .center{
                left:300px;
                right: 300px;
                background:skyblue;
            }
        </style>
        <article class="left-right-center">
            <div class="left"></div>
            <div class="center">绝对定位</div>
            <div class="right"></div>
        </article>
    </section>

3.flex

     <section class="layout flex">
        <style>
            .layout.flex{
                margin-top: 140px;
            }
            .layout.flex .left-center-right{
                display:flex;
                height:100px;
            }
            .layout.flex .left{
                width: 300px;
                background:red;
            }
            .layout.flex .center{
                flex:1;
                background:skyblue;
            }
            .layout.flex .right{
                width: 300px;
                background: yellow;
            }
        </style>
        <article class="left-center-right">
            <div class="left"></div>
            <div class="center">flex</div>
            <div class="right"></div>
        </article>
    </section>

4.table

<section class="layout table">
        <style>
            .layout.table .left-center-right{
                height:100px;
                width:100%;
                display: table;
            }
            .layout.table .left-center-right>div{
                display: table-cell;
            }
            .layout.table .left{
                width:300px;
                background: red;
            }
            .layout.table .center{
                background: skyblue;
            }
            .layout.table .right{
                width: 300px;
                background:yellow;
            }
        </style>
        <article class="left-center-right">
            <div class="left"></div>
            <div class="center">table布局</div>
            <div class="right"></div>
        </article>
    </section>

5.grid

<section class="layout grid">
        <style>
            .layout.grid .left-center-right{
                display: grid;
                grid-template-columns: 300px auto 300px;
                grid-template-rows: 100px;
            }
            .layout.grid .left{
                background: red;
            }
            .layout.grid .center{
                background: skyblue;
            }
            .layout.grid .right{
                background:yellow;
            }
        </style>
        <article class="left-center-right">
            <div class="left"></div>
            <div class="center">grid</div>
            <div class="right"></div>
        </article>
    </section>

ps:清除浮动

方案1:

overflow:hidden;

这样可以清除浮动,但是也存在较大弊端,如果父级内容超出会被隐藏

方案2:给浮动元素下方条件一个空的div,给div单独加个样式

  clear:both; 
  height:0; 
  overflow:hidden;

但是这个就需要在页面上新增标签,影响页面的加载

方案3:万能清除浮动法

::afer{ 
  content:' '; 
  clear: both; 
  height:0; 
  overflow:hidden; 
  visibility:hidden; 
  display:block;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 页面布局 题:假设高度已知,请写出三栏布局,其中左栏右栏宽各位300px,中间自适应。答: 7种方法1.浮动 2....
    noyanse阅读 542评论 0 0
  • 本文主要讲述页面布局样式方面涉及的知识点,更全面的对CSS相应的技术进行归类、整理、说明,没有特别详细的技术要点说...
    Joel_zh阅读 931评论 0 1
  • 题目: 假设高度已知,请写出三栏布局,其中左栏右栏高度各为300px,中间自适应 慕课网视频 能写出有几种方案? ...
    邢走在云端阅读 271评论 0 4
  • 说起前端开发,首先会想到页面布局,作为资深前端来说,用页面布局来谈前端可能会认为太浅显,但作为前端开发中最基础的技...
    希缌婷阅读 407评论 0 1
  • 页面布局 题目:假设高度已知,请写出三栏布局,其中左栏、右栏宽度各为300px,中间自适应 1、float 绝对定...
    飞菲fly阅读 1,624评论 2 4