首页 > 其他分享 >flex常用布局

flex常用布局

时间:2022-09-06 18:24:30浏览次数:87  
标签:flex 常用 元素 color 布局 height width background

公共样式:

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

        .has-flex {
            display: flex;
        }
    </style>

 垂直居中 子元素左右分布

css

        .father-one {
            width: 100%;
            height: 200px;
            background-color: #fffcef;
            align-items: center; /*纵轴)方向上的对齐方式。*/
            justify-content: space-between; /* 均匀排列每个元素 首个元素放置于起点,末尾元素放置于终点,中间元素的中间间隔相等 */
            justify-content: space-around; /* 均匀排列每个元素 每个元素周围分配相同的空间 */
            justify-content: space-evenly; /* 均匀排列每个元素   每个元素之间的间隔相等 */
        }

        .fa-one-child1 {
            height: 30px;
            width: 30px;
            background-color: #a6acde;
        }

        .fa-one-child2 {
            height: 40px;
            width: 40px;
            background-color: #e4b9f0;
        }

        .fa-one-child3 {
            height: 50px;
            width: 50px;
            background-color: #f3b009;
        }

        .fa-one-child4 {
            height: 60px;
            width: 60px;
            background-color: #f77c4f;
        }

html

<!--垂直居中 子元素左右分布 star-->
<h3>垂直居中 子元素左右分布</h3>
<div class="father-one has-flex">
    <div class="fa-one-child1"></div>
    <div class="fa-one-child2"></div>
    <div class="fa-one-child3"></div>
    <div class="fa-one-child4"></div>
</div>
<!--垂直居中 子元素左右分布 end-->

水平垂直居中

css

        .father-two {
            width: 100%;
            height: 200px;
            align-items: center; /*纵轴)方向上的对齐方式。*/
            justify-content: center; /*    横轴)方向上的对齐方式*/
            background-color: red;
        }

        .child {
            width: 50%;
            height: 60px;
            background-color: rosybrown;
        }

html

<!--水平垂直居中 star-->
<h3>水平垂直居中</h3>
<div class="father-two has-flex">
    <div class="child"></div>
</div>
<!--水平垂直居中 end-->

 水平垂直居中 图标在上文字在下

css

        .father-three {
            height: 80px;
            background-color: #f77c4f;
            align-items: center;
            justify-content: center;
            flex-direction: column;
        }

html

<!--水平垂直居中 图标在上文字在下 star-->
<h3>水平垂直居中 图标在上文字在下</h3>
<div class="has-flex father-three">
    <i class="fa fa-file-text-o" aria-hidden="true"></i>
    <p>测试</p>
</div>
<!--水平垂直居中 图标在上文字在下 end-->

子元素平分父元素,且自适应等高

 css

        .father-four {
            background-color: #ffd5eb;
        }

        .fa-four-child {
            flex: 1;
            text-align: center;
            background-color: #ffffff;
            border: 1px solid;
        }

html

<!--子元素平分父元素,且自适应等高   star-->
<h3>子元素平分父元素,且自适应等高 </h3>
<div class="has-flex father-four">
    <div class="fa-four-child">第一个</div>
    <div class="fa-four-child">第二个</div>
    <div class="fa-four-child">第三个</div>
    <div class="fa-four-child" style="height: 90px">第四个</div>
</div>
<!--子元素平分父元素,且自适应等高  end-->

 子元素平分父元素,两边对齐中间自适应相同宽度间隔,且自动换行

css

        .father-five {
            height: 100px;
            background-color: #a6acde;
            justify-content: space-between;
            flex-wrap: wrap;
        }

        .fa-five-child {
            width: 21%;
            background-color: #f77c4f;
        }

html

<!--子元素平分父元素,两边对齐中间自适应相同宽度间隔,且自动换行   star-->
<h3>子元素平分父元素,两边对齐中间自适应相同宽度间隔,且自动换行 </h3>
<div class="has-flex father-five">
    <div class="fa-five-child">第一个</div>
    <div class="fa-five-child">第二个</div>
    <div class="fa-five-child">第三个</div>
    <div class="fa-five-child">第四个</div>
    <div class="fa-five-child">第五个</div>
    <div class="fa-five-child">第六个</div>
    <div class="fa-five-child">第七个</div>
    <div class="fa-five-child">第八个</div>
</div>
<!--子元素平分父元素,两边对齐中间自适应相同宽度间隔,且自动换行  end-->

 三栏布局,两边固定宽中间自适应

css

        .father-six {
            height: 100px;
        }

        .one-child,
        .three-child {
            width: 300px;
            background-color: #a6acde;
        }

        .two-child {
            flex: 1;
            background-color: #eeeeee;
        }

html

<!--三栏布局,两边固定宽中间自适应   star-->
<h3>三栏布局,两边固定宽中间自适应 </h3>
<div class="has-flex father-six">
    <div class="fa-six-child one-child">第一个</div>
    <div class="fa-six-child two-child">第二个</div>
    <div class="fa-six-child three-child">第三个</div>
</div>
<!--三栏布局,两边固定宽中间自适应  end-->

对于Flex 布局更详细的讲解请移步:Flex 布局教程:语法篇

相关css面试题:两边宽度已知,如何让中间自适应 

标签:flex,常用,元素,color,布局,height,width,background
From: https://www.cnblogs.com/yingzi1028/p/16662838.html

相关文章

  • 06-Nginx常用命令汇总
    Nginx常用命令解析命令作用./nginx启动./nginx-sstop停止(不推荐),直接停止./nginx-squit退出(推荐),不再接收新的请求,处理完当......
  • IOTDB 常用操作
    1、查看版本  SHOWversion2、查看所有设备 SHOWdevices3、分页查看root.xxx.yyy表数据#时间戳升序,首条下标为0,取n条SELECT*FROMroot.xxx.yyynOFFSET04、......
  • 常用的提高读写效率的数据结构:哈希表,有序数组,搜索树
    哈希表:key-value的存储结构,把值放在数组中,用一个哈希函数把key换算成确定的位置,然后把value放在数组的这个位置,不可避免多个key值经过哈希算法后出现同一个值的情况,处理这......
  • HTML常用标签
    已经正式开始JavaWeb的学习了,但是感觉我们的起步有点晚,可能因为我们学习的重点在网页后台的原因?现在才学网页的知识。之前也看了些视频,但是前端知识确实好多没记住,所以写一......
  • Linux - 常用命令
     #查看系统版本uname-acat/proc/versioncat/etc/redhat-release #查看当前目录文件大小du--max-depth=1-hdu-sh #更新时区cp/u......
  • Docker - 常用命令
     #查看容器#查看活跃容器dockerps#查看所有容器(包括已关闭)dockerps-a#筛选指定名称和状态的容器dockerps-q--filtername=.*bingohuang.*--......
  • anaconda 常用命令
    一、环境1.1创建新环境condacreate-nyour_env_namepython=X.X(2.7\3.7等)1.2查看anaconda环境信息查看已经创建了哪些anaconda环境(星号代表当前环境)condainf......
  • Debian 10 Buster 国内常用镜像源
    Debian10Buster国内常用镜像源如何更新系统安装源一般我们修改/etc/apt/sources.list文件,将Debian的默认源地址改成新的地址即可,比如将http://deb.debian.org改......
  • Django ORM 常用字段和表查询
    表查询数据准备及测试环境搭建sqllite3django自带sqllite3小型数据库该数据库功能非常有限,并且针对日期类型的数据兼容性很差django切换MySQL数据django1.x版本的......
  • Windows/Mac OS常用快捷键汇总
    Windows/MacOS常用快捷键汇总目录Windows/MacOS常用快捷键汇总基本的快捷键文本编辑文本排版Web浏览器转自(Windows和Mac的键盘快捷键)[https://www.runoob.com/ta......