学小程序咱们须要有点html、css、js基础,而flex布局是咱们小程序经常使用的css布局,学习小程序以前,咱们须要了解一些css方面的布局知识-Flex布局,Flex 布局将成为将来布局的首选方案css
Flex 是 Flexible Box 的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性。html
传统布局小程序
flex布局浏览器
html结构:布局
<div class="container">
<span>1</span>
<span>2</span>
<span>3</span>
</div>
复制代码
.container{
display: flex;
height: 300px;
width: 80%;
background-color: red;
justify-content: space-around;
}
.container span{
background-color: blue;
/* width: 150px; */
height: 100px;
margin-right: 10px;
flex: 1;
}
复制代码
效果以下图:
学习
flex是flexible box 的缩写,意为“弹性布局”,用来为盒状模型提供更大的灵活性,任何一个容器均可以指定为flex布局。flex
采用flex布局的元素,称为flex容器(flex container),简称“容器”。它的全部子元素自动成为容器成员,称为flex项目(flex item),简称“项目”。
拿上面的例子来讲明:ui
父元素设置的常见6个属性:spa
在flex布局中,是分为主轴和侧轴两个方向,一样的说法:行和列、x轴和y轴3d
flex-direction属性决定主轴的方向(即项目的排列方向)
注意: 主轴和侧轴是会变化的,就看flex-direction设置谁为主轴,剩下的就是侧轴,而咱们的子元素是跟着主轴来排列的。
.container{
/* 给父元素添加flex 属性 */
display: flex;
height: 300px;
width: 80%;
background-color: red;
/*默认的主轴是x轴,子元素跟着主轴从左到右排列*/
/*flex-direction: row;*/
/*咱们能够把咱们的主轴设置为与轴,那么x轴就成为了侧轴,子元素沿着主轴的方向从上到下排列*/
flex-direction: column;
}
.container span{
background-color: blue;
width: 150px;
height: 100px;
margin-right: 10px;
}
复制代码
justify-content属性定义了项目在主轴上的对齐方式
注意:使用这个属性以前必定要肯定好主轴是哪一个
<div class="container">
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
</div>
复制代码
css样式效果:
.container{
/* 给父元素添加flex 属性 */
display: flex;
height: 300px;
width: 800px;
background-color: red;
/*默认的主轴是x*/
flex-direction: row;
/* justify-content是设置主轴上子元素的排列方式 */
/*让元素居中对齐*/
/* justify-content:center; */
/*平均分配剩余空间*/
/* justify-content: space-around; */
/*先贴两边,再分配剩余空间*/
justify-content: space-between;
}
.container span{
background-color: blue;
width: 150px;
height: 100px;
/* margin-right: 10px; */
}
复制代码
.container{
/* 给父元素添加flex 属性 */
display: flex;
height: 800px;
width: 800px;
background-color: red;
/*默认的主轴是x*/
flex-direction: column;
/* justify-content是设置主轴上子元素的排列方式 */
/*让元素居中对齐*/
/* justify-content:center; */
/*平均分配剩余空间*/
/* justify-content: space-around; */
/*先贴两边,再分配剩余空间*/
justify-content: space-between;
}
.container span{
background-color: blue;
width: 150px;
height: 100px;
/* margin-right: 10px; */
}
复制代码
默认状况下,项目都排在一条线上,flex-wrap属性定义,flex布局中默认是不换行的。若是装不下,会缩小子元素宽度,放到父元素里面。
该属性是控制子项在侧轴(默认是y轴)上的排列方式,在子项为单项的时候使用
.container{
/* 给父元素添加flex 属性 */
display: flex;
height: 300px;
width: 800px;
background-color: red;
/*默认的主轴是x*/
justify-content: center;
/*侧轴居中*/
align-items: center;
}
.container span{
background-color: blue;
width: 150px;
height: 100px;
margin: 10px;
}
复制代码
显示效果以下图:
设置子项在侧轴上的排列方式而且只能用于子项出现换行的状况(多行),在单行下是没有效果的
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>flex</title>
<style> .container{ /* 给父元素添加flex 属性 */ display: flex; height: 300px; width: 800px; background-color: red; /*默认的主轴是x*/ justify-content: center; flex-wrap: wrap; /*由于有了换行*/ align-content: space-around; } .container span{ background-color: blue; width: 150px; height: 100px; margin: 10px; } </style>
</head>
<body>
<div class="container">
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
<span>6</span>
</div>
</body>
</html>
复制代码
效果以下:
flex-flow属性是flex-direction和flex-wrap属性的复合属性
flex-flow: row wrap;
复制代码
flex属性定义子项目分配剩余空间,用flex来表示占了多少份数
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>flex</title>
<style>
.container{
/* 给父元素添加flex 属性 */
display: flex;
height: 150px;
width: 60%;
background-color: red;
margin: 0 auto;
}
.container div:nth-child(1) {
width: 100px;
height: 150px;
background-color: pink;
}
.container div:nth-child(2) {
flex:1;
height: 150px;
background-color: green;
}
.container div:nth-child(3) {
width: 100px;
height: 150px;
background-color: blue;
}
</style>
</head>
<body>
<section class="container">
<div>1</div>
<div>2</div>
<div>3</div>
</section>
</body>
</html>
复制代码
效果以下图:
align-self属性容许单个项目有与其余项目不同的对齐方式,可覆盖align-items属性。默认值为auto,表示继承父元素的align-items属性,若是没有父元素,则等同于stretch.
数值越小,排列越靠前,默认为0,
注意: 和z-index不同。
今天只要分享了关于移动端布局用到的flex布局,主要是各属性的用法,若是想了解更多。请扫描二维码,关注公众号: