Flex布局学习总结

    xiaoxiao2021-03-25  83

    概念部分就将通过学习阮一峰的Flex 布局教程总结的一些问答内容贴在下面:

    1.使用flex布局有什么好处?

    答:布局的传统解决方案,基于盒状模型,依赖 display属性 + position属性 + float属性。它对于那些特殊布局非常不方便,比如,垂直居中就不容易实现。2009年,W3C提出了一种新的方案----Flex布局,可以简便、完整、响应式地实现各种页面布局。目前,它已经得到了所有浏览器的支持,这意味着,现在就能很安全地使用这项功能。

    2.flex布局的简单使用?

    答:(1)任何一个容器都可以指定为Flex布局。 .box{ display: flex; } (2)行内元素也可以使用Flex布局。 .box{ display: inline-flex; } (3)Webkit内核的浏览器,必须加上-webkit前缀。 .box{ display: -webkit-flex; display: flex;}

    3.注意:设为Flex布局以后,子元素的float、clear和vertical-align属性将失效。

    4.简述一下flex布局的基本概念?

    答:采用Flex布局的元素,称为Flex容器。它的所有子元素自动成为容器成员,称为Flex项目。容器默认存在两根轴:水平的主轴(main axis)和垂直的交叉轴(cross axis)。

    5.简述一下容器的属性?

    答:(1)flex-direction属性决定主轴的方向(即项目的排列方向)。 .box { flex-direction: row | row-reverse | column | column-reverse;} row(默认值):主轴为水平方向,起点在左端。 row-reverse:主轴为水平方向,起点在右端。 column:主轴为垂直方向,起点在上沿。 column-reverse:主轴为垂直方向,起点在下沿。 (2)flex-wrap属性定义,如果一条轴线排不下,如何换行。 .box{ flex-wrap: nowrap | wrap | wrap-reverse; } nowrap(默认):不换行。 wrap:换行,第一行在上方。 wrap-reverse:换行,第一行在下方。 (3)flex-flow属性是flex-direction属性和flex-wrap属性的简写形式,默认值为row nowrap .box { row nowrap } (4)justify-content属性定义了项目在容器主轴上的对齐方式。 .box { justify-content: flex-start | flex-end | center | space-between | space-around; } flex-start(默认值):左对齐 flex-end:右对齐 center: 居中 space-between:两端对齐,项目之间的间隔都相等。 space-around:每个项目两侧的间隔相等。所以,项目之间的间隔比项目与边框的间隔大一倍。 (5)align-items属性定义项目在容器交叉轴上如何对齐。 .box { align-items: flex-start | flex-end | center | baseline | stretch; } flex-start:交叉轴的起点对齐。 flex-end:交叉轴的终点对齐。 center:交叉轴的中点对齐。 baseline: 项目的第一行文字的基线对齐。 stretch(默认值):如果项目未设置高度或设为auto,将占满整个容器的高度。 (6)align-content属性定义了多根轴线的对齐方式。如果项目只有一根轴线,该属性不起作用。 .box { align-content: flex-start | flex-end | center | space-between | space-around | stretch; } flex-start:与交叉轴的起点对齐。 flex-end:与交叉轴的终点对齐。 center:与交叉轴的中点对齐。 space-between:与交叉轴两端对齐,轴线之间的间隔平均分布。 space-around:每根轴线两侧的间隔都相等。所以,轴线之间的间隔比轴线与边框的间隔大一倍。 stretch(默认值):轴线占满整个交叉轴。

    6.简述一下项目的属性?

    答:(1)order属性定义项目的排列顺序。数值越小,排列越靠前,默认为0。 .item { order: <integer>; } (2)flex-grow属性定义项目的放大比例,默认为0,即如果存在剩余空间,也不放大。 .item { flex-grow: <number>; /* default 0 */ } 如果所有项目的flex-grow属性都为1,则它们将等分剩余空间(如果有的话)。如果一个项目的flex-grow属性为2,其他项目都为1,则前者占据的剩余空间将比其他项多一倍。 (3)flex-shrink属性定义了项目的缩小比例,默认为1,即如果空间不足,该项目将缩小。 .item { flex-shrink: <number>; /* default 1 */ } 如果所有项目的flex-shrink属性都为1,当空间不足时,都将等比例缩小。如果一个项目的flex-shrink属性为0,其他项目都为1,则空间不足时,前者不缩小。 负值对该属性无效。 (4)flex-basis属性定义了在分配多余空间之前,项目占据的主轴空间(main size)。浏览器根据这个属性,计算主轴是否有多余空间。它的默认值为auto,即项目的本来大小。 .item { flex-basis: <length> | auto; /* default auto */ } 它可以设为跟widthheight属性一样的值(比如350px),则项目将占据固定空间。 (5)flex属性是flex-grow, flex-shrinkflex-basis的简写,默认值为0 1 auto。后两个属性可选。 该属性有两个快捷值:auto (1 1 auto) 和 none (0 0 auto)。 建议优先使用这个属性,而不是单独写三个分离的属性,因为浏览器会推算相关值。 (6)align-self属性允许单个项目有与其他项目不一样的对齐方式,可覆盖align-items属性。默认值为auto,表示继承父元素的align-items属性,如果没有父元素,则等同于stretch。该属性可能取6个值,除了auto,其他都与align-items属性完全一致。

    ··············································································我是一条分割线····························································

    接下来就实现一些简单的布局: 1.先模拟实现阮一峰举的“骰子”布局的例子: (1-1):

    <!--.box模拟浏览器页面(骰子),.point模拟页面中的内容(点) --> <div class="box"> <div class="point"></div> </div> <!--此处设置宽高只是想固定样式,演示布局效果,忽略了响应式效果 --> .box{ width: 150px; height: 150px; background-color: #000; display: flex; } .point{ width: 40px; height: 40px; background-color: #fff; border-radius: 100%; }

    (1-2):

    布局部分同上 .box{ width: 150px; height: 150px; background-color: #000; display: flex; justify-content: center; } .point{ width: 40px; height: 40px; background-color: #fff; border-radius: 100%; }

    (1-3):

    布局部分同上 .box{ width: 150px; height: 150px; background-color: #000; display: flex; justify-content: flex-end; } .point{ width: 40px; height: 40px; background-color: #fff; border-radius: 100%; }

    (1-4):

    布局部分同上 .box{ width: 150px; height: 150px; background-color: #000; display: flex; justify-content: flex-end; align-items: flex-end; } .point{ width: 40px; height: 40px; background-color: #fff; border-radius: 100%; }

    (1-5)“1点”:

    布局部分同上 .box{ width: 150px; height: 150px; background-color: #000; display: flex; justify-content: center; align-items: center; } .point{ width: 40px; height: 40px; background-color: #fff; border-radius: 100%; }

    (2-1):

    <!--.box模拟浏览器页面(骰子),.point模拟页面中的内容(点) --> <div class="box"> <div class="point"></div> <div class="point"></div> </div> .box{ width: 150px; height: 150px; background-color: #000; display: flex; justify-content: space-between; } .point{ width: 40px; height: 40px; background-color: #fff; border-radius: 100%; }

    (2-2):

    布局部分同上 .box{ width: 150px; height: 150px; background-color: #000; display: flex; flex-direction: column; justify-content: space-between; } .point{ width: 40px; height: 40px; background-color: #fff; border-radius: 100%; }

    (2-3):

    布局部分同上 .box{ width: 150px; height: 150px; background-color: #000; display: flex; flex-direction: column; justify-content: space-between; align-items: center; } .point{ width: 40px; height: 40px; background-color: #fff; border-radius: 100%; }

    (2-4):

    布局部分同上 .box{ width: 150px; height: 150px; background-color: #000; display: flex; } .point{ width: 40px; height: 40px; background-color: #fff; border-radius: 100%; } .point:nth-child(2){ align-self: center; }

    (2-5)“2点”:

    布局部分同上 .box{ width: 150px; height: 150px; background-color: #000; display: flex; justify-content: space-between; } .point{ width: 40px; height: 40px; background-color: #fff; border-radius: 100%; } .point:nth-child(2){ align-self: flex-end; }

    (3)”3点“:

    <div class="box"> <div class="point"></div> <div class="point"></div> <div class="point"></div> </div> .box{ width: 150px; height: 150px; background-color: #000; display: flex; } .point{ width: 40px; height: 40px; background-color: #fff; border-radius: 100%; } .point:nth-child(2){ align-self: center; } .point:nth-child(3){ align-self: flex-end; }

    第三个点不在右下角的原因我分析是因为我给定了宽高,浏览器计算显示方式的时候产生了这个结果

    (4)“4点”:

    <!--.box模拟浏览器页面(骰子),.point模拟页面中的内容(点) --> <div class="box"> <div class="column"> <div class="item"></div> <div class="item"></div> </div> <div class="column"> <div class="item"></div> <div class="item"></div> </div> .box{ width: 150px; height: 150px; background-color: #000; display: flex; flex-wrap: wrap; align-content: space-between; } .column { flex-basis: 100%; display: flex; justify-content: space-between; } .item{ width: 40px; height: 40px; background-color: #fff; border-radius: 100%; }

    (5-1)“6点”:

    <div class="box"> <div class="column"> <div class="item"></div> <div class="item"></div> <div class="item"></div> </div> <div class="column"> <div class="item"></div> <div class="item"></div> <div class="item"></div> </div> .box{ width: 150px; height: 150px; background-color: #000; display: flex; justify-content: space-between; } .item{ width: 40px; height: 40px; background-color: #fff; border-radius: 100%; }

    (5-2)“6点”:

    项目与项目之间没有间隔的原因我不清楚,但是隐约感觉还是因为给定了宽高

    2.写出圣杯布局:

    <!DOCTYPE html> <html lang="en"> <head> <style> body { font: 24px Helvetica; background: #999999; } #main { min-height: 800px; margin: 0px; padding: 0px; display: -webkit-flex; display: flex; -webkit-flex-flow: row; flex-flow: row; } #main > article { margin: 4px; padding: 5px; border: 1px solid #cccc33; border-radius: 7pt; background: #dddd88; -webkit-flex: 3 1 60%; flex: 3 1 60%; -webkit-order: 2; order: 2; } #main > nav { margin: 4px; padding: 5px; border: 1px solid #8888bb; border-radius: 7pt; background: #ccccff; -webkit-flex: 1 6 20%; flex: 1 6 20%; -webkit-order: 1; order: 1; } #main > aside { margin: 4px; padding: 5px; border: 1px solid #8888bb; border-radius: 7pt; background: #ccccff; -webkit-flex: 1 6 20%; flex: 1 6 20%; -webkit-order: 3; order: 3; } header, footer { display: block; margin: 4px; padding: 5px; min-height: 100px; border: 1px solid #eebb55; border-radius: 7pt; background: #ffeebb; } /* 窄到已不足以支持三栏 */ @media all and (max-width: 640px) { #main{ -webkit-flex-flow: column; flex-direction: column; } #main > article, #main > nav, #main > aside { /* 恢复到文档内的自然顺序 */ -webkit-order: 0; order: 0; } #main > nav, #main > aside, header, footer { min-height: 50px; max-height: 50px; } } </style> </head> <body> <header>header</header> <div id='main'> <article>article</article> <nav>nav</nav> <aside>aside</aside> </div> <footer>footer</footer> </body> </html>

    当屏幕宽度大于640px时,布局如下:

    当屏幕宽度小于640px时,布局如下:

    转载请注明原文地址: https://ju.6miu.com/read-32386.html

    最新回复(0)