弹性容器与弹性项目
1.任何元素都可以通过display:flex属性来转换为弹性盒子元素
2.使用display:flex属性的元素叫flex容器
3.flex容器中的元素叫做flex项目
4.flex容器中的flex项目都会变成行内块元素
5.转变为flex元素后,它内部子元素就支持flex布局
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>弹性容器与弹性项目</title>
<style>
*{box-sizing: border-box;}
.main{
display: flex;
border: 1px solid;
height: 20em;
}
.main>.imt{
border: 1px solid;
width: 10em;
height: 8em;
}
</style>
</head>
<body>
<div class="main">
<div class="imt">imt1</div>
<div class="imt">imt2</div>
<div class="imt">imt3</div>
</div>
</body>
</html>
flex排列方式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>flex排列方式</title>
<style>
*{
box-sizing: border-box;
}
.main{
display: flex;
height: 15em;
border: 1px solid;
}
/* 单行排列 */
.text{
width: 5em;
border: 1px solid;
}
.main{
flex-flow: row nowrap;
}
/* 多行排列 */
.text{
width: 5em;
height: 5em;
border: 1px solid;
}
.main{
flex-flow: row wrap;
}
/* 主轴为列 */
.text{
width: 5em;
height: 5em;
border: 1px solid;
}
.main{
flex-flow: column nowrap;
}
/*列换行 */
.text{
width: 5em;
height: 5em;
border: 1px solid;
}
.main{
flex-flow: column wrap;
}
</style>
</head>
<body>
<div class="main">
<div class="text">text1</div>
<div class="text">text2</div>
<div class="text">text3</div>
<div class="text">text4</div>
<div class="text">text5</div>
<div class="text">text6</div>
<div class="text">text7</div>
<div class="text">text8</div>
</div>
</body>
</html>
弹性项目在主轴上的排列方式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>弹性项目在主轴上的排列方式</title>
<style>
*{box-sizing: border-box;}
.main{
display: flex;
height: 10em;
border: 1px solid;
}
.text{
width: 4em;
/* height: 2em; */
border: 1px solid;
}
/* 设置在单行容器上对齐前提:主轴上存在剩余空间 */
/* 空间分配有两种 */
/* 1.将所有项目视为一体,在项目两边进行分配 */
.main{
justify-content: center;
justify-content: flex-start;
justify-content: flex-end;
}
/* 2.将项目分为一个个独立的个体,在项目之间分配 */
.main{
/* 分散对齐 */
justify-content: space-around;
/* 两端对齐 */
justify-content: space-between;
/* 平均对齐 */
justify-content: space-evenly;
}
/* 交叉轴对齐方式 */
.main{
/* 默认拉伸 */
align-items: stretch;
/* 顶部对齐 */
align-items: flex-start;
/* 底部对齐 */
align-items: flex-end;
/* 居中 */
align-items: center;
}
/* 注意:单行容器切项目在主轴排列,交叉轴不存在适应空间.原因是单行容器,在交叉轴上只有一行没有多行 */
</style>
</head>
<body>
<div class="main">
<div class="text">text1</div>
<div class="text">text2</div>
<div class="text">text3</div>
<div class="text">text4</div>
</div>
</body>
</html>
flex对齐方式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>flex对齐方式</title>
<style>
*{box-sizing: border-box;}
.main{
display: flex;
height: 10em;
border: 1px solid;
}
.text{
width: 4em;
height: 2em;
border: 1px solid;
}
/* 设置在单行容器上对齐前提:主轴上存在剩余空间 */
/* 空间分配有两种 */
/* 1.将所有项目视为一体,在项目两边进行分配 */
.main{
flex-flow: row wrap;
}
.main{
align-content: center;
align-content: flex-start;
align-content: flex-end;
align-content: space-around;
align-content: space-between;
align-content: space-evenly;
}
</style>
</head>
<body>
<div class="main">
<div class="text">text1</div>
<div class="text">text2</div>
<div class="text">text3</div>
<div class="text">text4</div>
<div class="text">text5</div>
<div class="text">text6</div>
<div class="text">text7</div>
<div class="text">text8</div>
<div class="text">text9</div>
<div class="text">text10</div>
<div class="text">text11</div>
<div class="text">text12</div>
<div class="text">text1</div>
<div class="text">text2</div>
<div class="text">text3</div>
<div class="text">text4</div>
<div class="text">text5</div>
<div class="text">text6</div>
<div class="text">text7</div>
<div class="text">text8</div>
<div class="text">text9</div>
<div class="text">text10</div>
<div class="text">text11</div>
<div class="text">text12</div>
</div>
</body>
</html>