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. docker入门4-docker stack

    stack介绍 stack是一组共享依赖,可以被编排并具备扩展能力的关联service.举例来说就是在swarm那章描述docker层次架构时,说stack就是一个完整的服务--它可以由基于flask ...

  2. idea配置ssm框架

    详细教程如下:https://blog.csdn.net/GallenZhang/article/details/5193215 https://blog.csdn.net/qq_28008917/a ...

  3. 基于Java的二叉树的三种遍历方式的递归与非递归实现

    二叉树的遍历方式包括前序遍历.中序遍历和后序遍历,其实现方式包括递归实现和非递归实现. 前序遍历:根节点 | 左子树 | 右子树 中序遍历:左子树 | 根节点 | 右子树 后序遍历:左子树 | 右子树 ...

  4. IE浏览器连接WebSocket报错:java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986

    在项目开发中整合了WebSocket,本来没什么问题了,但是偶尔发现用IE浏览器打开web端不能推送消息,因为PC端与服务器建立连接失败了.网上查了很多资料, 又看了看源码,都不对症:又怀疑是Spri ...

  5. Spring官方宣布:新的Spring OAuth2.0授权服务器已经来了

    1. 前言 记不记得之前发过一篇文章Spring 官方发起Spring Authorization Server 项目.该项目是由Spring Security主导的一个社区驱动的.独立的孵化项目.由 ...

  6. 团队作业4:第七篇Scrum冲刺博客(歪瑞古德小队)

    目录 一.Daily Scrum Meeting 1.1 会议照片 1.2 项目进展 二.项目燃尽图 三.签入记录 3.1 代码/文档签入记录 3.2 Code Review 记录 3.3 issue ...

  7. Windows中使用PowerShell查看和卸载补丁

    查看:get-hotfix -id KB4470788 卸载:wusa /uninstall /kb:3045999 get-hotfix -id KB4470788 wusa /uninstall ...

  8. Java数据结构——循环链表

    一.单向循环链表表中的最后一个节点的指针域指向头结点,整个链表形成一个环.其他的与单链表相同. (以下图片均来自网络,侵删) 插入操作 删除操作 简单实现 public class CiNode { ...

  9. mysql中的函数总结

    mysql中常用日期时间函数 MySQL服务器中的三种时区设置: ①系统时区---保存在系统变量system_time_zone ②服务器时区---保存在全局系统变量global.time_zone ...

  10. 跟我一起学.NetCore之日志作用域及第三方日志框架扩展

    前言 上一节对日志的部分核心类型进行简单的剖析,相信现在再使用日志的时候,应该大概知道怎么一回事了,比如记录器是怎么来的,是如何将日志内容写入到不同目的地的等:当然还有很多细节没深入讲解,抽时间小伙伴 ...