• 三栏布局的几种方法


    三栏布局是一个比较常见的布局,在高度固定的情况下,左右各留出300px,中间自适应,可以有以下的几种写法。

    比较常见的有浮动布局和绝对定位布局,但是flex布局现在也非常普遍了,Grid布局可能兼容性不太好,但也不失为一种方法。


    因为下面的css代码中有许多的相同的地方,我们先我们先设置一些基本的css代码,用作左中右的颜色识别和高度的设置:

    基本的css代码:

                *{margin: 0; padding: 0;}
    
                .layout {
                    margin-top: 20px;
                }
                .layout article div {
                    min-height: 100px;
                }
                .left {
                    background: red;
                }
                .right {
                    background: green;
                }
                .center {
                    background: #ddd;
                }

    浮动布局(主要利用的就是浮动脱离文档流的特点):

      html代码:

        <section class="layout float">
                <article class="left-center-right">
                    <div class="left"></div>
                    <div class="right"></div>
                    <div class="center">
                        <h2>浮动解决方案</h2>
                        <p>这是三栏布局的中间部分这是三栏布局的中间部分</p>
                    </div>
                </article>
        </section>

      css代码:

             .layout.float .left {
                    float: left;
                    width: 300px;
                }
                .layout.float .right {
                    float: right;
                    width: 300px;
                }
                .layout.float .center {
                    margin: 0 300px 0 300px;
                }

    绝对定位布局(与浮动定位布局的原理基本相同):

    html代码:

            <article class="left-center-right">
                    <div class="left"></div>
                    <div class="right"></div>
                    <div class="center">
                        <h2>绝对定位解决方案</h2>
                        <p>这是三栏布局的中间部分这是三栏布局的中间部分</p>
                    </div>
                </article>

    css代码:

            .layout.absolute .left {
                    position: absolute;
                    left: 0;
                    width: 300px;
                }
                .layout.absolute .center {
                    margin: 0 300px 0 300px;
                }
                .layout.absolute .right {
                    position: absolute;
                    right: 0;
                    width: 300px;
                }          

    弹性盒子布局:

    这里用到了flex的一个简写方式。

    flex属性是flex-growflex-shrink 和 flex-basis的简写,默认值为0 1 auto。后两个属性可选。  --阮一峰《Flex布局教程》

    flex-grow定义项目的放大比例。

    如果flex-grow为1,那么它将占据项目的剩余空间,如果有许多个子项目,则他们将等分剩余项目空间。如果不清楚的,建议去看阮一峰老师的Flex布局教程,里面讲的很清晰。

     

    html代码:

         <section class="layout flex">
                <article class="left-center-right">
                    <div class="left"></div>
                    <div class="center">
                        <h2>flex解决方案</h2>
                        <p>这是三栏布局的中间部分这是三栏布局的中间部分</p>
                    </div>
                    <div class="right"></div>
                </article>
            </section>

     

    css代码:

            .layout.flex .left-center-right{
                    display: flex;
                }
                .layout.flex .left {
                    width: 300px;
                }
                .layout.flex .center {
                    flex: 1;
                }
                .layout.flex .right {
                    width: 300px;
                }

    Grid布局(兼容性较差)

    grid布局则可以直接控制二维布局,直接设定横向和纵向的布局方式。

    html代码:

         <section class="layout grid">
                <article class="left-center-right">
                    <div class="left"></div>
                    <div class="center">
                        <h2>Grid布局解决方案</h2>
                        <p>这是三栏布局的中间部分这是三栏布局的中间部分</p>
                    </div>
                    <div class="right"></div>
                </article>
            </section>

    css代码:

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

     

  • 相关阅读:
    java项目配置域名(tomcat直接配置 or 使用nginx反向代理)
    java爬虫学习
    python爬虫
    log4j的日志级别(ssm中log4j的配置)
    Python中list,tuple,dict,set的区别和用法
    [转]C# Eval在asp.net中的用法及作用
    【转】一个运维经理的运维经验总结
    [转]使用 LVS 实现负载均衡原理及安装配置详解
    [转]33个网站足以使你成为一个天才
    搜狐邮箱想说爱你不容易!
  • 原文地址:https://www.cnblogs.com/bax-life/p/8485934.html
Copyright © 2020-2023  润新知