赞
踩
弹性布局具有以下特点:
1、主轴与交叉轴
弹性容器具有主轴(main axis)和交叉轴(cross axis)。默认情况下,主轴是水平方向,交叉轴是垂直方向。()()
2、弹性容器
通过将父元素的display属性设置为flex或inline-flex来创建弹性容器。
子元素的弹性项目:弹性容器中的每个子元素都成为弹性项目。子元素可以指定各自在主轴和交叉轴上的大小、顺序以及对齐方式等。
3、主轴对齐
弹性项目可以在主轴上按照一定比例分配空间,也可以使用justify-content属性定义主轴的对齐方式。
4、交叉轴对齐
弹性项目可以在交叉轴上进行对齐,包括顶部对齐、底部对齐、居中对齐等,使用align-items属性定义交叉轴对齐方式。
5、换行与自动调整
可控制弹性项目是否换行,并且具备自动调整元素大小的能力。
弹性布局简化了网页布局的开发过程,提供了更灵活、响应式的布局方式。它适用于各种屏幕尺寸和设备类型,并能够快速适应不同的布局需求。
下面属性示例用到的测试代码1
<!DOCTYPE html> <html> <head> <!-- <mata charset="UTF-8"> --> <meta http-equiv="content-Type" content="text/html; charset=UTF-8" /> <title></title> <style> .content { width: 600px; height: 200px; border: 1px solid black; } .flexDiv { height: 150px; width: 150px; } .bgColorRed { background-color: red } .bgColorBlue { background-color: blue } .bgColorGreen { background-color: green } </style> </head> <body> <div class="content"> <div class="flexDiv bgColorRed">div1</div> <div class="flexDiv bgColorBlue">div2</div> <div class="flexDiv bgColorGreen">div2</div> </div> </body> <script> </script>
给测试代码外侧div加上flex,使其变成弹性布局
.flex {
display: flex;
}
<div class="content flex">
justify-content属性定义了项目在主轴上的对齐方式。
justify-content: flex-start | flex-end | center | space-between | space-around
我们将上述的css样式依次添加给class=content的div标签
.box1 {
/* 主轴对齐方式 */
/* justify-content: flex-start | flex-end | center | space-between | space-around; */
justify-content: center;
}
<div class="content flex box1">
align-items属性定义项目在交叉轴上如何对齐。
align-items: flex-start | flex-end | center | baseline | stretch
.box2 {
/* 交叉轴对齐方式 */
/* align-items: flex-start | flex-end | center | baseline | stretch; */
align-items: center
}
<div class="content flex box2">
去除项目的高度
.flexDiv {
/* height: 150px; */
width: 150px;
}
<div class="content flex box2">
<div class="flexDiv bgColorRed">
<div>div1</div>
</div>
<div class="flexDiv bgColorBlue">
<div style="margin-top: 20px;">div2</div>
</div>
<div class="flexDiv bgColorGreen">
<div style="margin-top: 40px;">div3</div>
</div>
</div>
决定主轴的方向,水平或者垂直
flex-direction: row | row-reverse | column | column-reverse
.box3 {
/* 决定主轴的方向,水平或者垂直 */
/* flex-direction: row | row-reverse | column | column-reverse; */
flex-direction: row
}
<div class="content flex box3">
换行不换行以及换行的方向
flex-wrap: nowrap | wrap | wrap-reverse;
flex-wrap的测试需要改变下页面结构,用测试代码2
<!DOCTYPE html> <html> <head> <!-- <mata charset="UTF-8"> --> <meta http-equiv="content-Type" content="text/html; charset=UTF-8" /> <title></title> <style> .content { width: 600px; height: 200px; border: 1px solid black; } .flexDiv2 { height: 100px; width: 150px; } .bgColorRed { background-color: red } .bgColorBlue { background-color: blue } .bgColorGreen { background-color: green } .bgColorPurple { background-color: purple } .bgColorYellow { background-color: yellow } .bgColorPink { background-color: pink } .flex { display: flex; } </style> </head> <body> <div class="content flex"> <div class="flexDiv2 bgColorRed">div1</div> <div class="flexDiv2 bgColorBlue">div2</div> <div class="flexDiv2 bgColorGreen">div3</div> <div class="flexDiv2 bgColorPurple">div4</div> <div class="flexDiv2 bgColorYellow">div5</div> <div class="flexDiv2 bgColorPink">div6</div> </div> </body> <script> </script>
.box4 {
/* 换行不换行以及换行的方向 */
/* flex-wrap: nowrap | wrap | wrap-reverse; */
flex-wrap: nowrap
}
<div class="content flex box4">
flex-flow: <flex-direction> || <flex-wrap>;
align-content属性定义了多根轴线的对齐方式。如果项目只有一根轴线,该属性不起作用。
align-content:flex-start | flex-end | center | space-between | space-around | stretch;
下面是测试代码3
<!DOCTYPE html> <html> <head> <!-- <mata charset="UTF-8"> --> <meta http-equiv="content-Type" content="text/html; charset=UTF-8" /> <title></title> <style> .content { width: 600px; height: 400px; border: 1px solid black; } .flexDiv2 { height: 100px; width: 150px; } .bgColorRed { background-color: red } .bgColorBlue { background-color: blue } .bgColorGreen { background-color: green } .bgColorPurple { background-color: purple } .bgColorYellow { background-color: yellow } .bgColorPink { background-color: pink } .bgColorMaroon { background-color: maroon } .bgColorSilver { background-color: silver } .bgColorOrange { background-color: orange } .flex { display: flex; } .box4 { /* 换行 */ flex-wrap: wrap; } </style> </head> <body> <div class="content flex box4"> <div class="flexDiv2 bgColorRed">div1</div> <div class="flexDiv2 bgColorBlue">div2</div> <div class="flexDiv2 bgColorGreen">div3</div> <div class="flexDiv2 bgColorPurple">div4</div> <div class="flexDiv2 bgColorYellow">div5</div> <div class="flexDiv2 bgColorPink">div6</div> <div class="flexDiv2 bgColorMaroon">div7</div> <div class="flexDiv2 bgColorSilver">div8</div> <div class="flexDiv2 bgColorOrange">div9</div> </div> </body> <script> </script>
.box5 {
/* 定义了多根轴线的对齐方式。如果项目只有一根轴线,该属性不起作用。 */
/* align-content: flex-start | flex-end | center | space-between | space-around | stretch; */
align-content: flex-start;
}
<div class="content flex box4 box5">
所以,轴线之间的间隔比轴线与边框的间隔大一倍。
order属性定义项目的排列顺序。数值越小,排列越靠前,默认为0。
<style> .content { width: 600px; height: 400px; border: 1px solid black; } .flexDiv { height: 150px; width: 150px; } .flexDiv2 { height: 100px; width: 150px; } .flexDiv3 { height: 200px; width: 150px; } .order1 { order: 1; } .order2 { order: 2; } .order3 { order: 3; } </style> <div class="content flex box5"> <div class="flexDiv2 order2 bgColorRed">order2</div> <div class="flexDiv3 order1 bgColorBlue">order1</div> <div class="flexDiv order3 bgColorGreen">order3</div> </div>
flex-grow: ; /* default 0 */
定义项目的放大比例,默认为0,即如果存在剩余空间,也不放大
如果所有项目的flex-grow属性都为1,则它们将等分剩余空间(如果有的话)。如果一个项目的flex-grow属性为2,其他项目都为1,则前者占据的剩余空间将比其他项多一倍。
flex-shrink: ; /* default 1 */
定义了项目的缩小比例,默认为1,即如果空间不足,该项目将缩小
如果所有项目的flex-shrink属性都为1,当空间不足时,都将等比例缩小。如果一个项目的flex-shrink属性为0,其他项目都为1,则空间不足时,前者不缩小。
负值对该属性无效
定义了在分配多余空间之前,项目占据的主轴空间(main size)。浏览器根据这个属性,计算主轴是否有多余空间。它的默认值为auto,即项目的本来大小。
flex-basis: | auto; /* default auto */
它可以设为跟width或height属性一样的值(比如350px),则项目将占据固定空间。
flex属性是flex-grow, flex-shrink 和 flex-basis的简写,默认值为0 1 auto。后两个属性可选。
flex: none | [ <‘flex-grow’> <‘flex-shrink’>? || <‘flex-basis’> ]
该属性有两个快捷值:auto (1 1 auto) 和 none (0 0 auto)。
建议优先使用这个属性,而不是单独写三个分离的属性,因为浏览器会推算相关值。
align-self属性允许单个项目有与其他项目不一样的对齐方式,可覆盖align-items属性。默认值为auto,表示继承父元素的align-items属性,如果没有父元素,则等同于stretch。
align-self: auto | flex-start | flex-end | center | baseline | stretch;
该属性可能取6个值,除了auto,其他都与align-items属性完全一致。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。