flexbox之前有接触,写项目时也用过,但也只是简单的,对其也是似懂非懂,所以今天下定决心把这个再学一遍,因为似懂非懂就是不懂

  本文主要是的demo演示,想看flexbox语法 请移步flexbox语法

  本文主要分两大部分:flexbox的Demo与常见的布局

  以下demo的图片与css代码从左至右从上到下一一对应

  flexbox的Demo

  单元素demo四个角

  

  html

  <div class='box-1'>
<span>一</span>
</div>

  css

       .box-1{
/* 一个元素 */
左上角
display: flex;
/* 右上角 */
justify-content: flex-end;
/* 左下角 */
align-items: flex-end;
/* 右下角 */
justify-content: flex-end;
align-items: flex-end;
}
  单元素居中

           

  html

  <div class='box-1'>
<span>一</span>
</div>

  css

      .box-1{
/* 顶部水平居中 */
justify-content: center;
/* 左垂直居中 */
align-items: center;
/* 右垂直居中 */
justify-content: flex-end;
align-items: center;
/* 垂直和水平居中 */
justify-content: center;
align-items: center; */
/* 底部水平居中 */
justify-content: center;
align-items: flex-end;
}
  二个元素对齐方式

  html 

  <div class='box-1'>
<span>一</span>
<span>二</span>
</div>

  css

     .box-1{
/* 二个元素 */
display: flex;
/* 水平两端对齐,项目之间的间隔都相等*/
justify-content: space-between;
/* 左垂直两端对齐,项目之间的间隔相等 flex-direction: column : 主轴为垂直方向,起点在上沿。*/
flex-direction: column;
justify-content: space-between;
/* 垂直居中两端对齐,项目之间的间隔相等 flex-direction: column : 主轴为垂直方向,起点在上沿。align-items这时就代表水平方向*/
flex-direction: column;
justify-content: space-between;
align-items: center;
/* 右垂直两端对齐,项目之间的间隔相等 */
flex-direction: column;
justify-content: space-between;
align-items: flex-end;
/* 水平居中两端对齐,项目之间的间隔都相等 */
justify-content: space-between;
align-items: center;
/* 底部两端对齐,项目之间的间隔都相等 */
justify-content: space-between;
align-items: flex-end;
}
/* 两元素左对角对齐 */
.box-1{
justify-content: space-between;
}
span:nth-child(2){
align-self: flex-end;
}
  三元素对齐

  

  html

  <div class='box-1'>
<span>一</span>
<span>二</span>
<span>三</span>
<div>

  css

    .box-1{
/* 三元素左对角对齐 */
justify-content: space-between;
/* 倒三角 */
justify-content: space-between;
}
/* 三元素左对角对齐 */
span:nth-child(2){
align-self: center;
}
span:nth-child(3){
align-self: flex-end;
}
/* 倒三角 */
span:nth-child(2){
align-self: flex-end;
}
  13个元素换行操作

  html

  <div class='box-1'>
<span>一</span>
<span>二</span>
<span>三</span>
<span>四</span>
<span>五</span>
<span>六</span>
<span>七</span>
<span>八</span>
<span>九</span>
<span>十</span>
<span>十一</span>
<span>十二</span>
<span>十三</span>
</div>

  css

    .box-1{
/* 十三元素换行操作 */
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
align-content: space-between;
}
  四个元素占满4个角

 html

  <div class="box-2">
<div class="column">
<span>1</span>
<span>2</span>
</div>
<div class="column">
<span>3</span>
<span>4</span>
</div>
</div>

  css

    .box-2 {
float: left;
width: 400px;
height: 400px;
border: solid 1px black;
display: flex;
flex-wrap: wrap;
align-content: space-between;
}
.column {
flex-basis: 100%;
display:flex;
justify-content: space-between; }
.column span{
display: inline-block;
text-align: center;
color: #fff;
width: 80px;
height: 80px;
border-radius: 50%;
background-color: #000;
}
  9元素水平和垂直平分容器

  

  html

<div class="box-3">
<div class="row">
<span>1</span>
<span>2</span>
<span>3</span>
</div>
<div class="row">
<span>4</span>
<span>5</span>
<span>6</span>
</div>
<div class="row">
<span>7</span>
<span>8</span>
<span>9</span>
</div>
</div>

  css

    .box-3 {
width: 180px;
height: 190px;
border: solid 1px black;
display: flex;
flex-wrap: wrap;
align-items: center;
}
.row{
flex-basis: 100%;
display: flex;
justify-content: space-between;
flex:;
}
.box-3 span {
display: inline-block;
text-align: center;
color: #fff;
width: 50px;
height: 50px;
border-radius: 50%;
background-color: #000;
}

  常见的布局

 

  html

    <h1>网格布局</h1>
<div class="grid grid1">
<div class="grid-cell u-lof2" >1/2</div>
<div class="grid-cell">auto</div>
<div class="grid-cell u-lof3">1/3</div>
<div class="grid-cell u-lof3">1/3</div>
<div class="grid-cell">auto</div>
<div class="grid-cell u-lof3">1/3</div>
<div class="grid-cell u-lof4">1/4</div>
<div class="grid-cell u-lof3">1/3</div>
<div class="grid-cell">auto</div>
</div>

  css

  .grid {
display: flex;
margin:30px auto;
width: 100%;
text-align: center;
border-radius:6px;
background-color: #eee;
box-shadow: 0 2px 10px 0 #111 ,
insert 0 2px 0 0 #fff;
}
.grid .grid-cell{
margin: 10px;
height: 30px;
border-radius: 5px;
background-color: #454545;
line-height: 30px;
text-align: center;
color: #fff;
}
.grid1 {
display: flex;
flex-wrap: wrap;
}
.grid1 .grid-cell{
flex:;
}
.grid1 .u-lof2 {
flex:0 0 50%;
}
.grid1 .u-lof3{
flex: 0 0 33.333%;
}
.grid1 .u-lof4{
flex: 0 0 25%
}

  html

    <h1>圣杯布局</h1>
<div class="grid holy-grid">
<div class="header">header</div>
<div class="content">
<div class="nav">nav</div>
<div class="main">main</div>
<div class="ads">ads</div>
</div>
<div class="footer">footer</div>
</div>

  css

  .holy-grid {
display: flex;
flex-direction: column;
}
.holy-grid .header,
.holy-grid .footer{
flex:;
height: 40px;
background-color: #676767;
line-height: 40px;
color: #fff;
}
.holy-grid .header {
border-top-right-radius: 5px;
border-top-left-radius: 5px;
}
.holy-grid .footer {
border-bottom-left-radius: 5px;
border-bottom-right-radius: 5px;
}
.holy-grid .content{
display: flex;
flex:;
min-height: 300px;
line-height: 300px;
}
.holy-grid .nav,
.holy-grid .ads{
flex: 0 0 70px;
}
.holy-grid .main{
flex:;
}
.holy-grid .nav {
order:;
background-color: #a1a1a1;
}
.holy-grid .ads {
background-color: #d3d3d3;
}

  html

    <h1>悬挂式布局</h1>
<div class="Media">
<img src="./jr.png" alt="" class="Media-figure">
<div class="Media-body">
<p class="body-title">JR禁赛真正原因曝光:他向助教身上撒了一碗汤</p>
<div class="body-content">
腾讯体育讯(文/Brian Windhorst和Dave McMenamin,ESPN撰稿人) 北京时间3月3日,据ESPN报道,骑士后卫JR-史密斯被球队禁赛一场的原因曝光,原来是因为他向助理教练达蒙-琼斯身上撒了一碗汤。
</div>
</div>
</div>

  css

  .Media {
display: flex;
align-items: flex-start; }
.Media-figure {
margin-right: 1em;
width: 200px;
height: 100px;
}
.Media-body {
border-radius: 5px;
background-color: #eee;
flex:;
height: 100px;
}
.Media-body .body-title{
font-weight: bold;
margin:;
}
.Media-body .body-content {
margin:;
}

  

  注意:设置align-items属性时必须保证容器有高度   

    .item {flex: 1;}
    .item {
     flex-grow: 1;
     flex-shrink: 1;
    flex-basis: 0%;
    }
    这两者是等价的,三者含义见flexbox语法

  

  参考文献:

       https://codepen.io/lcc19941214/pen/vLdVNg

       http://www.ruanyifeng.com/blog/2015/07/flex-examples.html

复习 | 彻底弄懂Flexbox之Demo篇的更多相关文章

  1. 30分钟彻底弄懂flex布局

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由elson发表于云+社区专栏 目前在不考虑IE以及低端安卓机(4.3-)的兼容下,已经可以放心使用flex进行布局了.什么是flex布 ...

  2. 彻底弄懂JS的事件冒泡和事件捕获(不推荐阅读)

    由于搬去敌台了,好久没来博客园,今天无意中翻到有“误认子弟”的评论,这里特意做个说明. 本文中关于事件冒泡和事件捕获的描述和例子都是OK的,错就错在后面用jquery去展示了利用事件冒泡的例子有误,其 ...

  3. 彻底弄懂LSH之simHash算法

    马克·吐温曾经说过,所谓经典小说,就是指很多人希望读过,但很少人真正花时间去读的小说.这种说法同样适用于“经典”的计算机书籍. 最近一直在看LSH,不过由于matlab基础比较差,一直没搞懂.最近看的 ...

  4. SQL Server-聚焦NOLOCK、UPDLOCK、HOLDLOCK、READPAST你弄懂多少?(三十四)

    前言 时间流逝比较快,博主也在快马加鞭学习SQL Server,下班回来再晚也不忘记更新下博客,时间挤挤总会有的,现在的努力求的是未来所谓的安稳,每学一门为的是深度而不是广度,求的是知识自成体系而不是 ...

  5. 彻底弄懂AngularJS中的transclusion

    点击查看AngularJS系列目录 彻底弄懂AngularJS中的transclusion AngularJS中指令的重要性是不言而喻的,指令让我们可以创建自己的HTML标记,它将自定义元素变成了一个 ...

  6. Golang, 以 9 个简短代码片段,弄懂 defer 的使用特点

    作者:林冠宏 / 指尖下的幽灵 掘金:https://juejin.im/user/587f0dfe128fe100570ce2d8 博客:http://www.cnblogs.com/linguan ...

  7. 一文弄懂神经网络中的反向传播法——BackPropagation【转】

    本文转载自:https://www.cnblogs.com/charlotte77/p/5629865.html 一文弄懂神经网络中的反向传播法——BackPropagation   最近在看深度学习 ...

  8. 弄懂flex布局

    目前在不考虑IE以及低端安卓机(4.3-)的兼容下,已经可以放心使用flex进行布局了.什么是flex布局以及它的好处,这里就不再赘述. 在这篇文章里,想说说flex布局的属性语法及其细节.那么网上也 ...

  9. [转]js模块化编程之彻底弄懂CommonJS和AMD/CMD!

    原文: https://www.cnblogs.com/chenguangliang/p/5856701.html ------------------------------------------ ...

随机推荐

  1. 初始化vtable

    在InstanceKlass::link_class_impl()方法中完成方法连接后会继续初始化vtable与itable,之前已经介绍过vtable与itable,并且在类解析过程中已经完成了大小 ...

  2. Java 10更新汇总,新的编译器通吃主流编程语言

    早些时候Oracle发布了Java 10,这是Oracle更改发布策略之后的第一版Java,Oracle现已决定每六个月发布一个新的Java版本,周期缩短了,但是并不是说我们要学的更多了,而是说缩短开 ...

  3. 【笔记】springCloud--Alibaba--nacos介绍----启动报错解决方案

    Nacos介绍 · 欢迎来到 Nacos 的世界! · Nacos 致力于帮助您发现.配置和管理微服务.Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现.服务配置.服务元数据及流量管 ...

  4. Java继承后访问成员的特点

    继承后的特点--成员变量 对象访问成员变量时,会先在子类中查找有没有定义对应的变量,若子类中存在就会就近使用子类中的变量,若子类中没有定义就会沿着继承关系往上找有没有定义相应的变量,若父类中也没有则编 ...

  5. 第一篇博客 Python开发环境配置

    本文主要介绍Windows7环境下安装并配置Anaconda+VSCode作为Python开发环境. 目录 Anaconda与包管理配 Anaconda安装 添加环境变量 Anaconda安装错误及解 ...

  6. muduo源码解析11-logger类

    logger: class logger { }; 在说这个logger类之前,先看1个关键的内部类 Impl private: //logger内部数据实现类Impl,内部含有以下成员变量 //时间 ...

  7. 工作不到一年,做出了100k系统,老板给我升职加薪

    看了下自己上一次发技术文还是在6月15日,算了算也是两个来月了.别怕,短暂的离开,是为了更好的相遇. 来到新公司以后啊,发现公司的搜索业务是真的太多了,大大小小有几百个搜索业务.来了之后得先梳理.熟悉 ...

  8. 使用OpenCV和Python构建自己的车辆检测模型

    概述 你对智慧城市的想法感到兴奋吗?如果是的话,你会喜欢这个关于建立你自己的车辆检测系统的教程的 在深入实现部分之前,我们将首先了解如何检测视频中的移动目标 我们将使用OpenCV和Python构建自 ...

  9. Pyinstaller打包: 将资源文件或文件夹打包到最后生成的exe中

    前提:用pyinstaller打包时部分资源文件可以利用qrc转成py文件来读取,但是有部分文件类型不适用. 原理:Pyinstaller 将资源文件一起打包到exe中.当exe运行时,会生成一个临时 ...

  10. GET和POST的本质区别

    前言:相信小伙伴们面试时候一定都遇到过这个问题,即使没有遇到过,至少也听说过,网上资料一大片,大概每个人都能说出来一些.但是总感觉面试装逼不成功,所以就翻阅了部分资料,进一步整理了下. 一般当我们提到 ...