flex布局涉及到的12个属性和实例
1.为什么要使用flex
布局?
flex
意为弹性布局,布局元素大小在flex
容器中自动伸缩,以适应容器的变化,特别适合响应式布局
2.容器上的6个属性
序号 | 名称 | 描述 |
---|---|---|
1. | flex-direction | 主轴方向 |
2. | flex-wrap | 主轴上项目换行方式 |
3. | flex-flow | 主轴方向和主轴上的项目换行方式的复合属性 |
4. | justify-content | 主轴上的项目对齐方式 |
5. | align-items | 交叉轴上的项目对齐方式(适用于单行容器) |
6. | align-content | 交叉轴上的项目对齐方式(适用于多行容器) |
2.1主轴方向flex-direction
2.1.1源代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.flex-container {
/* 将容器设置为flex容器 */
display: flex;
/* 设置主轴的方向,默认从左到右排列 */
flex-direction: row;
/* 主轴方向从右到左 */
/* flex-direction: row-reverse; */
/* 主轴方向从上到下 */
/* flex-direction: column; */
/* 主轴方向从下到上 */
/* flex-direction: column-reverse; */
width: 300px;
height: 150px;
}
.item {
width: 100px;
height: 100px;
}
</style>
<title>Document</title>
</head>
<body>
<div class="flex-container">
<div class="item">flex1</div>
<div class="item">flex2</div>
<div class="item">flex3</div>
<div class="item">flex4</div>
</div>
</body>
</html>
2.1.2效果图
2.2项目换行方式flex-wrap
2.2.1源代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.flex-container {
display: flex;
/* 设置主轴方向项目换行方式,默认不换行 */
flex-wrap: nowrap;
/* 换行 */
/* flex-wrap: wrap; */
/* 反向换行 */
/* flex-wrap: wrap-reverse; */
width: 300px;
height: 300px;
}
.item {
width: 100px;
height: 100px;
}
</style>
<title>Document</title>
</head>
<body>
<div class="flex-container">
<div class="item">flex1</div>
<div class="item">flex2</div>
<div class="item">flex3</div>
<div class="item">flex4</div>
</div>
</body>
</html>
2.2.2效果图
2.3flex-flow
2.3.1源代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.flex-container {
display: flex;
/* flex-flow:为flex-direction和flex-wrap的复合属性 */
flex-flow: row wrap;
width: 300px;
height: 300px;
}
.item {
width: 100px;
height: 100px;
}
</style>
<title>Document</title>
</head>
<body>
<div class="flex-container">
<div class="item">flex1</div>
<div class="item">flex2</div>
<div class="item">flex3</div>
<div class="item">flex4</div>
</div>
</body>
</html>
2.3.2效果图
2,4.主轴上的项目对齐方式justify-content
2.4.1源代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.flex-container {
display: flex;
width: 500px;
height: 150px;
/* 主轴上项目对齐方式,默认左对齐 */
justify-content: flex-start;
/* 右对齐 */
/* justify-content: flex-end; */
/* 居中对齐 */
/* justify-content: center; */
/* 两端对齐 */
/* justify-content: space-between; */
/* 分散对齐,项目间距是两侧两倍 */
/* justify-content: space-around; */
/* 平均对齐 */
/* justify-content: space-evenly; */
}
.item {
width: 100px;
height: 100px;
}
</style>
<title>Document</title>
</head>
<body>
<div class="flex-container">
<div class="item">flex1</div>
<div class="item">flex2</div>
<div class="item">flex3</div>
<div class="item">flex4</div>
</div>
</body>
</html>
2.4.2效果图
2.5交叉轴上的项目对齐方式(适用于单行容器)align-items
2.5.1源代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.flex-container {
display: flex;
/* 交叉轴对齐方式,默认为起始线对齐 */
align-items: flex-start;
/* 终止线对齐 */
/* align-items: flex-end; */
/* 居中对齐 */
/* align-items: center; */
width: 400px;
height: 150px;
}
.item {
width: 100px;
height: 100px;
}
</style>
<title>Document</title>
</head>
<body>
<div class="flex-container">
<div class="item">flex1</div>
<div class="item">flex2</div>
<div class="item">flex3</div>
<div class="item">flex4</div>
</div>
</body>
</html>
2.5.2效果图
2.6交叉轴上的项目对齐方式(适用于多行容器)
2.6.1源代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.flex-container {
display: flex;
flex-flow: row wrap;
/* 多行容器项目交叉轴对齐方式,默认为stretch */
/* align-content: stretch; */
/* 起始线对齐 */
align-content: flex-start;
/* 终止线对齐 */
/* align-content: flex-end; */
/* 居中对齐 */
/* align-content: center; */
/* 两端对齐 */
/* align-content: space-between; */
/* 分散对齐 */
/* align-content: space-around; */
/* 平均对齐 */
/* align-content: space-evenly; */
width: 300px;
height: 300px;
}
.item {
width: 100px;
height: 100px;
}
</style>
<title>Document</title>
</head>
<body>
<div class="flex-container">
<div class="item">flex1</div>
<div class="item">flex2</div>
<div class="item">flex3</div>
<div class="item">flex4</div>
</div>
</body>
</html>
2.6.2效果图
效果图同主轴对齐方式
3.项目上的6个属性
序号 | 名称 | 描述 |
---|---|---|
1. | order | 项目排序 |
2. | align-self | 交叉轴独立对齐方式 |
3. | ||
3. | ||
3. | ||
3. |
3.1项目排序order
3.1.1源代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.flex-container {
display: flex;
width: 400px;
/* 项目排序,默认为0,支持负数
数值越小排在前面
*/
/* order: 0; */
}
.item {
width: 100px;
height: 100px;
}
.item:first-of-type {
order: 4;
}
.item:nth-of-type(2) {
order: 1;
}
.item:nth-of-type(3) {
/* 支持负数排序 */
order: -1;
}
.item:last-of-type {
order: 6;
}
</style>
<title>Document</title>
</head>
<body>
<div class="flex-container">
<div class="item">flex1</div>
<div class="item">flex2</div>
<div class="item">flex3</div>
<div class="item">flex4</div>
</div>
</body>
</html>
3.1.2效果图
4.总结
- flex基础虽然已经掌握,但是实战中用的还是不是很熟练
- 常用的属性可以写成复合属性,来简化代码
- 后端学习时还可以再仔细研究和练习下