• CSS弹性盒模型flex在布局中的应用


      前面已经详细介绍过flex弹性盒模型的基本语法兼容写法,本文将介绍flex在布局中的应用

    元素居中

    【1】伸缩容器上使用主轴对齐justify-content和侧轴对齐align-items

    复制代码
    <style>
    .parent{
        display: flex;
        justify-content: center;
        align-items: center;
    }
    </style>
    复制代码
    <div class="parent"  style=" height: 100px;  200px;">
        <div class="in" style="">DEMO</div>      
    </div>

    【2】在伸缩项目上使用margin:auto

    复制代码
    <style>
    .parent{
        display: flex;
    }
    .in{
        margin: auto;
    }
    </style>
    复制代码
    <div class="parent"  style="height: 100px; 200px;">
        <div class="in" style="">DEMO</div>      
    </div>

    两端对齐

    复制代码
    <style>
    .parent{
        display: flex;
        justify-content:space-between
    }
    </style>
    复制代码
    复制代码
    <div class="parent"  style="height: 100px; 200px;">
        <div class="in" style="">DEMO</div>
        <div class="in" style="">DEMO</div>
        <div class="in" style="">DEMO</div>
        <div class="in" style="">DEMO</div>      
    </div>
    复制代码

    底端对齐

    复制代码
    <style>
    .parent{
        display: flex;
        align-items: flex-end;
    }
    </style>
    复制代码
    复制代码
    <div class="parent"  style="height: 100px; 200px;">
        <div class="in" style=" height:20px;">DEMO</div>
        <div class="in" style=" height:30px;">DEMO</div>
        <div class="in" style=" height:40px;">DEMO</div>
        <div class="in" style=" height:50px;">DEMO</div>      
    </div>
    复制代码

    输入框按钮

    复制代码
    <style>
    .inputBox{
        display: flex;
        width: 250px;
    }
    .inputBox-ipt{
        flex: 1;
    }
    </style>
    复制代码
    <div class="inputBox">
      <input class="inputBox-ipt">
      <button class="inputBox-btn">按钮</button>
    </div>

    等分布局

    复制代码
    <style>
    body,p{margin: 0;}
    .parent{
        display: flex;
    }
    .child{
        flex:1;
        height: 100px;
    }
    .child + .child{
        margin-left: 20px;
    }
    </style>
    复制代码
    复制代码
    <div class="parent" style="">
        <div class="child" style="">1</div>
        <div class="child" style="">2</div>
        <div class="child" style="">3</div>
        <div class="child" style="">4</div>                
    </div>    
    复制代码

    多列自适应布局

    复制代码
    <style>
    p{margin: 0;}
    .parent{display: flex;}
    .left,.center{margin-right: 20px;}
    .right{flex: 1;}
    </style>
    复制代码
    复制代码
    <div class="parent" style="">
        <div class="left" style="">
            <p>left</p>
            <p>left</p>
        </div>            
        <div class="center" style="">
            <p>center</p>
            <p>center</p>
        </div>            
        <div class="right"  style="">
            <p>right</p>
            <p>right</p>
        </div>                    
    </div>
    复制代码

    悬挂布局

    复制代码
    <style>        
    .box{
        display: flex;
        background-color: lightgrey;
        width: 300px;
    }
    .left{
        margin-right: 20px;
        background-color: lightblue;
        height: 30px;
    }
    .main{
        flex:1;
    }
    </style>
    复制代码
    <div class="box">
        <div class="left">左侧悬挂</div>
        <div class="main">主要内容主要内容主要内容主要内容主要内容主要内容主要内容主要内容主要内容主要内容主要内容主要内容主要内容</div>    
    </div>

    全屏布局

    复制代码
    <style>
    body,p{margin: 0;}
    body,html,.parent{height: 100%;}
    .parent{
        display: flex;
        flex-direction: column;
    }
    .top,.bottom{
        height: 50px;
    }
    .middle{
        display: flex;
        flex: 1;
    }
    .left{
        width: 100px;
        margin-right: 20px;
    }
    .right{
        flex: 1;
        overflow: auto;
    }
    .right-in{
        height: 1000px;
    }
    </style>
    复制代码
    复制代码
    <div class="parent" id="parent" style="">
        <div class="top" style="">
            <p>top</p>
        </div> 
        <div class="middle" style="">
            <div class="left" style="">
                <p>left</p>
            </div>     
            <div class="right" style="">
                <div class="right-in">
                    <p>right</p>
                </div>            
            </div>                    
        </div>              
        <div class="bottom" style="">
            <p>bottom</p>
        </div>        
    </div>
    复制代码
    好的代码像粥一样,都是用时间熬出来的
     
     

  • 相关阅读:
    homebrew
    Flutter状态管理之provide和provider的使用区别
    Flutter Bloc状态管理 简单上手
    Flutter 路由传入中文参数报错无法push问题
    Flutter 一些常用第三方库、插件
    js替换字符串中的空格,换行符 或 替换成<br>
    Flutter状态管理Provider,简单上手
    Flutter Resolving dependencies...很慢解决办法
    Flutter布局--appbar导航栏和状态栏
    Flutter运行报错 `kernel_snapshot for errors` 解决方案
  • 原文地址:https://www.cnblogs.com/lzbk/p/6095996.html
Copyright © 2020-2023  润新知