zoukankan      html  css  js  c++  java
  • flex实现常见布局

    flex相信大家都不陌生,对其各个属性都知道,不同属性的搭配能实现我们常见的复杂布局,可以说简单粗暴了~

    1、topbar + main + footbar

    html代码

    <div class="container">
         <header>header...</header>
         <main>内容</main>
         <footer>footer...</footer>
    </div>

    css代码

    header{
      height:100px;
      background:#0de294;
    }
    footer{
      height:100px;
      background:#0de294;
    }
    .container{
      display:flex;
      flex-direction:column;
      height:100vh;//屏幕高度的100 %
    }
    main{
      flex-grow:1;//实现了main自动填充剩余空间
    }

    2、元素的水平垂直居中

    html代码

    <div class="container">
        <div class="inner">center</div>
    </div>

    css代码

    .container{
      height:300px;
      width:300px;
      border:1px solid #0de294;
      display:flex;
      justify-content:center;  //水平居中
      align-items:center;   //垂直居中
    }
    
    .inner{
      color: #0de294;
    }

    3、圣杯布局

    html代码

    <div class="container">
           <main>main</main>
           <aside>aside</aside>
           <nav>nav</nav>
    </div>

    css代码

    .container{
      display:flex;
      height:100vh;
    }
    aside{
      width:50px;
      background:#0de294;
    }
    main{
      flex-grow:1;
    }
    nav{
      width:80px;
      background:#0de294;
      order:-1;//使得order处于最左侧(html中main写在了最前,以利于优先加载主内容区)
    }

    4、平均分配空间的栅格布局

    html 代码

    <div class="row">
             <div class="column">1</div>
             <div  class="column">2</div>
             <div  class="column">3</div>
    </div>

    css代码

    .row{
      display:flex;
      flex-wrap:wrap;
      border:1px solid #0de294;
    }
    .column{
      list-style:none;
      flex:1;//相当于flex:1 1 0%,而之所以不管各个column元素内容的宽度为多大,都能均分到相等的空间,正是因为相当于在设置了flex-grow:1使得剩余空间按相等比例自动分配的同时又设置了flex-basis:0%,才使得整个空间都平均分配了。
      height:100px;
      border:1px solid black;
    }
    码农随笔防失忆,只为记录风雨路上的脚丫印印~
  • 相关阅读:
    EM
    解决使用bootstrap modal时,icon-picker组件被遮挡问题
    记一次cpu100%问题排查过程
    docker安装nexus搭建maven私库
    记一次bootstrap table使用中的遭遇
    数据的逻辑结构与存储结构的基本概念(数据结构巩固一)
    前端js函数中直接获取springmvc后台model中传值
    springboot+vue部署后提示找不到css
    将springboot的jar包添加到windows服务及遇到的问题及其解决思路
    springboot打包jar后读取资源文件
  • 原文地址:https://www.cnblogs.com/bella-lin/p/9111727.html
Copyright © 2011-2022 走看看